-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmaxFlow.cpp
More file actions
148 lines (143 loc) · 3.58 KB
/
Copy pathmaxFlow.cpp
File metadata and controls
148 lines (143 loc) · 3.58 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
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
// complexity O(V^2*E)
// problem: LightOj -> Power Transmission 1155
#include<bits/stdc++.h>
using namespace std;
#define FasterIO ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0)
typedef long long ll;
typedef pair<int,int> pi;
#define ff first
#define ss second
const int MX=100000;
const int inf=1e9;
struct DinicMaxflow
{
struct edge
{
int a, b, cap, flow ;
edge(int _a, int _b, int _c, int _d)
{
a = _a, b = _b, cap = _c, flow = _d;
}
} ;
int n, s, t, level [ MX ] , ptr [ MX ] ;
vector < edge > e ;
vector < int > g [ MX ] ;
queue<int>q;
void add_edge ( int a, int b, int cap )
{
edge e1 = edge( a, b, cap, 0 ) ;
edge e2 = edge( b, a, 0 , 0 ) ; // egde capacity will be cap for bidirectional edge
// if multiple edge occurs all edge should be considered
g [ a ] . push_back ( ( int ) e. size ( ) ) ;
e. push_back ( e1 ) ;
g [ b ] . push_back ( ( int ) e. size ( ) ) ;
e. push_back ( e2 ) ;
}
bool bfs ( )
{
memset ( level, -1 , sizeof level ) ;
level [ s ] = 0 ; q.push(s);
while( !q.empty() )
{
int u = q.front();
q.pop();
for(int i=0; i<g[u].size(); i++)
{
int id = g[u][i];
int v = e[id].b;
if ( level [ v ] == - 1 && e [ id ] . flow < e [ id ] . cap )
{
level[v] = level[u]+1;
q.push(v);
}
}
}
return level [ t ] != - 1 ;
}
int dfs ( int u, int flow )
{
if ( ! flow ) return 0 ;
if ( u == t ) return flow ;
for ( ; ptr [ u ] < ( int ) g [ u ] . size ( ) ; ++ ptr [ u ] )
{
int id = g [ u ] [ ptr [ u ] ] ;
int v = e [ id ] . b ;
if ( level [ v ] != level [ u ] + 1 ) continue ;
int mxflow = dfs ( v, min ( flow, e [ id ] . cap - e [ id ] . flow ) ) ;
if ( mxflow )
{
e [ id ] . flow += mxflow ;
e [ id ^ 1 ] . flow -= mxflow ;
return mxflow ;
}
}
return 0 ;
}
int MaxFlow ( )
{
int flow = 0 ;
while(bfs())
{
memset ( ptr, 0 , sizeof ptr ) ;
while ( int mxflow = dfs ( s, inf ) )
{
flow += mxflow ;
if(mxflow == 0)break;
}
}
return flow ;
}
void init( int _n, int _s, int _t )
{
n = _n; s = _s; t = _t;
for(int i=0; i<e.size(); i++) e[i].flow=0;
}
void RESET()
{
e.clear();
for(int i=0; i<=n; i++) g[i].clear();
}
} DMF;
int main()
{
FasterIO;
int tc,cs=1;
cin>>tc;
while(tc--)
{
int n,m;
cin>>n;
DMF.RESET();
DMF.init(2*n+2,0,2*n+1);
for(int i=1; i<=n; i++)
{
int cap;
cin>>cap;
DMF.add_edge(i,i+n,cap);
}
cin>>m;
for(int i=1; i<=m; i++)
{
int u,v,cap;
cin>>u>>v>>cap;
DMF.add_edge(u+n,v,cap);
}
int st,ed;
cin>>st>>ed;
for(int i=1; i<=st; i++)
{
int x;
cin>>x;
DMF.add_edge(0,x,inf);
}
for(int i=1; i<=ed; i++)
{
int x;
cin>>x;
DMF.add_edge(x+n,2*n+1,inf);
}
int ans=DMF.MaxFlow();
cout<<"Case "<<cs++<<": "<<ans<<"\n";
}
return 0;
}