forked from marioyc/Online-Judge-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path3308 - Paratroopers.cpp
120 lines (93 loc) · 2.65 KB
/
3308 - Paratroopers.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
#include <cstdio>
#include <climits>
#include <cmath>
#include <algorithm>
using namespace std;
struct flow_graph{
int MAX_V,E,s,t,head,tail;
int *to,*next,*last,*dist,*q,*now;
double *cap;
flow_graph(){}
flow_graph(int V, int MAX_E){
MAX_V = V; E = 0;
cap = new double[2*MAX_E], to = new int[2*MAX_E], next = new int[2*MAX_E];
last = new int[MAX_V], q = new int[MAX_V];
dist = new int[MAX_V], now = new int[MAX_V];
fill(last,last+MAX_V,-1);
}
void clear(){
fill(last,last+MAX_V,-1);
E = 0;
}
void add_edge(int u, int v, double uv, double vu = 0){
to[E] = v, cap[E] = uv, next[E] = last[u]; last[u] = E++;
to[E] = u, cap[E] = vu, next[E] = last[v]; last[v] = E++;
}
bool bfs(){
fill(dist,dist+MAX_V,-1);
head = tail = 0;
q[tail] = t; ++tail;
dist[t] = 0;
while(head<tail){
int v = q[head]; ++head;
for(int e = last[v];e!=-1;e = next[e]){
if(cap[e^1]>1e-7 && dist[to[e]]==-1){
q[tail] = to[e]; ++tail;
dist[to[e]] = dist[v]+1;
}
}
}
return dist[s]!=-1;
}
double dfs(int v, double f){
if(v==t) return f;
for(int &e = now[v];e!=-1;e = next[e]){
if(cap[e]>1e-7 && dist[to[e]]==dist[v]-1){
double ret = dfs(to[e],min(f,cap[e]));
if(ret>1e-7){
cap[e] -= ret;
cap[e^1] += ret;
return ret;
}
}
}
return 0;
}
double max_flow(int source, int sink){
s = source; t = sink;
double f = 0,df;
while(bfs()){
for(int i = 0;i<MAX_V;++i) now[i] = last[i];
while(true){
df = dfs(s,10);
if(df<1e-7) break;
f += df;
}
}
return f;
}
}G(102,600);
int main(){
int T,R,C,N;
double x;
scanf("%d",&T);
while(T--){
scanf("%d %d %d",&R,&C,&N);
G.clear();
for(int i = 0;i<R;++i){
scanf("%lf",&x);
G.add_edge(0,1+i,log(x));
}
for(int i = 0;i<C;++i){
scanf("%lf",&x);
G.add_edge(1+R+i,1+R+C,log(x));
}
for(int i = 0,r,c;i<N;++i){
scanf("%d %d",&r,&c);
--r; --c;
G.add_edge(1+r,1+R+c,10);
}
printf("%.4f\n",exp(G.max_flow(0,1+R+C)));
}
return 0;
}