POJ 3308 Paratroopers 最大流,乘积化和 难度:2

本文探讨了在2500年的地球与火星战争中,如何使用最优化算法来部署激光枪以最少的费用消灭降落的敌方伞兵,确保关键武器工厂的安全。
Paratroopers
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 7267 Accepted: 2194

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

这道题第一眼还以为是最优匹配,但是可能开枪数大于最大匹配数,所以不能这么解
以花费为两边容量建边,中间原来的边设为inf,那么每一条边都会流过行花费或者列花费的流量限制
值得一提的是乘积应当转化为自然对数加和然后再指数回来,一开始没看到

另 会在减的过程中小于0,所以直接用a==0判断会T或者WA
//996k 16ms
#include <cstdio>
#include <cstring>
#include <queue>
#include <assert.h>
#include <cmath>
using namespace std;
const double inf=1e20;
const double eps=1e-8;
const int maxnum=302;
const int sups=300,supt=301;
double f[maxnum][maxnum];
int e[maxnum][maxnum];
int len[maxnum];

double min(double a1,double b1) {
    return a1<b1?a1:b1;
}

int m,n,l;//m ri n ci
void input(){
    scanf("%d%d%d",&m,&n,&l);
    memset(len,0,sizeof(len));

    for(int i=0;i<m;i++){
        double ttc;
        scanf("%lf",&ttc);
        f[sups][i]=log(ttc);
        f[i][sups]=0;
        e[sups][len[sups]++]=i;
        e[i][len[i]++]=sups;
    }
    for(int i=m;i<m+n;i++){
        double ttc;
        scanf("%lf",&ttc);
        f[i][supt]=log(ttc);
        f[supt][i]=0;
        e[supt][len[supt]++]=i;
        e[i][len[i]++]=supt;
    }
    for(int i=0;i<l;i++){
        int tr,tc;
        scanf("%d%d",&tr,&tc);tr--;tc=tc-1+m;
        f[tr][tc]=inf;
        f[tc][tr]=0;
        e[tr][len[tr]++]=tc;e[tc][len[tc]++]=tr;
    }
}

int d[maxnum];
bool vis[maxnum];
bool  bfs(){
    memset(vis,0,sizeof(vis));
    d[supt]=0;
    queue<int >que;
    que.push(supt);
    vis[supt]=true;
    while(!que.empty()){
        int fr=que.front();que.pop();
        for(int i=0;i<len[fr];i++){
            int to=e[fr][i];
            if(!vis[to]&&fabs(f[to][fr])>eps){
                vis[to]=true;
                d[to]=d[fr]+1;
                que.push(to);
            }
        }
    }
    return vis[sups];
}

int cur[maxnum];
double dfs(int s,double a){
    if(s==supt||a<eps)return a;
    double flow=0;
    for(int &i=cur[s];i<len[s];i++){
        int to=e[s][i];
        double sub;
        if(d[s]==d[to]+1&&(sub=dfs(to,min(a,f[s][to])))>eps){
            f[s][to]-=sub;
            f[to][s]+=sub;
            flow+=sub;
            a-=sub;
            if(fabs(a)<eps)break;
        }
    }
    return flow;
}
double maxflow(){
    double ans=0.000;
    while(bfs()){
        memset(cur,0,sizeof(cur));
        ans+=dfs(sups,inf);
    }
    return ans;
}

int main(){
    int t;
    scanf("%d",&t);
    while(t--){
        input();
        double ans=maxflow();
        printf("%.4f\n",exp(ans));
    }
    return 0;
}

  





转载于:https://www.cnblogs.com/xuesu/p/3926435.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值