POJ 3308 Paratroopers(最小割+对数降级)

来源:互联网 发布:曾飞洋 知乎 编辑:程序博客网 时间:2024/05/12 13:42
Paratroopers
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 8549 Accepted: 2588

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

14 4 52.0 7.0 5.0 2.01.5 2.0 2.0 8.01 12 23 34 41 4

Sample Output

16.0000

Source

Amirkabir University of Technology Local Contest 2006


题目大意:

    在一个M*N的网格上,有一些点有敌人,可以在每一行或每一列上安装激光炮,激光炮可以消灭一行或一列的敌人,此外在每一个位置上安装激光炮都有话费,求最小的花费乘积。


解题思路:

    题干是非常经典的网络流入门题,不过这里要求乘积最小。我们跑最大流只能得到和最小的结果,于是就可以考虑使用对数把乘积降级成为加法。具体方法就是把每个花费取对数,然后跑最大流,最后把结果还原就是最小乘积。


AC代码:

#include <iostream>#include <algorithm>#include <cstdio>#include <cstring>#include <vector>#include <queue>#include <cstdlib>#include <cmath>#include <map>#include <string>using namespace std;#define INF 0x3f3f3f3f#define LL long long#define fi first#define se second#define mem(a,b) memset((a),(b),sizeof(a))const int MAXN=50+3;const int MAXV=MAXN*2;//节点数struct Edge{    int to,rev;    double cap;    Edge(int t,double c,int r):to(t),cap(c),rev(r){};};vector<Edge> G[MAXV];int level[MAXV];int iter[MAXV];int N,M,L;void add_edge(int from,int to,double cap){    G[from].push_back(Edge(to,cap,G[to].size()));    G[to].push_back(Edge(from,0,G[from].size()-1));}void bfs(int s){    mem(level,-1);    queue<int> que;    level[s]=0;    que.push(s);    while(!que.empty())    {        int v=que.front(); que.pop();        for(int i=0;i<G[v].size();++i)        {            Edge &e=G[v][i];            if(e.cap>0&&level[e.to]<0)            {                level[e.to]=level[v]+1;                que.push(e.to);            }        }    }}double dfs(int v,int t,double f){    if(v==t)        return f;    for(int &i=iter[v];i<G[v].size();++i)    {        Edge &e=G[v][i];        if(e.cap>0&&level[v]<level[e.to])        {            double d=dfs(e.to,t,min(f,e.cap));            if(d>0)            {                e.cap-=d;                G[e.to][e.rev].cap+=d;                return d;            }        }    }    return 0;}double max_flow(int s,int t){    double flow=0;    for(;;)    {        bfs(s);        if(level[t]<0)            return flow;        mem(iter,0);        double f;        while((f=dfs(s,t,INF))>0)            flow+=f;    }}int main(){    int T;    scanf("%d",&T);    while(T--)    {        scanf("%d%d%d",&M,&N,&L);        //0         源点        //1 ~ M     行        //M+1 ~ M+N 列        //M+N+1     汇点        int s=0,t=M+N+1;        for(int i=0;i<=t;++i)            G[i].clear();        for(int i=1;i<=M;++i)        {            double cmp;            scanf("%lf",&cmp);            add_edge(s,i,log(cmp));//花费取e的对数        }        for(int i=1;i<=N;++i)        {            double cmp;            scanf("%lf",&cmp);            add_edge(M+i,t,log(cmp));//花费取e的对数        }        for(int i=0;i<L;++i)        {            int y,x;            scanf("%d%d",&y,&x);            add_edge(y,M+x,INF);        }        printf("%.4f\n",exp(max_flow(s,t)));//结果用exp还原    }        return 0;}


0 0
原创粉丝点击