POJ 3308 Paratroopers(最小点权覆盖)(对数乘转加)

http://poj.org/problem?id=3308

 

r*c的地图

每一个大炮可以消灭一行一列的敌人

安装消灭第i行的大炮花费是ri

安装消灭第j行的大炮花费是ci

已知敌人坐标,同时消灭所有敌人,问最小花费

花费为所有大炮消费的乘积

 

乘转加:log(a*b*c)=log(a)+log(b)+log(c)

经典的最小点权覆盖

源点向行连,列向汇点连

第i行j列有敌人,点i向点j连inf边

 

最小点权覆盖=最小割

 

 

#include<cmath>
#include<queue>
#include<cstdio>
#include<cstring>
#include<algorithm>

using namespace std;

const double inf=2000000000;
const double eps=1e-9;

#define N 201
#define M 701

int src,decc;

int tot;
int front[N],to[M<<1],nxt[M<<1];
double val[M<<1];

int cur[N],lev[N];

queue<int>q;

void add(int u,int v,double cap)
{
    to[++tot]=v; nxt[tot]=front[u]; front[u]=tot; val[tot]=cap;
    to[++tot]=u; nxt[tot]=front[v]; front[v]=tot; val[tot]=0;
}

bool bfs()
{
    for(int i=0;i<=decc;++i) lev[i]=-1,cur[i]=front[i];
    while(!q.empty()) q.pop();
    lev[src]=0;
    q.push(src);
    int now,t;
    while(!q.empty())
    {
        now=q.front();
        q.pop();
        for(int i=front[now];i;i=nxt[i])
        {
            t=to[i];
            if(lev[t]==-1 && val[i]>eps)
            {
                lev[t]=lev[now]+1;
                if(t==decc) return true;
                q.push(t);        
            }
        }
    }
    return false;
}

double dinic(int now,double flow)
{
    if(now==decc) return flow;
    double rest=0,delta;
    int t;
    for(int &i=cur[now];i;i=nxt[i])
    {
        t=to[i];
        if(lev[t]>lev[now] && val[i]>eps)
        {
            delta=dinic(t,min(flow-rest,val[i]));
            if(delta>eps)
            {
                rest+=delta;
                val[i]-=delta; val[i^1]+=delta;
                if(fabs(rest-flow)<eps) break;
            }
        }
    }
    if(fabs(rest-flow)>eps) lev[now]=-1;
    return rest;
}

int main()
{
    int T;
    scanf("%d",&T);
    int n,m,k;
    double x,ans;
    int a,b;
    while(T--)
    {
        scanf("%d%d%d",&n,&m,&k);
        decc=n+m+1;
        tot=1;
        memset(front,0,sizeof(front));
        for(int i=1;i<=n;++i)
        {
            scanf("%lf",&x);
            add(src,i,log(x));
        }
        for(int i=1;i<=m;++i)
        {
            scanf("%lf",&x);
            add(i+n,decc,log(x));
        }
        for(int i=1;i<=k;++i)
        {
            scanf("%d%d",&a,&b);
            add(a,b+n,inf);
        }
        ans=0;
        while(bfs()) ans+=dinic(src,inf);
        printf("%.4lf\n",exp(ans));
    }
}

 

Paratroopers
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 8903 Accepted: 2679

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

转载于:https://www.cnblogs.com/TheRoadToTheGold/p/8001339.html

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值