poj3308 Paratroopers

Paratroopers
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 8516 Accepted: 2573

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
大意:

现在要安装激光杀死外星人,每个激光可以消灭掉一行或者一列的所有外星人,但是放置的位置不同,修建一个激光的花费也会不同。

然后给你激光安装在每行和每列的花费,以及所有外星人的落脚点的坐标。现在问你最小花费是多少。

这题和 POJ3041 非常相似,但却并不是一回事
本题的模型是显然是一个二分图,并且是二分图中直观的顶点覆盖问题
首先说说“覆盖”的大致概念:图 G 的一个顶点覆盖是由一些顶点构成的集合 Q∈V(G), G 中有若干条边的集合 P∈E(G) ,这些边均至少有一个端点在 Q 内,则 P Q 顶点覆盖。
二分图中的顶点覆盖问题是,如果覆盖每个顶点需要付出不同的代价,也可以说是不同的花费,或称为点权(或为容量),那么问题可以描述成,在保证覆盖所有边的情况下,如何使得权和最小
对于本题,如果把伞兵视为边、行与列视为顶点,则可以通过以下方式来构图:增加两个顶点 - 源点与汇点;对于第 i 行,从源点向顶点 Ri 连接一条容量为在第 i 行安装激光枪费用的弧;对于第 j 列,从顶点 Cj 向汇点连接一条容量为在第 j 列安装激光枪费用的弧。如果某一点( i j )有伞兵降落,则从顶点 Ri 向顶点 Cj 连接一条容量为无穷大的弧。例如,对样例输入构造图:

根据最大流最小割定理,此时求的最小割就是花费最小的方案了。

需要注意的是,它问的是所有费用的乘积,因此在算法中可以先取对数,转化成加法计算才行。

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <queue>
#include <cmath>
using namespace std;
const int MAXN=100+5;
const int inf=1e9;
const double eps=1e-6;
int n,m,k;
int s,e;
double tu[MAXN][MAXN];

int pre[MAXN];
double flow[MAXN];
double EK()
{
    int i;
    for(i=0;i<=e;++i)pre[i]=-1;
    queue<int>q;
    q.push(s);
    flow[s]=inf*1.0;
    while(!q.empty())
    {
        int u=q.front();
        q.pop();
        if(u==e)break;
        for(i=1;i<=e;++i)
        {
            if(pre[i]==-1&&tu[u][i]>=eps)
            {
                pre[i]=u;
                flow[i]=min(flow[u],tu[u][i]);
                q.push(i);
            }
        }
    }
    if(pre[e]==-1)return 0;
    else return flow[e];
}

double get_maxflow()
{
    double max_flow=0.0;
    double flow=EK();
    while(flow>=eps)
    {
        max_flow+=flow;
        int u=pre[e],v=e;
        while(u!=-1)
        {
            tu[u][v]-=flow;
            tu[v][u]+=flow;
            v=u;
            u=pre[v];
        }
        flow=EK();
    }
    return exp(max_flow);
}

int main()
{
    int i;
    int t;
    scanf("%d",&t);
    while(t--)
    {
        memset(tu,0,sizeof(tu));
        scanf("%d%d%d",&n,&m,&k);
        s=0;
        e=n+m+1;
        double x;
        for(i=1;i<=n;++i)
        {
            scanf("%lf",&x);
            tu[s][i]=log(x);
        }
        m+=n;
        for(i=n+1;i<=m;++i)
        {
            scanf("%lf",&x);
            tu[i][e]=log(x);
        }
        int u,v;
        while(k--)
        {
            scanf("%d%d",&u,&v);
            v+=n;
            tu[u][v]=inf;
        }
        printf("%.4f\n",get_maxflow());
    }
    return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值