关闭

POJ 3308 Paratroopers 最小点权覆盖Dinic

标签: c++poj图论
373人阅读 评论(0) 收藏 举报
分类:
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
*/



0
0
查看评论
发表评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场

POJ 3308 Paratroopers 最小点权(二分图)

点击打开链接 Paratroopers Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6453   Ac...
  • Dinivity123
  • Dinivity123
  • 2013-11-29 17:07
  • 739

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

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

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

传送门:【POJ】3308 Paratroopers 题目分析:逗比了求的是乘积最小还
  • u013368721
  • u013368721
  • 2014-08-03 11:49
  • 447

POJ3041 Asteroids 二分图最小顶点覆盖 Dinic求解最大流

题意 一个n*n的矩阵,有k个位置有小行星,一炮可以摧毁一行或一列行星 问最少需要多少炮可以摧毁所有行星 思路 把一行、一列看成节点,矩阵里的一个小行星看成一条边,如行星位置(r,c),则第r行的节点...
  • luke2834
  • luke2834
  • 2016-04-02 21:41
  • 576

poj 3308(最小点权覆盖、最小割)

题目链接:http://poj.org/problem?id=3308 思路:裸的最小点权覆盖,建立超级源点和超级汇点,将源点与行相连,容量为这行消灭敌人的代价,将列与汇点相连,容量为这列消灭敌人的...
  • zjsyhjh
  • zjsyhjh
  • 2014-05-26 18:04
  • 278

poj 3308 最小点权覆盖(对数乘积)

火星人侵略地球,他们意图登陆破坏某个地区的兵器工厂。据探子回报,火星人登陆的地区为n*m大小的地域,而且每一个火星人的着陆点坐标已知。 火星人很强悍,只要有一个火星人着陆后能够幸存,他必定能毁坏...
  • u010126535
  • u010126535
  • 2014-10-17 09:47
  • 405

poj3308 Paratroopers --- 最小点权覆盖->最小割

题目是一个很明显的二分图带权匹配模型, 添加源点到nx建边,ny到汇点建边,(nx,ny)=inf建边,求最小割既得最小点权覆盖。 在本题中由于求的是乘积,所以先全部取log转换为加法,最后再乘方...
  • u011032846
  • u011032846
  • 2014-07-31 08:05
  • 624

poj 2125 最小割解决 "最小点权覆盖问题" +输出解(割边集)

poj 2125 最小割解决 "最小点权覆盖问题" +输出解(割边集) 分类: ACM_图论_二分匹配 ACM_图论_网络流 ACM_图论_最小割 ACM_图论2013-09-05 20...
  • pi9nc
  • pi9nc
  • 2014-05-26 21:42
  • 1549

poj 3020(二分图最小路径覆盖,拆点)

题目链接题目描述: Antenna Placement Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 8534 ...
  • fouzhe
  • fouzhe
  • 2016-07-26 20:15
  • 421

最大流的理解以及dinic模板 poj1273

增广路以及残留网络的定义不再赘述了。算导上说的很清楚,证明也有,看懂了就知道怎么求最大流了。 而算导上提到的FF方法以及ek算法的伪代码中都是将流与残留容量分开储存,其实代码实现的时候我们只需存正反...
  • hitwhacmer1
  • hitwhacmer1
  • 2015-07-23 11:43
  • 1294
    个人资料
    • 访问:376648次
    • 积分:11810
    • 等级:
    • 排名:第1476名
    • 原创:805篇
    • 转载:2篇
    • 译文:0篇
    • 评论:111条
    最新评论