include iostream include iomanip include cstdio include cstring includ

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
typedef long long ll;
typedef long double ld;
#define PATH "D:\\QtProjects\\olymp\\"
const int N = 10001;
const int M = 100000;
int head[N], thead[N];
int linke[M];
int cap[M];
int dest[M];
int sz;
int used[N];
int curused;
inline void init(){
memset(linke, -1, sizeof(linke));
memset(head, -1, sizeof(head));
memset(used, -1, sizeof(used));
//fill(linke, linke + M, -1);
//fill(head, head + N, -1);
//fill(used, used + N, -1);
curused = 0;
sz = 0;
}
inline void addarc(const int frm, const int to, const int capacity){
dest[sz] = to;
linke[sz] = head[frm];
head[frm] = sz;
cap[sz] = capacity;
++sz;
}
inline void addedge(const int frm, const int to, const int capacity){
addarc(frm, to, capacity);
addarc(to, frm, 0);
}
int S, T;
int lvl[N];
inline int dfs(int v, int f){
if (v == T) return f;
int ans = 0;
for (int& arc = thead[v]; arc != -1 && f > 0; arc = linke[arc]){
int u = dest[arc];
if (cap[arc] && lvl[u] == lvl[v] + 1){
int curf = dfs(u, min(f, cap[arc]));
if (curf){
cap[arc] -= curf;
cap[arc ^ 1] += curf;
//return curf;
f -= curf;
ans += curf;
}
}
}
return ans;
}
const int INF = 2e9;
int q[N + 5];
inline bool bfs(const int mCap = 1){
int l = 0;
int r = 1;
memset(lvl, -1, sizeof(lvl));
lvl[S] = 0;
//from[S] = -1;
q[0] = S;
while (l != r){
int v = q[l++];
//int v = q.front(); q.pop();
for (int arc = head[v]; arc != -1; arc = linke[arc]){
int u = dest[arc];
if (cap[arc] >= mCap && lvl[u] == -1){
//from[u] = arc;
lvl[u] = lvl[v] + 1;
q[r++] = u;
}
}
}
return lvl[T] != -1;
}
int main(){
freopen(PATH"input.txt", "r", stdin);
//freopen("partition.in", "r", stdin);
//freopen("partition.out", "w", stdout);
//ios_base::sync_with_stdio(false);
init();
int n; scanf("%d\n", &n);
S = 0; T = n - 1;
for (int i = 0, t1, t2; i < n; ++i) scanf("%d%d", &t1, &t2);
int m; scanf("%d", &m);
for (int i = 0; i < m; ++i){
int a, b, c;
scanf("%d%d%d", &a, &b, &c);
//cin >> a >> b >> c;
--a; --b;
addarc(a, b, c);
addarc(b, a, c);
}
int sum = 0;
while (bfs()){
memcpy(thead, head, sizeof(head));
for (int t = dfs(S, INF); t != 0; t = dfs(S, INF)){
//++curused;
sum += t;
}
}
printf("%d\n", sum);
for (int arc = 0; arc < sz; arc += 2){
if (cap[arc] < cap[arc ^ 1]){
int to = dest[arc] + 1;
int fr = dest[arc ^ 1] + 1;
int f = (cap[arc ^ 1] - cap[arc]) / 2;
printf("%d %d %d\n", fr, to, f);
//cout << fr << ' ' << to << ' ' << f << '\n';
}
else
{
int to = dest[arc ^ 1] + 1;
int fr = dest[arc] + 1;
int f = (cap[arc] - cap[arc ^ 1]) / 2;
printf("%d %d %d\n", fr, to, f);
//cout << fr << ' ' << to << ' ' << f << '\n';
}
}
//cout.flush();
return 0;
}