forked from mpfeifer1/Kattis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathragingriver.cpp
150 lines (125 loc) · 3.68 KB
/
ragingriver.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
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll inf = (ll)1 << 60;
struct MinCostMaxFlow {
vector<vector<ll>> cap, flow, cost;
vector<ll> dad, pi;
ll n;
ll search(ll source, ll sink) {
vector<bool> found(n+1, false);
vector<ll> dist(n+1, inf);
/*
Arrays.fill(found, false);
Arrays.fill(dist, inf);
*/
dist[source] = 0;
while (source != n) {
ll best = n;
found[source] = true;
for (ll k = 0; k < n; k++) {
if (found[k]) continue;
if (flow[k][source] != 0) {
ll val = dist[source] + pi[source] - pi[k] - cost[k][source];
if (dist[k] > val) {
dist[k] = val;
dad[k] = source;
}
}
if (flow[source][k] < cap[source][k]) {
ll val = dist[source] + pi[source] - pi[k] + cost[source][k];
if (dist[k] > val) {
dist[k] = val;
dad[k] = source;
}
}
if (dist[k] < dist[best]) best = k;
}
source = best;
}
for (ll k = 0; k < n; k++) {
pi[k] = min(pi[k] + dist[k], inf);
}
return found[sink];
}
pair<ll,ll> getflow(vector<vector<ll>> param_cap, vector<vector<ll>> param_cost, ll source, ll sink) {
cap.clear();
cost.clear();
flow.clear();
dad.clear();
pi.clear();
n = param_cap.size();
cap.resize(n, vector<ll>(n));
cost.resize(n, vector<ll>(n));
flow.resize(n, vector<ll>(n,0));
for(ll i = 0; i < n; i++) {
for(ll j = 0; j < n; j++) {
cap[i][j] = param_cap[i][j];
cost[i][j] = param_cost[i][j];
}
}
dad.resize(n,0);
pi.resize(n,0);
ll totflow = 0, totcost = 0;
while(search(source, sink) > 0) {
ll amt = inf;
for(ll x = sink; x != source; x = dad[x])
amt = min(amt, flow[x][dad[x]] != 0 ? flow[x][dad[x]] :
cap[dad[x]][x] - flow[dad[x]][x]);
for(ll x = sink; x != source; x = dad[x]) {
if(flow[x][dad[x]] != 0) {
flow[x][dad[x]] -= amt;
totcost -= amt * cost[x][dad[x]];
}
else {
flow[dad[x]][x] += amt;
totcost += amt * cost[dad[x]][x];
}
}
totflow += amt;
}
return {totflow, totcost};
}
};
// 0 is source
// 1 is left bank
// 2 - r+1 are rocks
// r+2 is right bank
// r+3 is sink
ll fix(ll n, ll r) {
if(n == -2) return 1;
if(n == -1) return r+2;
return n+2;
}
int main(){
ll p, r, l;
cin >> p >> r >> l;
MinCostMaxFlow flow;
vector<vector<ll>> cap, cost;
cap.resize(r+4, vector<ll>(r+4, 0));
cost.resize(r+4, vector<ll>(r+4, 0));
// 0 is source
// 1 is left bank
// 2 - r+1 are rocks
// r+2 is right bank
// r+3 is sink
cap[0][1] = p;
cap[r+2][r+3] = p;
for(int i = 0; i < l; i++) {
int n1, n2;
cin >> n1 >> n2;
n1 = fix(n1,r);
n2 = fix(n2,r);
cap[n1][n2] = 1;
cap[n2][n1] = 1;
cost[n1][n2] = 1;
cost[n2][n1] = 1;
}
pair<ll,ll> p1 = flow.getflow(cap, cost, 0, r+3);
if(p1.first < p) {
cout << p - p1.first << " people left behind" << endl;
}
else {
cout << p1.second << endl;
}
}