forked from ngthanhtrung23/CompetitiveProgramming
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathD.cpp
More file actions
165 lines (143 loc) · 4.95 KB
/
D.cpp
File metadata and controls
165 lines (143 loc) · 4.95 KB
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
#include <bits/stdc++.h>
#define FOR(i,a,b) for(int i=(a),_b=(b); i<=_b; i++)
#define FORD(i,a,b) for(int i=(a),_b=(b); i>=_b; i--)
#define REP(i,a) for(int i=0,_a=(a); i<_a; i++)
#define EACH(it,a) for(__typeof(a.begin()) it = a.begin(); it != a.end(); ++it)
#define DEBUG(x) { cout << #x << " = "; cout << (x) << endl; }
#define PR(a,n) { cout << #a << " = "; FOR(_,1,n) cout << a[_] << ' '; cout << endl; }
#define PR0(a,n) { cout << #a << " = "; REP(_,n) cout << a[_] << ' '; cout << endl; }
#define sqr(x) ((x) * (x))
#define ll long long
#define SZ(X) ((int) ((X).size()))
using namespace std;
template<class Flow=int, class Cost=int>
struct MinCostFlow {
const Flow INF_FLOW = 1000111000;
const Cost INF_COST = 1000111000111000LL;
int n, t, S, T;
Flow totalFlow;
Cost totalCost;
vector<int> last, visited;
vector<Cost> dis;
struct Edge {
int to;
Flow cap, orig_cap;
Cost cost;
int next;
int id;
Edge(int to, Flow cap, Cost cost, int next, int id) :
to(to), cap(cap), orig_cap(cap), cost(cost), next(next), id(id) {}
};
vector<Edge> edges;
MinCostFlow(int n) : n(n), t(0), totalFlow(0), totalCost(0), last(n, -1), visited(n, 0), dis(n, 0) {
edges.clear();
}
int addEdge(int from, int to, Flow cap, Cost cost, int id) {
edges.push_back(Edge(to, cap, cost, last[from], id));
last[from] = t++;
edges.push_back(Edge(from, 0, -cost, last[to], id));
last[to] = t++;
return t - 2;
}
pair<Flow, Cost> minCostFlow(int _S, int _T) {
S = _S; T = _T;
SPFA();
while (1) {
while (1) {
REP(i,n) visited[i] = 0;
if (!findFlow(S, INF_FLOW)) break;
}
if (!modifyLabel()) break;
}
return make_pair(totalFlow, totalCost);
}
private:
void SPFA() {
REP(i,n) dis[i] = INF_COST;
priority_queue< pair<Cost,int> > Q;
Q.push(make_pair(dis[S]=0, S));
while (!Q.empty()) {
int x = Q.top().second;
Cost d = -Q.top().first;
Q.pop();
// For double: dis[x] > d + EPS
if (dis[x] != d) continue;
for(int it = last[x]; it >= 0; it = edges[it].next)
if (edges[it].cap > 0 && dis[edges[it].to] > d + edges[it].cost)
Q.push(make_pair(-(dis[edges[it].to] = d + edges[it].cost), edges[it].to));
}
REP(i,n) dis[i] = dis[T] - dis[i];
}
Flow findFlow(int x, Flow flow) {
if (x == T) {
totalCost += dis[S] * flow;
totalFlow += flow;
return flow;
}
visited[x] = 1;
Flow now = flow;
for(int it = last[x]; it >= 0; it = edges[it].next)
// For double: fabs(dis[edges[it].to] + edges[it].cost - dis[x]) < EPS
if (edges[it].cap && !visited[edges[it].to] && dis[edges[it].to] + edges[it].cost == dis[x]) {
Flow tmp = findFlow(edges[it].to, min(now, edges[it].cap));
edges[it].cap -= tmp;
edges[it ^ 1].cap += tmp;
now -= tmp;
if (!now) break;
}
return flow - now;
}
bool modifyLabel() {
Cost d = INF_COST;
REP(i,n) if (visited[i])
for(int it = last[i]; it >= 0; it = edges[it].next)
if (edges[it].cap && !visited[edges[it].to])
d = min(d, dis[edges[it].to] + edges[it].cost - dis[i]);
// For double: if (d > INF_COST / 10) INF_COST = 1e20
if (d == INF_COST) return false;
REP(i,n) if (visited[i])
dis[i] += d;
return true;
}
};
int f[1011], res[1011];
int main() {
ios :: sync_with_stdio(false);
freopen("gas.in", "r", stdin);
freopen("gas.out","w", stdout);
int n, m;
while (cin >> n >> m) {
memset(f, 0, sizeof f);
MinCostFlow<int,ll> flow(n+2);
int need = 0;
FOR(i,1,m) {
int u, v, lb; cin >> u >> v >> lb;
f[u] -= lb;
f[v] += lb;
flow.addEdge(u, v, 1000111, 1, i);
res[i] = lb;
}
FOR(i,1,n) {
if (f[i] > 0) {
flow.addEdge(0, i, f[i], 0, 0);
need += f[i];
}
else flow.addEdge(i, n+1, -f[i], 0, 0);
}
auto x = flow.minCostFlow(0, n+1);
if (x.first < need) {
cout << -1 << endl;
continue;
}
for(auto e : flow.edges) {
int id = e.id;
int fl = e.orig_cap - e.cap;
if (fl <= 0) continue;
res[id] += fl;
}
int t = 0;
FOR(i,1,m) t += res[i];
cout << t << endl;
FOR(i,1,m) printf("%d\n", res[i]);
}
}