-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathDoomsday.cpp
executable file
·116 lines (108 loc) · 2.15 KB
/
Doomsday.cpp
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
#include <bits/stdc++.h>
using namespace std;
template <typename T>
struct Dinic {
struct Edge {
int to;
T cap;
Edge(int a, T b): to(a), cap(b) {}
};
int n;
vector<vector<int>> adj;
vector<Edge> edges;
vector<int> level, on;
Dinic(int a = 0) {
n = a;
adj.resize(a + 2);
}
void init(int a) {
adj.clear();
edges.clear();
adj.resize(a + 1);
n = a;
}
void add(int from, int to, T cap) {
adj[from].push_back(int(edges.size()));
edges.push_back(Edge(to, cap));
adj[to].push_back(int(edges.size()));
edges.push_back(Edge(from, 0));
}
bool bfs(int sink, int src) {
level = vector<int>(n + 2, 0);
queue<int> q;
level[sink] = 0;
q.push(sink);
while(!q.empty()) {
int cur = q.front();
q.pop();
for(int id : adj[cur]) {
if (edges[id].cap == 0) continue;
int to = edges[id].to;
if (level[to] == 0 && to != sink) {
q.push(to);
level[to] = level[cur] + 1;
}
}
}
//for(int i = 0; i <= src; i++) cout << i << ' ' << level[i] << '\n';
return level[src] > 0;
}
T dfs(int v, int src, T cur) {
if (v == src) return cur;
for(; on[v] < adj[v].size(); on[v]++) {
int id = adj[v][on[v]];
if (level[v] + 1 != level[edges[id].to]) {
continue;
}
if (edges[id].cap > 0) {
T temp = dfs(edges[id].to, src, min(cur, edges[id].cap));
if (temp > 0) {
edges[id].cap -= temp;
edges[id ^ 1].cap += temp;
return temp;
}
}
}
return 0;
}
T max_flow(int sink, int src) {
T ans = 0;
while(bfs(sink, src)) {
on = vector<int>(n + 2, 0);
T cur;
while(cur = dfs(sink, src, 1e9)) {
ans += cur;
}
}
return ans;
}
};
int main() {
ios::sync_with_stdio(0); cin.tie(0);
int n, e, x;
cin >> n >> e >> x;
Dinic<int> graph(n + 1);
int sum = 0;
for(int i = 0; i < n; i++) {
int y;
cin >> y;
sum += y;
graph.add(0, i + 1, y);
}
for(int i = 0; i < n; i++) {
int y;
cin >> y;
graph.add(i + 1, n + 1, y);
}
for(int i = 0; i < e; i++) {
int a, b, c;
cin >> a >> b >> c;
if (c <= x) {
//cout << a << ' ' << b << '\n';
graph.add(a, b, 1e9);
graph.add(b, a, 1e9);
}
}
cout << sum - graph.max_flow(0, n + 1) << '\n';
return 0;
}