poj3308——Paratroopers(最小割)

来源:互联网 发布:c语言模块化程序设计 编辑:程序博客网 时间:2024/05/21 10:02

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

敌军处于一个n*m的网格上,每个网格有一个坐标。现在想全部消灭这个敌军,就要在一行或者一列上放置武器,这个武器能摧毁该列或者该行的全部敌人,但相应的也会有开销。求最少需要多少钱就能把敌军全部歼灭,注意最后的金额是各行和各列的开销相乘。。。地球都要灭亡了还管这些

总之因为最后是算乘积,所以要转化成log(a*b)=log(a)+log(b)的形式,最后再还原;
将每个敌军的位置拆成两个点,一个点连x坐标,边权是x边的开销,另一个点连y坐标,边权是y边的开销,两个点之间也相连,边权是无限大。再将所有x坐标与源点相连,y坐标与汇点相连,最少的开销就是这个图的最小割,也可以说是最小费用最大流?

#include <iostream>#include <cstring>#include <string>#include <vector>#include <queue>#include <cstdio>#include <set>#include <math.h>#include <algorithm>#include <queue>#include <iomanip>#include <ctime>#define INF 0x3f3f3f3f#define MAXN 1005#define Mod 1000000007using namespace std;struct Edge{    int s,e,next;    double v;} edge[15*MAXN];int n,e_num,head[MAXN],d[MAXN],sp,tp;void AddEdge(int a,int b,double c){    edge[e_num].s=a;    edge[e_num].e=b;    edge[e_num].v=c;    edge[e_num].next=head[a];    head[a]=e_num++;    edge[e_num].s=b;    edge[e_num].e=a;    edge[e_num].v=0.0;    edge[e_num].next=head[b];    head[b]=e_num++;}int bfs(){    queue <int> q;    memset(d,-1,sizeof(d));    d[sp]=0;    q.push(sp);    while(!q.empty())    {        int cur=q.front();        q.pop();        for(int i=head[cur]; i!=-1; i=edge[i].next)        {            int u=edge[i].e;            if(d[u]==-1 && edge[i].v>0) //没有标记,且可行流大于0            {                d[u]=d[cur]+1;                q.push(u);            }        }    }    return d[tp] != -1;//汇点是否成功标号,也就是说是否找到增广路}double dfs(int a,double b) //a为起点{    double r=0;    if(a==tp)return b;    for(int i=head[a]; i!=-1 && r<b; i=edge[i].next)    {        int u=edge[i].e;        if(edge[i].v>0 && d[u]==d[a]+1)        {            double x=min(edge[i].v,b-r);            x=dfs(u,x);            r+=x;            edge[i].v-=x;            edge[i^1].v+=x;        }    }    if(!r)d[a]=-2;    return r;}double dinic(int sp,int tp){    double total=0.0,t;    while(bfs())    {        while(t=dfs(sp,INF))            total+=t;    }    return total;}double row[MAXN],col[MAXN];int main(){    int t;    scanf("%d",&t);    while(t--)    {        int m,l;        scanf("%d%d%d",&n,&m,&l);        e_num=0;        memset(head,-1,sizeof(head));        for(int i=1;i<=n;++i)            scanf("%lf",&col[i]);        for(int i=1;i<=m;++i)            scanf("%lf",&row[i]);        sp=0,tp=n+m+1;        for(int i=1;i<=n;++i)        {            AddEdge(sp,i,log(col[i]));        }        for(int i=1;i<=m;++i)        {            AddEdge(i+n,tp,log(row[i]));        }        for(int i=1;i<=l;++i)        {            int a,b;            scanf("%d%d",&a,&b);            AddEdge(a,b+n,INF);        }        double ans=dinic(sp,tp);        printf("%.4lf\n",exp(ans));    }    return 0;}
0 0