Paratrooperstime limit: 1000 msmemory limit: 65536 kbthis problem will be judged on PKU. Original ID: 3308
64-bit integer Io format: % LLD Java class name: Main
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 inM×NGrid 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 your ved, 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 inITh row (resp. Column) of the grid yard isRi(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
Input begins with a numberTShowing the number of test cases and then,TTest 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 lineMPositive real numbers greater or equal to 1.0 comes whereITh number isRiAnd then, a lineNPositive real numbers greater or equal to 1.0 comes whereITh number isCi. Finally,LLines Come each containing the row and column of A paratro.
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
14 4 52.0 7.0 5.0 2.01.5 2.0 2.0 8.01 12 23 34 41 4
Sample output
16.0000
Sourceamirkabir University of Technology local contest 2006 solution: Least vertex weight Overwrite
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 long14 #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 }
View code
Poj 3308 paratroopers