-
Notifications
You must be signed in to change notification settings - Fork 34
/
1153-Internet-Bandwidth.cpp
129 lines (86 loc) · 1.46 KB
/
1153-Internet-Bandwidth.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
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <vector>
#include <stack>
#include <limits.h>
using namespace std;
int s;
int t;
int n;
vector < vector < pair <int, int> > > g;
int a[110][110];
int parent[110];
int dfs()
{
int vis[110];
int top;
int x;
memset(vis, 0, sizeof vis);
stack <int> q;
q.push(s);
vis[s] = 1;
while(!q.empty()) {
top = q.top();
q.pop();
for (int i = 0; i < g[top].size(); i++) {
x = g[top][i].first;
if(a[top][x] > 0 and vis[x] == 0) {
q.push(x);
vis[x] = 1;
parent[x] = top;
}
}
}
return vis[t] == 1;
}
int main()
{
int x;
int y;
int w;
int CS;
int flow;
int k;
int c;
int p;
int ans;
scanf("%d", &CS);
for (int cs = 1; cs <= CS; cs++) {
scanf("%d", &n);
scanf("%d", &s);
scanf("%d", &t);
scanf("%d", &c);
memset(a, 0, sizeof(a));
vector < vector < pair <int, int> > > tg(n+2);
swap(tg, g);
ans = 0;
for (int i = 0; i < c; i++) {
scanf("%d", &x);
scanf("%d", &y);
scanf("%d", &w);
a[x][y] += w;
a[y][x] += w;
g[x].push_back(make_pair(y, w));
g[y].push_back(make_pair(x, w));
}
while(dfs()) {
k = t;
flow = INT_MAX;
while(k != s) {
p = parent[k];
flow = min(flow, a[k][p]);
k = parent[k];
}
k = t;
while(k != s) {
p = parent[k];
a[k][p] -= flow;
a[p][k] -= flow;
k = parent[k];
}
ans += flow;
}
printf("Case %d: %d\n", cs, ans);
}
}