MY Blog

我最讨厌中途放弃的人

POJ 3308 Paratroopers 最小点权覆盖Dinic
F - Paratroopers
Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u

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

火星人进攻地球在n*m的格子内部,地球人可以在n行每一行安装枪,m列同理。在不同地方安放枪费用不同,问如何安放使得行花费乘上列花费最小使得消灭所有敌人。

这就是最小点集覆盖,最小割用dinic模板。因为是求乘所以转为对数就变成了加法。

ACcode:

#pragma warning(disable:4786)//使命名长度不受限制
#pragma comment(linker, "/STACK:102400000,102400000")//手工开栈
#include <map>
#include <set>
#include <queue>
#include <cmath>
#include <stack>
#include <cctype>
#include <cstdio>
#include <cstring>
#include <stdlib.h>
#include <iostream>
#include <algorithm>
#define rd(x) scanf("%d",&x)
#define rd2(x,y) scanf("%d%d",&x,&y)
#define rds(x) scanf("%s",x)
#define rdc(x) scanf("%c",&x)
#define ll long long int
#define maxn 100005
#define mod 1000000007
#define INF 1e8 //int 最大值
#define FOR(i,f_start,f_end) for(int i=f_start;i<=f_end;++i)
#define MT(x,i) memset(x,i,sizeof(x))
#define PI  acos(-1.0)
#define E  exp(1)
#define esp 0.00000001
using namespace std;
struct Edge{
    int to,next;
    double w;
}e[maxn];
int n,m,loop,num,tot,head[maxn],level[maxn];
void addEdge(int u,int v,double w){
    e[tot].to=v;e[tot].w=w;e[tot].next=head[u];head[u]=tot++;
    e[tot].to=u;e[tot].w=0;e[tot].next=head[v];head[v]=tot++;
}
bool makelevel(){
    double w;
    MT(level,-1);
    int que[maxn];
    int front=0,rear=0,u;
    level[rear++]=0;
    while(front!=rear){
        u=que[front++];
        for(int k=head[u];k!=-1;k=e[k].next){
            int v=e[k].to;
            w=e[k].w;
            if(w>esp&&level[v]<0){
                level[v]=level[u]+1;
                que[rear++]=v;
            }
        }
    }
    return level[n+m+1]>=0;
}
double dfs(int now ,double maxf,int t){
    if(now==t)return maxf;
    double ret=0,s;
    for(int k=head[now];k!=-1;k=e[k].next){
            int v=e[k].to;
            double w=e[k].w;
            if(w>esp&&level[v]==level[now]+1&&(s=dfs(v,min(maxf,w),t))){
                e[k].w-=s;
                e[k^1].w+=s;
                return s;
            }
        }
    return 0;
}
double dinic(int s,int t){
    double ans=0,ss;
    while(makelevel()){
            ss=dfs(s,INF,t);
        if(ss>esp)
            ans+=ss;
        else break;
    }
    return ans;
}
int main(){
    rd(loop);
    while(loop--){
        double val;
        int u,v;
        MT(head,-1);
        tot=0;
        rd2(n,m);rd(num);
        FOR(i,1,n){
            scanf("%lf",&val);
            addEdge(0,i,log(val));
        }
        FOR(i,1,m){
            scanf("%lf",&val);
            addEdge(n+i,n+m+1,log(val));
        }
        FOR(i,1,num){
            rd2(u,v);
            addEdge(u,v+n,INF);
        }
        printf("%0.4lf\n",exp(dinic(0,n+m+1)));
    }
    return 0;
}
/*
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
*/



阅读更多
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/zp___waj/article/details/49953993
文章标签: c++ poj 图论
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

不良信息举报

POJ 3308 Paratroopers 最小点权覆盖Dinic

最多只允许输入30个字

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭