forked from sitz/UVa-Online-Judge
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path10330.cpp
115 lines (109 loc) · 1.91 KB
/
10330.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
#include <bits/stdc++.h>
using namespace std;
#define WHITE 0
#define GRAY 1
#define BLACK 2
#define MAX_NODES 202
#define oo 1 << 30
int cap[MAX_NODES][MAX_NODES];
int flow[MAX_NODES][MAX_NODES];
int color[MAX_NODES];
int pred[MAX_NODES];
int b, d, head, i, increment, j, m, n, tail, x, y, z, lim_sup;
int q[MAX_NODES];
void enqueue(int x)
{
q[tail] = x;
tail++;
color[x] = GRAY;
}
int dequeue()
{
int x = q[head];
head++;
color[x] = BLACK;
return x;
}
int bfs(int start, int target)
{
for (i = 0; i < lim_sup; i++)
{
color[i] = WHITE;
}
head = tail = 0;
enqueue(start);
pred[start] = -1;
while (head != tail)
{
i = dequeue();
for (j = 0; j < lim_sup; j++)
{
if (color[j] == WHITE && cap[i][j] - flow[i][j] > 0)
{
enqueue(j);
pred[j] = i;
}
}
}
return color[target] == BLACK;
}
int min(int x, int y)
{
return x < y ? x : y;
}
int max_flow(int source, int sink)
{
int max_flow = 0;
for (i = 0; i < lim_sup; i++)
for (j = 0; j < lim_sup; j++)
{
flow[i][j] = 0;
}
while (bfs(source, sink))
{
increment = oo;
for (i = lim_sup - 1; pred[i] != -1; i = pred[i])
{
increment = min(increment, cap[pred[i]][i] - flow[pred[i]][i]);
}
for (i = lim_sup - 1; pred[i] != -1; i = pred[i])
{
flow[pred[i]][i] += increment;
flow[i][pred[i]] -= increment;
}
max_flow += increment;
}
return max_flow;
}
int main()
{
while (scanf("%d", &n) != EOF)
{
lim_sup = (n + 1) << 1;
for (i = 0; i < lim_sup; i++)
{
for (j = 0; j < lim_sup; j++)
{
cap[i][j] = 0;
}
}
for (i = 1; i <= n; scanf("%d", &cap[i][n + i]), i++)
;
scanf("%d", &m);
for (i = 0; i < m; cap[n + x][y] = z, i++)
{
scanf("%d %d %d", &x, &y, &z);
}
scanf("%d %d", &b, &d);
for (i = 0; i < b; cap[0][x] = oo, i++)
{
scanf("%d", &x);
}
for (i = 0; i < d; cap[n + x][2 * n + 1] = oo, i++)
{
scanf("%d", &x);
}
printf("%d\n", max_flow(0, 2 * n + 1));
}
return 0;
}