POJ3308 Paratroopers(网络流)(最小割)

                                                 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

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
【分析】讲真,要不是看题解,很难想到用网络流做。这题建图是把每一行,每一列作为结点,然后建立两个
超级源点,汇点,源点与行连边,容量为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 10000
typedef long long ll;
using namespace std;
const int N=;
const int M=;
int power(int a,int b,int c){int ans=;while(b){if(b%==){ans=(ans*a)%c;b--;}b/=;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=,maxn;
double ans;
bool flag; bool bfs()
{
queue<int>q;
memset(pre,-,sizeof(pre));
q.push();
pre[]=;
while(!q.empty() && !dis[t]){
int u=q.front();q.pop();
for(int i=;i<=t;i++){
if(pre[i]==-&&w[u][i].c>w[u][i].f){
q.push(i);
pre[i]=u;
if(i==t) return true;
}
}
}
return false;
} void dinic()
{
ans=;
while(bfs()){
double tt=inf;
for(int i=t;i!=;i=pre[i]){
tt=min(tt,w[pre[i]][i].c-w[pre[i]][i].f);
}
for(int i=t;i!=;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+;
for(int i=;i<=n;i++){
scanf("%lf",&d);
d=log(d);
w[][i].c=d;
}
for(int i=;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,,sizeof(w));
ans=;
init();
dinic();
}
return ;
}
上一篇:iOS开发 QQ粘性动画效果


下一篇:spring是怎样面向接口编程的?