POJ 3308 Paratroopers //最小割

Paratroopers
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 2357 Accepted: 753

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

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

Source

 

 

刷进第一版,甚是开心哈

这道题的构图我曾经在前面有次比赛做过,当时没做出来,现在想到底是哪道题,一直想不起来,有想起的童鞋,告我一声

谢了

 

再说一下这个题目的构图

最小权值点集覆盖

哈哈

就是最小点集覆盖的BOSS版本

 

最小点集覆盖的模型就不说了,就是用最小的点集合去覆盖E。接着想说的就是,我做了一系列最小点集覆盖以后,我发现

最小点集覆盖都可以映射到那个最原始的模型上,就是那个消行消列的矩阵。

 

最小权值点集覆盖的题目用最小割可以很完美的实现

构图来说的话也是比较简单的

还是增加两个点,一个超级源点,一个超级汇点

同样,将行集合置于左边,将列结合置于右边,行集合与超级源点连边,边的权值为消灭行的权值

列与汇点连边,边的权值为消灭列的权值

接着行列相交的点上要是有卫兵,就连边,边长为正无穷

 

这么构造最小割的意义在于什么

很简单,首先由于X->Y 的边为无穷大,所以不会被最小割割到

那么必然就会割到S-->X,Y-->T的边

接着为什么得到的最小割对应这覆盖所有的边呢,前面那篇日志就可以说明。

 

 

#include<cstdio>

#include<cstring>

#include<cmath>

const int N=102;

const int M=2001;

const double inf=1e10;

int head[N];

struct Edge

{

    int v,next;

    double w;

} edge[M];

int cnt,n,s,t;

void addedge(int u,int v,double w)

{

    edge[cnt].v=v;

    edge[cnt].w=w;

    edge[cnt].next=head[u];

    head[u]=cnt++;

    edge[cnt].v=u;

    edge[cnt].w=0;

    edge[cnt].next=head[v];

    head[v]=cnt++;

}

double sap()

{

    int pre[N],cur[N],dis[N],gap[N];

    double flow=0,aug=inf;

    int u;

    bool flag;

    for(int i=0; i<n; i++)

    {

        cur[i]=head[i];

        gap[i]=dis[i]=0;

    }

    gap[s]=n;

    u=pre[s]=s;

    while(dis[s]<n)

    {

        flag=0;

        for(int &j=cur[u]; j!=-1; j=edge[j].next)

        {

            int v=edge[j].v;

            if(edge[j].w>0&&dis[u]==dis[v]+1)

            {

                flag=1;

                if(edge[j].w<aug) aug=edge[j].w;

                pre[v]=u;

                u=v;

                if(u==t)

                {

                    flow+=aug;

                    while(u!=s)

                    {

                        u=pre[u];

                        edge[cur[u]].w-=aug;

                        edge[cur[u]^1].w+=aug;

                    }

                    aug=inf;

                }

                break;

            }

        }

        if(flag) continue;

        int mindis=n;

        for(int j=head[u]; j!=-1; j=edge[j].next)

        {

            int v=edge[j].v;

            if(edge[j].w>0&&dis[v]<mindis)

            {

                mindis=dis[v];

                cur[u]=j;

            }

        }

        if((--gap[dis[u]])==0)

            break;

        gap[dis[u]=mindis+1]++;

        u=pre[u];

    }

    return flow;

}

int main()

{

    int m,l,T;

    double x;

    scanf("%d",&T);

    while(T--)

    {

        scanf("%d%d%d",&n,&m,&l);

        s=0;

        t=n+m+1;

        cnt=0;

        memset(head,-1,sizeof(head));

        for(int i=1;i<=n;i++)

        {

            scanf("%lf",&x);

            addedge(0,i,log(x));

        }

        for(int i=1;i<=m;i++)

        {

            scanf("%lf",&x);

            addedge(i+n,n+m+1,log(x));

        }

        for(int i=1;i<=l;i++)

        {

            int a,b;

            scanf("%d%d",&a,&b);

            addedge(a,b+n,inf);

        }

        n=n+m+2;

        printf("%.4lf/n",exp(sap()));

    }

    return 0;

}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值