(模板题)poj 3308 Paratroopers(Dinic算法最大流)

Paratroopers
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 8481 Accepted: 2554

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

提示

题意:
在n行m列(1<=n,m<=50)的地图中,在地图外每行每列上都有一座人间大炮,大炮可以把当前行或列的石头打掉,且每一台的花费都不一样。我们需要用这些大炮打掉地图中的l(1<=l<=500)个石头,总的花费为所使用的大炮花费的乘积,求出最小花费。
思路:
类似于 poj3041,但有一点不同,这题使用最少并不代表是最小花费,我们需将二分图转化为网络流模型,利用最小割==最大流来求出答案。
建模过程:
建立源点、终点,每个点的x坐标到y坐标建立一个无穷大的边(别忘了流量为0的反向边,后面也一样),源点与地图中x坐标建边,边权就是每一行大炮的花费,终点与地图中y坐标建边,边权就是每一列大炮的花费,跑一遍最大流即可。
精度要求貌似较高,需要利用ln(a*b)=lna+lnb来减少精度损失,最后做输出用exp函数就可得到a*b的值。
浅谈最小割模型及其应用: http://www.doc88.com/p-4455456708170.html
Dinic算法的原理与构造: http://blog.csdn.net/wall_f/article/details/8207595

示例程序

Source Code

Problem: 3308		Code Length: 2267B
Memory: 468K		Time: 16MS
Language: GCC		Result: Accepted
#include <stdio.h>
#include <string.h>
#include <math.h>
#define MAX 1e9+7
struct
{
    int v,next;
    double val;
}w[1500];
int h[102],numw,deep[102];
void insert(int u,int v,double val)
{
    w[numw].v=v;
    w[numw].val=val;
    w[numw].next=h[u];
    h[u]=numw;
    numw++;
    w[numw].v=u;
    w[numw].val=0;
    w[numw].next=h[v];
    h[v]=numw;
    numw++;
}
double min(double x,double y)
{
    if(x>y)
    {
        return y;
    }
    else
    {
        return x;
    }
}
double eps(double x)
{
    return fabs(x)<1e-5?0:x;
}
int bfs(int s,int t)
{
    int i,q[20000],top=0,f=0;
    memset(deep,0,sizeof(deep));
    deep[s]=1;
    q[top]=s;
    top++;
    while(f<top)
    {
        if(q[f]==t)
        {
            return 1;
        }
        for(i=h[q[f]];i!=-1;i=w[i].next)
        {
            if(deep[w[i].v]==0&&eps(w[i].val)>0)
            {
                q[top]=w[i].v;
                top++;
                deep[w[i].v]=deep[q[f]]+1;
            }
        }
        f++;
    }
    return 0;
}
double dfs(int s,int t,double max)
{
    int i,pos;
    double val,temp=0;
    if(s==t)
    {
        return max;
    }
    for(i=h[s];i!=-1;i=w[i].next)
    {
        pos=w[i].v;
        val=w[i].val;
        if(deep[s]+1==deep[pos]&&eps(val)>0)
        {
            val=dfs(pos,t,min(max-temp,val));
            w[i].val=w[i].val-val;
            w[i^1].val=w[i^1].val+val;
            temp=temp+val;
            if(temp==max)
            {
                break;
            }
        }
    }
    return temp;
}
double dinic(int s,int t)
{
    double sum=0;
    while(bfs(s,t)==1)
    {
        sum=sum+dfs(s,t,MAX);
    }
    return exp(sum);
}
int main()
{
    int t,m,n,l,i,i1,u,v;
    double val;
    scanf("%d",&t);
    for(i=1;t>=i;i++)
    {
        memset(h,-1,sizeof(h));
        numw=0;
        scanf("%d %d %d",&n,&m,&l);
        for(i1=1;n>=i1;i1++)
        {
            scanf("%lf",&val);
            insert(0,i1,log(val));
        }
        for(i1=1;m>=i1;i1++)
        {
            scanf("%lf",&val);
            insert(n+i1,n+m+1,log(val));
        }
        for(i1=1;l>=i1;i1++)
        {
            scanf("%d %d",&u,&v);
            insert(u,v+n,MAX);
        }
        printf("%.4f\n",dinic(0,n+m+1));
    }
    return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值