-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path10330.cpp
More file actions
93 lines (82 loc) · 2.15 KB
/
10330.cpp
File metadata and controls
93 lines (82 loc) · 2.15 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
#include<bits/stdc++.h>
#define INF 2100000000
using namespace std;
bool visited[210];
int flowArray[210], pathArray[210];
int edge[210][210], baki[210][210];
int n;
int BFS(int s, int t)
{
memset(visited, 0 , sizeof(visited));
memset(pathArray, 0, sizeof(pathArray));
memset(flowArray, 0, sizeof(flowArray));
flowArray[s] = INF;
visited[s] = 1;
queue <int> Q;
Q.push(s);
int i, j;
while (!Q.empty())
{
i = Q.front();
Q.pop();
for (j = 1; j <= 2*n+1; j++)
{
if (!visited[j] && baki[i][j] > 0)
{
visited[j] = 1;
flowArray[j] = min(flowArray[i], baki[i][j]);
pathArray[j] = i;
Q.push(j);
if (j == t) return flowArray[t];
}
}
}
return flowArray[t];
}
int main()
{
//freopen("in.txt","r",stdin);
int m,a,b,d,capacity,i,j;
while (scanf("%d", &n) != EOF)
{
memset(edge, 0, sizeof(edge));
for (i = 1; i <= n; i++)
{
scanf("%d", &capacity);
edge[i][i+n] += capacity;
}
scanf("%d", &m);
for (i = 1; i <= m; i++)
{
scanf("%d %d %d", &a, &b, &capacity);
edge[a+n][b] += capacity;
}
scanf("%d %d", &b, &d);
for (i = 1; i <= b; i++)
{
scanf("%d", &a);
edge[0][a] = INF; //entry points set as infinity
}
for (i = 1; i <= d; i++)
{
scanf("%d", &a);
edge[a+n][2*n+1] = INF; // ending point to super sink set as infinity
edge[a+n][2*n+1] = INF; // ending point to super sink set as infinity
}
//printf("%d\n", maxflow(0, 2*n+1));
//edmond-karp
memcpy(baki, edge, sizeof(edge));
int flow=0, dif,s=0,t=2*n+1;
while(dif = BFS(s, t))
{
for (i = pathArray[t], j = t; i != j; i = pathArray[j = i])
{
baki[i][j] = baki[i][j]-dif;
baki[j][i] = baki[j][i]+dif;
}
flow =flow+ dif;
}
printf("%d\n", flow);
}
return 0;
}