POJ 3308 Paratroopers 最小点权覆盖Dinic

原创 2015年11月20日 23:28:21
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
*/



版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

POJ 3308 Paratroopers (最小点权覆盖Dinic)

题意:有些火星人要来袭击地球,我们知道这些火星人登陆的坐标(row,col),现在要把他们全部KO。地球上有一种激光武器每次可以消灭一行或者一列中所有的火星人。但是不同的行以及不同的列上安置武器的花费...
  • Tsaid
  • Tsaid
  • 2011-10-23 22:59
  • 831

poj 3308 Paratroopers 二分图最小点权覆盖集+ 最小割+邻接表

Description It is year 2500 A.D. and there is a terrible war between the forces of the Earth and ...

poj 3308 Paratroopers最小点权覆盖

//poj 3308 //sep9 #include #include #include using namespace std; const int MAXN=128; const int M...

POJ 3308 Paratroopers 二分图最小点权覆盖

题目:http://poj.org/problem?id=3308题意:有一个n*m的格子,格子上有一些敌人,给出这些敌人在格子上的位置,可以在行或者列上安装一些武器,每次消灭一行或者一列,动用每一个...

【POJ】3308 Paratroopers 化乘为加——最小点权覆盖

传送门:【POJ】3308 Paratroopers 题目分析:逗比了求的是乘积最小还

POJ 3308 Paratroopers (二分图最小点权覆盖 -> 最小割 -> 最大流)

POJ 3308 Paratroopers 链接:http://poj.org/problem?id=3308 题意:有一个N*M的方阵,有L个伞兵降落在方阵上。现在要将所有的伞兵都消灭掉...

POJ 3308 二分图最小点权覆盖 最大流

本题题意就是,公元XXXX年,地球跟外星人打仗,然后有一个n*m的网格,会有外星人降落到某些位置上,因为外星人比较猛,所以必须一下来就消灭他们,现在可以在某些行或者某些列的首部放一些激光枪。这些枪的特...

POJ--3308--Paratroopers【Dinic】二分图顶点覆盖+网络最大流

链接:http://poj.org/problem?id=3308 题意:未来世界火星人要入侵地球,他们要派一些伞兵来摧毁地球的兵工厂,兵工厂可以视为一个m*n的矩阵,现在知道了他们每个伞兵的降落位...

poj 2125 Destroying The Graph 最小点权覆盖

题意: 求一个图的最小点权覆盖。 分析: 拆点转化为求最小割。 代码: //poj 2125 //sep9 #include #include #include using names...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)