Skip to content

Commit 47aa203

Browse files
committed
I am back: Internet bandwidth
1 parent 3958155 commit 47aa203

File tree

1 file changed

+129
-0
lines changed

1 file changed

+129
-0
lines changed

Diff for: 1153-Internet-Bandwidth.cpp

+129
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
#include <iostream>
2+
#include <stdio.h>
3+
#include <string.h>
4+
#include <vector>
5+
#include <stack>
6+
#include <limits.h>
7+
using namespace std;
8+
9+
int s;
10+
int t;
11+
int n;
12+
13+
vector < vector < pair <int, int> > > g;
14+
int a[110][110];
15+
int parent[110];
16+
17+
int dfs()
18+
{
19+
20+
int vis[110];
21+
int top;
22+
int x;
23+
24+
memset(vis, 0, sizeof vis);
25+
26+
stack <int> q;
27+
q.push(s);
28+
vis[s] = 1;
29+
30+
31+
while(!q.empty()) {
32+
top = q.top();
33+
q.pop();
34+
for (int i = 0; i < g[top].size(); i++) {
35+
x = g[top][i].first;
36+
if(a[top][x] > 0 and vis[x] == 0) {
37+
q.push(x);
38+
vis[x] = 1;
39+
parent[x] = top;
40+
}
41+
42+
}
43+
44+
45+
}
46+
return vis[t] == 1;
47+
48+
49+
50+
}
51+
52+
53+
int main()
54+
{
55+
56+
int x;
57+
int y;
58+
int w;
59+
int CS;
60+
int flow;
61+
int k;
62+
int c;
63+
int p;
64+
int ans;
65+
66+
67+
68+
69+
scanf("%d", &CS);
70+
71+
for (int cs = 1; cs <= CS; cs++) {
72+
scanf("%d", &n);
73+
scanf("%d", &s);
74+
scanf("%d", &t);
75+
scanf("%d", &c);
76+
77+
memset(a, 0, sizeof(a));
78+
vector < vector < pair <int, int> > > tg(n+2);
79+
80+
swap(tg, g);
81+
ans = 0;
82+
83+
for (int i = 0; i < c; i++) {
84+
scanf("%d", &x);
85+
scanf("%d", &y);
86+
scanf("%d", &w);
87+
88+
a[x][y] += w;
89+
a[y][x] += w;
90+
g[x].push_back(make_pair(y, w));
91+
g[y].push_back(make_pair(x, w));
92+
}
93+
94+
95+
while(dfs()) {
96+
k = t;
97+
98+
flow = INT_MAX;
99+
100+
while(k != s) {
101+
p = parent[k];
102+
flow = min(flow, a[k][p]);
103+
k = parent[k];
104+
105+
106+
}
107+
108+
109+
k = t;
110+
111+
112+
while(k != s) {
113+
p = parent[k];
114+
a[k][p] -= flow;
115+
a[p][k] -= flow;
116+
k = parent[k];
117+
}
118+
119+
ans += flow;
120+
}
121+
122+
printf("Case %d: %d\n", cs, ans);
123+
}
124+
125+
126+
}
127+
128+
129+

0 commit comments

Comments
 (0)