POJ 3308 Paratroopers 最小点权(二分图)

来源:互联网 发布:埃及夏朝 知乎 编辑:程序博客网 时间:2024/06/04 18:24
点击打开链接

Paratroopers
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 6453 Accepted: 1940

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 × 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

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

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

Source

Amirkabir University of Technology Local Contest 2006

给你一个矩阵,要求覆盖图中的一些点, 可以每行覆盖也可以每列覆盖,但是每行每列都有一个花费,最后的花费是行和列的乘积。
因为是相乘不好弄,所以可以转换成相加。那么如何转换呢?需要用log( a )+log( b ) = log( a * b )转化。
然后建图超级源点与行相连,流量为费用,超级汇点与列相连,流量为费用。有外星人的点横坐标与列坐标相连,容量为inf,最后求最大流即可。
#include<stdio.h>#include<math.h>#include<string.h>#define N 3007#define inf 0x3f3f3fint head[N],num,ans,start,end,gap[N],dis[N];struct edge{int st,ed,next;double flow;}e[N*200];void addedge(int x,int y,double w){e[num].st=x;e[num].ed=y;e[num].flow=w;e[num].next=head[x];head[x]=num++;e[num].st=y;e[num].ed=x;e[num].flow=0;e[num].next=head[y];head[y]=num++;}double dfs(int u,double minflow){if(u==end)return minflow;int i,v;double f,flow=0.0;for(i=head[u];i!=-1;i=e[i].next){v=e[i].ed;if(e[i].flow>0){if(dis[v]+1==dis[u]){f=dfs(v,e[i].flow>minflow-flow?minflow-flow:e[i].flow);flow+=f;e[i].flow-=f;e[i^1].flow+=f;if(minflow-flow<=1e-8)return flow;if(dis[start]>=ans)return flow;}}}if(--gap[dis[u]]==0)dis[start]=ans;dis[u]++;gap[dis[u]]++;return flow;}double spfa(){double maxflow=0.0;memset(gap,0,sizeof(gap));memset(dis,0,sizeof(dis));gap[0]=ans;while(dis[start]<ans)maxflow+=dfs(start,inf);return maxflow;}int main(){int i,n,m,k,t;scanf("%d",&t);while(t--){scanf("%d%d%d",&n,&m,&k);memset(head,-1,sizeof(head));num=0;start=0;end=n+m+1;ans=end+1;double anss,val;for(i=1;i<=n;i++){scanf("%lf",&val);addedge(start,i,log(val));}for(i=1;i<=m;i++){scanf("%lf",&val);addedge(i+n,end,log(val));}int a,b;for(int i=1;i<=k;i++){scanf("%d%d",&a,&b);addedge(a,b+n,inf);}anss=spfa();printf("%.4lf\n",exp(anss));}return 0;}