(POJ3308)Paratroopers 最大流最小割,建图,模板题

                       Paratroopers
        Time Limit: 1000MS      Memory Limit: 65536K
        Total Submissions: 8373     Accepted: 2516

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 m × 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
Source

Amirkabir University of Technology Local Contest 2006

题意:
在一个n*m的网格中,每行每列有一个激光枪,每个激光枪可以消灭该行或该列的所有伞兵,使用每个激光枪有一定的费用。有L个伞兵位于网格中,要消灭所有的伞兵,要建一个激光枪系统,系统的费用是每个激光枪的之积。问如何建立激光枪系统使整个系统费用最小。

分析:
此题的难点就是建图,我们以以下方式建图:把伞兵视边,行和列视为顶点,增加两个顶点-源点和汇点;对于第i行,从源点向顶点Ri建立一条容量为在第i行安装激光枪的费用的弧;对于第j列,从顶点Cj向汇点建立一条容量为在第j列安装激光枪的费用的弧。如果有伞兵降落于(i,j),则从顶点Ri向顶点Cj建立一条容量为无穷大的弧。

为了求割的最小容量,所以R->C不可能被选中,只能选中某些行或列。此时最小割即为最小费用。由最大流最小割定理我们求最大流即可。
由于费用是个费用之积,我们可以利用log(),转换成求和后在还原:a*b*c=e^(log(a*b*c)) ,又log(a*b*c)=log(a)+log(b)+log(c),所以我们先求log(a)+log(b)+log(c),最后利用exp()还原。

AC代码:

#include<iostream>
#include<algorithm>
#include<string>
#include<sstream>
#include<set>
#include<vector>
#include<stack>
#include<map>
#include<queue>
#include<deque>
#include<cstdlib>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<ctime>
#include<functional>
using namespace std;

#define N 1000
#define INF 100000000

struct Edge
{
    int from,to;
    double cap,flow;
    Edge(int u,int v,double c,double f):from(u),to(v),cap(c),flow(f){}
};

struct Dinic
{
    int n,m,s,t;//结点数,边数(包括反向弧),源点编号,汇点编号
    vector<Edge>edges;//边表,dges[e]和dges[e^1]互为反向弧
    vector<int>G[N];//邻接表,G[i][j]表示结点i的第j条边在e数组中的编号
    bool vis[N]; //BFS的使用
    int d[N]; //从起点到i的距离
    int cur[N]; //当前弧下标

    void addedge(int from,int to,double cap)
    {
        edges.push_back(Edge(from,to,cap,0));
        edges.push_back(Edge(to,from,0,0));
        int  m=edges.size();
        G[from].push_back(m-2);
        G[to].push_back(m-1);
    }

    bool bfs()
    {
        memset(vis,0,sizeof(vis));
        queue<int>Q;
        Q.push(s);
        d[s]=0;
        vis[s]=1;
        while(!Q.empty())
        {
            int x=Q.front();Q.pop();
            for(int i=0;i<G[x].size();i++)
            {
                Edge&e=edges[G[x][i]];
                if(!vis[e.to]&&e.cap>e.flow)//只考虑残量网络中的弧
                {
                    vis[e.to]=1;
                    d[e.to]=d[x]+1;
                    Q.push(e.to);
                }
            }

        }
        return vis[t];
    }

    double dfs(int x,double a)//x表示当前结点,a表示目前为止的最小残量
    {
        if(x==t||a==0)return a;//a等于0时及时退出,此时相当于断路了
        double flow=0,f;
        for(int&i=cur[x];i<G[x].size();i++)//从上次考虑的弧开始,注意要使用引用,同时修改cur[x]
        {
            Edge&e=edges[G[x][i]];//e是一条边
            if(d[x]+1==d[e.to]&&(f=dfs(e.to,min(a,e.cap-e.flow)))>0)
            {
                e.flow+=f;
                edges[G[x][i]^1].flow-=f;
                flow+=f;
                a-=f;
                if(!a)break;//a等于0及时退出,当a!=0,说明当前节点还存在另一个曾广路分支。

            }
        }
        return flow;
    }

    double Maxflow(int s,int t)//主过程
    {
        this->s=s,this->t=t;
        double flow=0;
        while(bfs())//不停地用bfs构造分层网络,然后用dfs沿着阻塞流增广
        {
            memset(cur,0,sizeof(cur));
            flow+=dfs(s,INF);
        }
        return flow;
    }
  };

  int main()
  {
      //freopen("in.txt","r",stdin);
      int t,n,m,L,a,b;
      double w;
      scanf("%d",&t);
      while(t--)
      {
          Dinic dinic;
          scanf("%d%d%d",&n,&m,&L);
          for(int i=1;i<=n;i++)
          {
              scanf("%lf",&w);
              dinic.addedge(0,i,log(w));
          }
          for(int i=1;i<=m;i++)
          {
              scanf("%lf",&w);
              dinic.addedge(n+i,n+m+1,log(w));
          }
          for(int i=0;i<L;i++)
          {
              scanf("%d%d",&a,&b);
              dinic.addedge(a,n+b,INF);
          }
          printf("%.4lf\n",exp(dinic.Maxflow(0,n+m+1)));
      }

      return 0;
  }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值