-
Notifications
You must be signed in to change notification settings - Fork 174
/
Copy pathParcel Delivery.cpp
86 lines (76 loc) · 1.93 KB
/
Parcel Delivery.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
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxN = 501, maxM = 1001;
const ll INF = 0x3f3f3f3f3f3f3f3f;
int N, M, K, p[maxN];
ll d[maxN], cap[maxN][maxN], cost[maxN][maxN];
bool inq[maxN];
vector<int> G[maxN];
struct Edge {
int u, v;
ll r, c;
} edges[maxM], redges[maxM];
void bellman_ford(){
fill(inq+1, inq+N+1, false);
fill(d+1, d+N+1, INF);
fill(p+1, p+N+1, 0);
queue<int> Q;
Q.push(1);
d[1] = 0;
inq[1] = true;
while(!Q.empty()){
int u = Q.front(); Q.pop();
inq[u] = false;
for(int i : G[u]){
Edge e = (i < 0 ? redges[-i] : edges[i]);
if(e.r > 0 && d[e.v] > d[u] + e.c){
d[e.v] = d[u] + e.c;
p[e.v] = i;
if(!inq[e.v]){
inq[e.v] = true;
Q.push(e.v);
}
}
}
}
}
ll minimum_cost_flow(){
ll flow = 0, cost = 0;
while(flow < K){
bellman_ford();
if(d[N] == INF) break;
ll aug = K-flow;
int u = N;
while(u != 1){
Edge e = (p[u] < 0 ? redges[-p[u]] : edges[p[u]]);
aug = min(aug, e.r);
u = e.u;
}
flow += aug;
cost += aug * d[N];
u = N;
while(u != 1){
if(p[u] < 0){
redges[-p[u]].r -= aug;
edges[-p[u]].r += aug;
} else {
redges[p[u]].r += aug;
edges[p[u]].r -= aug;
}
u = (p[u] < 0 ? redges[-p[u]].u : edges[p[u]].u);
}
}
return (flow < K ? -1 : cost);
}
int main(){
scanf("%d %d %d", &N, &M, &K);
for(int i = 1, u, v, r, c; i <= M; i++){
scanf("%d %d %d %d", &u, &v, &r, &c);
G[u].push_back(i);
G[v].push_back(-i);
edges[i] = {u, v, r, c};
redges[i] = {v, u, 0, -c};
}
printf("%lld\n", minimum_cost_flow());
}