forked from ngthanhtrung23/CompetitiveProgramming
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathH.cpp
More file actions
159 lines (138 loc) · 4.87 KB
/
H.cpp
File metadata and controls
159 lines (138 loc) · 4.87 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
#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 DEBUG(X) { cout << #X << " = " << 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=ll>
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;
Cost cost;
int next;
Edge(int to, Flow cap, Cost cost, int next) :
to(to), cap(cap), cost(cost), next(next) {}
};
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) {
edges.push_back(Edge(to, cap, cost, last[from]));
last[from] = t++;
edges.push_back(Edge(from, 0, -cost, last[to]));
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;
}
};
const int MN = 111;
int x[MN];
int c[MN][MN];
int main() {
ios :: sync_with_stdio(0); cin.tie(0);
freopen("rentacar.in", "r", stdin);
freopen("rentacar.out", "w", stdout);
int n, m;
while (cin >> n >> m) {
memset(x, 0, sizeof x);
int s = 0, t = n + 1;
MinCostFlow<int,ll> flow(t+1);
int suma = 0;
FOR(i,1,n) {
int a, b; cin >> a >> b;
x[i] += a;
x[i] -= b;
if (x[i] > 0) suma += x[i];
if (x[i] > 0) flow.addEdge(s, i, x[i], 0);
else if (x[i] < 0) flow.addEdge(i, t, -x[i], 0);
}
FOR(i,1,n) FOR(j,1,n) c[i][j] = (i == j) ? 0 : 1e9;
while (m--) {
int u, v, cost; cin >> u >> v >> cost;
c[u][v] = min(c[u][v], cost);
c[v][u] = c[u][v];
}
FOR(k,1,n) FOR(i,1,n) FOR(j,1,n)
c[i][j] = min(c[i][j], c[i][k] + c[k][j]);
FOR(i,1,n) FOR(j,1,n)
if (i != j && c[i][j] < 1e9) {
flow.addEdge(i, j, 10111, c[i][j]);
}
auto p = flow.minCostFlow(s, t);
if (p.first != suma) cout << -1 << endl;
else cout << p.second << endl;
}
}