标签:des style blog http color io os java ar
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 × n grid yard of one their main weapon factories in order to destroy it. In addition, the spies informed them the row and column of the places in the yard in which each paratrooper will land. Since the paratroopers are very strong and well-organized, even one of them, if survived, can complete the mission and destroy the whole factory. As a result, the defense force of the Earth must kill all of them simultaneously after their landing.
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 (resp. ci ) and the total cost of constructing a system firing all guns simultaneously is equal to the product of their costs. Now, your team as a high rank defense group must select the guns that can kill all paratroopers and yield minimum total cost of constructing the firing system.
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 npositive 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.
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.
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
16.0000
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <cmath> 5 #include <algorithm> 6 #include <climits> 7 #include <vector> 8 #include <queue> 9 #include <cstdlib> 10 #include <string> 11 #include <set> 12 #include <stack> 13 #define LL long long 14 #define pii pair<int,int> 15 using namespace std; 16 const int maxn = 120; 17 const double INF = 10000.0; 18 struct arc { 19 int to,next; 20 double flow; 21 arc(int x = 0,double y = 0.0,int z = -1) { 22 to = x; 23 flow = y; 24 next = z; 25 } 26 }; 27 arc e[maxn*maxn]; 28 int head[maxn],d[maxn],tot,cur[maxn],S,T; 29 int n,m,k; 30 void add(int u,int v,double flow) { 31 e[tot] = arc(v,flow,head[u]); 32 head[u] = tot++; 33 e[tot] = arc(u,0.0,head[v]); 34 head[v] = tot++; 35 } 36 bool bfs() { 37 memset(d,-1,sizeof(d)); 38 queue<int>q; 39 d[S] = 1; 40 q.push(S); 41 while(!q.empty()) { 42 int u = q.front(); 43 q.pop(); 44 for(int i = head[u]; ~i; i = e[i].next) { 45 if(e[i].flow > 0 && d[e[i].to] == -1) { 46 d[e[i].to] = d[u] + 1; 47 q.push(e[i].to); 48 } 49 } 50 } 51 return d[T] > -1; 52 } 53 double dfs(int u,double low) { 54 double tmp = 0,a; 55 if(u == T) return low; 56 for(int &i = cur[u]; ~i; i = e[i].next) { 57 if(e[i].flow > 0 && d[e[i].to] == d[u] + 1 && (a = dfs(e[i].to,min(e[i].flow,low)))) { 58 tmp += a; 59 low -= a; 60 e[i].flow -= a; 61 e[i^1].flow += a; 62 if(low <= 0) break; 63 } 64 } 65 if(tmp <= 0) d[u] = -1; 66 return tmp; 67 } 68 int main() { 69 double cap,ans; 70 int x,y,t; 71 scanf("%d",&t); 72 while(t--) { 73 scanf("%d %d %d",&n,&m,&k); 74 memset(head,-1,sizeof(head)); 75 S = ans = tot = 0; 76 T = n + m + 1; 77 for(int i = 1; i <= n; i++) { 78 scanf("%lf",&cap); 79 add(S,i,log(cap)); 80 } 81 for(int i = 1; i <= m; i++) { 82 scanf("%lf",&cap); 83 add(i+n,T,log(cap)); 84 } 85 for(int i = 0; i < k; i++) { 86 scanf("%d %d",&x,&y); 87 add(x,y+n,INF); 88 } 89 ans = 0.0; 90 while(bfs()) { 91 memcpy(cur,head,sizeof(head)); 92 ans += dfs(S,INF); 93 } 94 printf("%.4f\n",exp(ans)); 95 } 96 return 0; 97 }
标签:des style blog http color io os java ar
原文地址:http://www.cnblogs.com/crackpotisback/p/3992612.html