首页 > 代码库 > POJ3308 Paratroopers(网络流)(最小割)
POJ3308 Paratroopers(网络流)(最小割)
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 8443 | Accepted: 2541 |
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 × 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
【分析】讲真,要不是看题解,很难想到用网络流做。这题建图是把每一行,每一列作为结点,然后建立两个
超级源点,汇点,源点与行连边,容量为Ri,列与汇点连边,容量为Ci,对于位置坐标,行向列连边,容量
为inf,然后求最大流。
#include <iostream>#include <cstring>#include <cstdio>#include <cstring>#include <algorithm>#include <cmath>#include <time.h>#include <string>#include <map>#include <stack>#include <vector>#include <set>#include <queue>#define inf 10000000#define mod 10000typedef long long ll;using namespace std;const int N=105;const int M=5000;int power(int a,int b,int c){int ans=1;while(b){if(b%2==1){ans=(ans*a)%c;b--;}b/=2;a=a*a%c;}return ans;}struct man{ double c,f;}w[N][N];int head[N],dis[N],pre[N],n,m,k;int t,cnt=0,maxn;double ans;bool flag;bool bfs(){ queue<int>q; memset(pre,-1,sizeof(pre)); q.push(0); pre[0]=0; while(!q.empty() && !dis[t]){ int u=q.front();q.pop(); for(int i=1;i<=t;i++){ if(pre[i]==-1&&w[u][i].c>w[u][i].f){ q.push(i); pre[i]=u; if(i==t) return true; } } } return false;}void dinic(){ ans=0; while(bfs()){ double tt=inf; for(int i=t;i!=0;i=pre[i]){ tt=min(tt,w[pre[i]][i].c-w[pre[i]][i].f); } for(int i=t;i!=0;i=pre[i]){ w[pre[i]][i].f+=tt;w[i][pre[i]].f-=tt; } ans+=tt; } printf("%.4lf\n",exp(ans));}void init(){ int a,b; double d; scanf("%d%d%d",&n,&m,&k);t=n+m+1; for(int i=1;i<=n;i++){ scanf("%lf",&d); d=log(d); w[0][i].c=d; } for(int i=1;i<=m;i++){ scanf("%lf",&d); d=log(d); w[i+n][t].c=d; } while(k--){ scanf("%d%d",&a,&b); w[a][b+n].c=inf; }}int main(){ int T; scanf("%d",&T); while(T--){ memset(w,0,sizeof(w)); ans=0; init(); dinic(); } return 0;}
POJ3308 Paratroopers(网络流)(最小割)