forked from mpfeifer1/Kattis
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathavoidingtheapocalypse.cpp
More file actions
158 lines (138 loc) · 2.99 KB
/
avoidingtheapocalypse.cpp
File metadata and controls
158 lines (138 loc) · 2.99 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
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll inf = (ll)1 << 60;
const ll MAXN = 1000000 + 10;
struct edge {
ll a, b, cap, flow;
};
ll n, s, t, d[MAXN], ptr[MAXN], q[MAXN];
vector<edge> e;
vector<ll> g[MAXN];
void addedge(ll a, ll b, ll cap) {
edge e1 = { a, b, cap, 0 };
edge e2 = { b, a, 0, 0 };
g[a].push_back((ll) e.size());
e.push_back(e1);
g[b].push_back((ll) e.size());
e.push_back(e2);
}
bool bfs() {
ll qh=0, qt=0;
q[qt++] = s;
memset(d, -1, n * sizeof d[0]);
d[s] = 0;
while(qh < qt && d[t] == -1) {
ll v = q[qh++];
for(size_t i=0; i<g[v].size(); ++i) {
ll id = g[v][i],
to = e[id].b;
if(d[to] == -1 && e[id].flow < e[id].cap) {
q[qt++] = to;
d[to] = d[v] + 1;
}
}
}
return d[t] != -1;
}
ll dfs(ll v, ll flow) {
if(!flow) return 0;
if(v == t) return flow;
for(; ptr[v]<(ll)g[v].size(); ++ptr[v]) {
ll id = g[v][ptr[v]];
ll to = e[id].b;
if(d[to] != d[v] + 1) continue;
ll pushed = dfs(to, min (flow, e[id].cap - e[id].flow));
if(pushed) {
e[id].flow += pushed;
e[id^1].flow -= pushed;
return pushed;
}
}
return 0;
}
ll getflow() {
ll flow = 0;
for(;;) {
if(!bfs()) break;
memset(ptr, 0, n * sizeof ptr[0]);
while(ll pushed = dfs(s,inf)) {
flow += pushed;
}
}
return flow;
}
// This requres 'n' to be set to
// its value from the previous run
// This removes all edges from the graph
void reset() {
e.clear();
for(int i = 0; i < n; i++) {
g[i].clear();
}
}
struct road {
ll a, b, cap, t;
};
void solve() {
ll locations, start, group, time;
cin >> locations >> start >> group >> time;
start--;
time++;
ll ends;
cin >> ends;
vector<ll> loc(ends);
for(auto& i : loc) {
cin >> i;
i--;
}
ll r;
cin >> r;
vector<road> roads(r);
for(auto& i : roads) {
cin >> i.a >> i.b >> i.cap >> i.t;
i.a--;
i.b--;
}
::n = locations * time + 2;
::s = ::n-1;
::t = ::n-2;
// Add edge from source to first location
addedge(::s,start,group);
// Add edges from nodes to themselves so you can wait
for(int i = 1; i < time; i++) {
ll t1 = (i-1) * locations;
ll t2 = i * locations;
for(int j = 0; j < locations; j++) {
addedge(t1+j,t2+j,inf);
}
}
// Add each road at each timestep
for(auto& i : roads) {
for(ll j = 0; j < time; j++) {
if(j + i.t >= time) break;
ll t1 = j;
ll t2 = j + i.t;
ll n1 = t1*locations + i.a;
ll n2 = t2*locations + i.b;
addedge(n1,n2,i.cap);
}
}
// Add edge from each facility at each time to the sink
for(auto i : loc) {
for(ll j = 0; j < time; j++) {
ll n1 = j * locations + i;
addedge(n1, ::t, inf);
}
}
// Run flow
cout << getflow() << endl;
reset();
}
int main() {
ll cases;
cin >> cases;
while(cases--) {
solve();
}
}