poj 3308 Paratroopers(最小點權覆蓋)
Paratroopers
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 8954 | Accepted: 2702 |
Description
It is year 2500 A.D. and there is a terrible war between the forces of the Earth and the Mars. Recently, the commanders of the Earth are informed by their spies that the invaders of Mars want to land some paratroopers in the m
In order to accomplish this task, the defense force wants to utilize some of their most hi-tech laser guns. They can install a gun on a row (resp. column) and by firing this gun all paratroopers landed in this row (resp. column) will die. The cost of installing a gun in the ith row (resp. column) of the grid yard is ri
Input
Input begins with a number T showing the number of test cases and then, T test cases follow. Each test case begins with a line containing three integers 1 ≤ m ≤ 50 , 1 ≤ n ≤ 50 and 1 ≤ l ≤ 500 showing the number of rows and columns of the yard and the number of paratroopers respectively. After that, a line with m positive real numbers greater or equal to 1.0 comes where the ith number is ri and then, a line with n positive real numbers greater or equal to 1.0 comes where the ith number is ci. Finally, l lines come each containing the row and column of a paratrooper.
Output
For each test case, your program must output the minimum total cost of constructing the firing system rounded to four digits after the fraction point.
Sample Input
1 4 4 5 2.0 7.0 5.0 2.0 1.5 2.0 2.0 8.0 1 1 2 2 3 3 4 4 1 4
Sample Output
16.0000
二分圖的最小點權覆蓋。
code
1 #include<cstdio> 2 #include<algorithm> 3 #include<cstring> 4 #include<cmath> 5 6 using namespace std; 7 8 const int N = 5010; 9 const double INF = 1000000000.0; 10 const double eps = 1e-10; 11 struct Edge{ 12 int to,nxt;double c; 13 Edge() {} 14 Edge(int x,double y,int z) {to = x,c = y,nxt = z;} 15 }e[100100]; 16 int q[100100],L,R,S,T,tot = 1; 17 int dis[N],cur[N],head[N]; 18 19 void add_edge(int u,int v,double c) { 20 e[++tot] = Edge(v,c,head[u]);head[u] = tot; 21 e[++tot] = Edge(u,0,head[v]);head[v] = tot; 22 } 23 bool bfs() { 24 for (int i=1; i<=T; ++i) cur[i] = head[i],dis[i] = -1; 25 L = 1,R = 0; 26 q[++R] = S;dis[S] = 0; 27 while (L <= R) { 28 int u = q[L++]; 29 for (int i=head[u]; i; i=e[i].nxt) { 30 int v = e[i].to; 31 if (dis[v] == -1 && e[i].c > eps) { 32 dis[v] = dis[u]+1;q[++R] = v; 33 if (v==T) return true; 34 } 35 } 36 } 37 return false; 38 } 39 double dfs(int u,double flow) { 40 if (u==T) return flow; 41 double used = 0; 42 for (int &i=cur[u]; i; i=e[i].nxt) { 43 int v = e[i].to; 44 if (dis[v] == dis[u] + 1 && e[i].c > eps) { 45 double tmp = dfs(v,min(flow-used,e[i].c)); 46 if (tmp > eps) { 47 e[i].c -= tmp;e[i^1].c += tmp; 48 used += tmp; 49 if (used == flow) break; 50 } 51 } 52 } 53 if (used != flow) dis[u] = -1; 54 return used; 55 } 56 double dinic() { 57 double ret = 0.0; 58 while (bfs()) ret += dfs(S,INF); 59 return ret; 60 } 61 void Clear() { 62 tot = 1; 63 memset(head,0,sizeof(head)); 64 } 65 int main() { 66 int Case,n,m,E,u,v;double x; 67 scanf("%d",&Case); 68 while (Case--) { //-不要設T 69 Clear(); 70 scanf("%d%d%d",&n,&m,&E); 71 S = n+m+1;T = n+m+2; 72 for (int i=1; i<=n; ++i) { 73 scanf("%lf",&x); 74 add_edge(S,i,log(x)); 75 } 76 for (int i=1; i<=m; ++i) { 77 scanf("%lf",&x); 78 add_edge(i+n,T,log(x)); 79 } 80 for (int i=1; i<=E; ++i) { 81 scanf("%d%d",&u,&v); 82 add_edge(u,v+n,INF); 83 } 84 double ans = dinic(); 85 printf("%.4lf\n",exp(ans)); 86 } 87 return 0; 88 }
poj 3308 Paratroopers(最小點權覆蓋)