-
Notifications
You must be signed in to change notification settings - Fork 3
/
congest.cpp
224 lines (203 loc) · 4.08 KB
/
congest.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
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
#include <bits/stdc++.h>
using namespace std;
#define hmap unordered_map
using uint = unsigned int;
using ll = long long;
using ull = unsigned long long;
struct edge;
struct node {
// Edges
vector<edge*> E;
// number of comuters
int c;
// total time
ull tt = -1;
// edge in the augmenting path
edge *e;
// flag for visited
bool f = false;
// index
int i;
};
struct edge {
node *src, *dest;
// time
ull t;
edge(node *src, node *dest, ull t) :
src(src),
dest(dest),
t(t) {}
};
struct node_cmp {
bool operator() (node *a, node *b) const {
if (a->tt != b->tt) return a->tt < b->tt;
return a < b;
}
};
vector<node> N;
void dijkstra(node *src) {
set<node*, node_cmp> Q;
src->tt = 0;
Q.insert(src);
while (!Q.empty()) {
auto itr = Q.begin();
auto cur = *itr;
Q.erase(itr);
for (auto e : cur->E) {
// new total time
ull ntt = e->t + cur->tt;
auto dest = e->dest;
if (ntt < dest->tt) {
itr = Q.find(dest);
if (itr != Q.end()) Q.erase(itr);
dest->tt = ntt;
Q.insert(dest);
}
}
}
}
struct flow_edge {
int flow;
node *src, *dest;
int rev;
flow_edge(node *src, node *dest, int flow) :
src(src), dest(dest), flow(flow) {}
};
struct flow_net {
// visited
vector<bool> V;
vector<vector<flow_edge>> M;
// parents
vector<pair<int, int>> P;
flow_net(size_t n) : M(n), V(n, false), P(n) {}
void connect(node *src, node *from, int flow) {
flow_edge a(src, from ,flow), b(from, src, 0);
b.rev = M[src->i].size();
a.rev = M[from->i].size();
M[src->i].push_back(a);
M[from->i].push_back(b);
}
void unmark() {
fill(V.begin(), V.end(), false);
}
void mark(node *n) {
V[n->i] = true;
}
bool marked(node *n) {
return V[n->i];
}
vector<flow_edge> E(node *n) {
return M[n->i];
}
};
flow_net build() {
flow_net res(N.size() + 1);
res.P.back() = make_pair(-1, -1);
queue<node*> Q;
Q.push(&N[0]);
N[0].f = true;
while (!Q.empty()) {
auto cur = Q.front();
Q.pop();
for (auto e : cur->E) {
auto dest = e->dest;
if (cur->tt + e->t == dest->tt) {
res.connect(dest, cur, 1);
}
if (!dest->f) {
dest->f = true;
Q.push(dest);
}
}
}
return res;
}
bool bfs(flow_net &fn, node *s, node *t) {
fn.unmark();
queue<node*> Q;
Q.push(s);
fn.mark(s);
while (!Q.empty()) {
auto cur = Q.front();
Q.pop();
auto E = fn.E(cur);
for (size_t i = 0; i < E.size(); ++i) {
auto dest = E[i].dest;
if (fn.marked(dest) || E[i].flow == 0) continue;
fn.P[dest->i] = make_pair(cur->i, i);
fn.mark(dest);
Q.push(dest);
}
}
return fn.marked(t);
}
int dfs(flow_net &fn, node *t) {
int res = N.size();
int cur = t->i;
while (true) {
int p = fn.P[cur].first;
int j = fn.P[cur].second;
if (p == -1) break;
auto &e = fn.M[p][j];
res = min(res, e.flow);
cur = p;
}
cur = t->i;
while (true) {
int p = fn.P[cur].first;
int j = fn.P[cur].second;
if (p == -1) break;
auto &e = fn.M[p][j];
e.flow -= res;
fn.M[cur][e.rev].flow += res;
cur = p;
}
return res;
}
int max_flow(flow_net fn, vector<node*> &vec) {
node *s = new node();
s->i = N.size();
node *t = &N[0];
for (auto n : vec) {
fn.connect(s, n, 1);
}
int res = 0;
while (bfs(fn, s, t)) {
res += dfs(fn, t);
}
delete s;
return res;
}
int main() {
int n, m, c;
cin >> n >> m >> c;
N = vector<node>(n);
for (int i = 0; i < n; ++i) N[i].i = i;
while (m--) {
int a, b, c;
cin >> a >> b >> c;
--a; --b;
N[a].E.push_back(new edge(&N[a], &N[b], c));
N[b].E.push_back(new edge(&N[b], &N[a], c));
}
while (c--) {
int k;
cin >> k;
--k;
N[k].c++;
}
dijkstra(&N[0]);
flow_net fn = build();
hmap<ull, vector<node*>> T;
for (int i = 0; i < n; ++i) {
for (int j = 0; j < N[i].c; ++j) {
T[N[i].tt].push_back(&N[i]);
}
}
int res = 0;
for (auto &entry : T) {
res += max_flow(fn, entry.second);
}
cout << res << endl;
return 0;
}