-
Notifications
You must be signed in to change notification settings - Fork 0
/
1155.cpp
121 lines (100 loc) · 2.04 KB
/
1155.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
/*
Date : 23 Jan, 2014
Problem : Light OJ 1155 Power Transmission
*/
#include <iostream>
#include <string.h>
#include <stdio.h>
#include <queue>
#define inf 1000006
#define sz 210
using namespace std;
int source, sink,par[sz], c[sz][sz], N;
bool vis[sz];
int BFS()
{
int pathcap, tot, i, cur, now;
queue<int> q;
pathcap = inf;
memset(vis, false, sizeof(vis));
memset(par, -1, sizeof(par));
vis[source] = true;
q.push(source);
tot = 2*N + 2;
while(!q.empty())
{
cur = q.front(); q.pop();
if(cur== sink) break;
for(i = 0; i <tot; i++){
if(vis[i] || c[cur][i]<= 0) continue;
par[i] = cur;
vis[i] = true;
q.push(i);
}
}
int prv;
now = sink;
while(par[now] > -1){
prv = par[now];
pathcap = min(pathcap, c[prv][now]);
now = prv;
}
now = sink;
while(par[now] > -1){
prv = par[now];
c[prv][now] -= pathcap;
c[now][prv] += pathcap;
now = prv;
}
if(pathcap== inf) pathcap = 0;
return pathcap;
}
int max_flow()
{
int ans,flow;
ans = 0;
while(1){
flow = BFS();
if(flow == 0) break;
ans+= flow;
}
return ans;
}
int main()
{
int t, tc, i, a,b,cap;
scanf("%d",&t);
tc = 0;
while(tc<t){
tc++;
memset(c, 0, sizeof(c));
cin>>N;
for(i = 0; i < N; i++){
cin>>a;
c[i][i+N] +=a;
}
int M,B,D;
cin>>M;
for(i = 0; i < M; i++){
cin>>a>>b>>cap;
a--; b--;
c[a+N][b] += cap;
}
cin>> B>> D;
source = 2*N;
sink = 2*N+1;
for(i =0; i < B; i++){
cin>>a;
a--;
c[source][a] += inf;
}
for(i = 0; i < D; i++){
cin>>a;
a--;
c[a+N][sink] += inf;
}
int ans = max_flow();
printf("Case %d: %d\n",tc,ans);
}
return 0;
}