码迷,mamicode.com
首页 > 其他好文 > 详细

hdu 4289 利用最大流思想求图的最小割

时间:2015-04-06 17:20:57      阅读:233      评论:0      收藏:0      [点我收藏+]

标签:

http://acm.hdu.edu.cn/showproblem.php?pid=4289

Problem Description
  You, the head of Department of Security, recently received a top-secret information that a group of terrorists is planning to transport some WMD 1 from one city (the source) to another one (the destination). You know their date, source and destination, and they are using the highway network.
  The highway network consists of bidirectional highways, connecting two distinct city. A vehicle can only enter/exit the highway network at cities only.
  You may locate some SA (special agents) in some selected cities, so that when the terrorists enter a city under observation (that is, SA is in this city), they would be caught immediately.
  It is possible to locate SA in all cities, but since controlling a city with SA may cost your department a certain amount of money, which might vary from city to city, and your budget might not be able to bear the full cost of controlling all cities, you must identify a set of cities, that:
  * all traffic of the terrorists must pass at least one city of the set.
  * sum of cost of controlling all cities in the set is minimal.
  You may assume that it is always possible to get from source of the terrorists to their destination.
------------------------------------------------------------
1 Weapon of Mass Destruction
 

Input
  There are several test cases.
  The first line of a single test case contains two integer N and M ( 2 <= N <= 200; 1 <= M <= 20000), the number of cities and the number of highways. Cities are numbered from 1 to N.
  The second line contains two integer S,D ( 1 <= S,D <= N), the number of the source and the number of the destination.
  The following N lines contains costs. Of these lines the ith one contains exactly one integer, the cost of locating SA in the ith city to put it under observation. You may assume that the cost is positive and not exceeding 107.
  The followingM lines tells you about highway network. Each of these lines contains two integers A and B, indicating a bidirectional highway between A and B.
  Please process until EOF (End Of File).
 

Output
  For each test case you should output exactly one line, containing one integer, the sum of cost of your selected set.
  See samples for detailed information.
 

Sample Input
5 6 5 3 5 2 3 4 12 1 5 5 4 2 3 2 4 4 3 2 1
 

Sample Output
3
/**
hdu 4289  利用最大流思想求图的最小割
题目大意:一个恐怖分子要从起点出发到终点去,终点和起点之间是一张高速公路网络其含n个点(包含起点和终点),现在警察要在其中一些些点中设伏,每个点有一个设伏花费,
         问在哪些点设伏可以使得在保证能抓到恐怖分子的前提下花费最少
解题思路:求图的最小割的一道题目,将每个点拆成两个点,中将连接(单向边)为该点的设伏花费,然后给定的任意两点自己的告诉公路的权值为oo(双向边),最后跑一遍最大流,
          最大流即为最小割
*/
#include<cstdio>
#include<iostream>
#include<string.h>
#include<algorithm>
using namespace std;
const int oo=1<<30;
const int mm=220005;
const int mn=1212;

int node,src,dest,edge;
int ver[mm],flow[mm],_next[mm];
int head[mn],work[mn],dis[mn],q[mn];

void prepare(int _node,int _src,int _dest)
{
    node=_node,src=_src,dest=_dest;
    for(int i=0; i<node; ++i)head[i]=-1;
    edge=0;
}

void addedge(int u,int v,int c)
{
    ver[edge]=v,flow[edge]=c,_next[edge]=head[u],head[u]=edge++;
    ver[edge]=u,flow[edge]=0,_next[edge]=head[v],head[v]=edge++;
}

bool Dinic_bfs()
{
    int i,u,v,l,r=0;
    for(i=0; i<node; ++i)dis[i]=-1;
    dis[q[r++]=src]=0;
    for(l=0; l<r; ++l)
        for(i=head[u=q[l]]; i>=0; i=_next[i])
            if(flow[i]&&dis[v=ver[i]]<0)
            {
                dis[q[r++]=v]=dis[u]+1;
                if(v==dest)return 1;
            }
    return 0;
}

int Dinic_dfs(int u,int exp)
{
    if(u==dest)return exp;
    for(int &i=work[u],v,tmp; i>=0; i=_next[i])
        if(flow[i]&&dis[v=ver[i]]==dis[u]+1&&(tmp=Dinic_dfs(v,min(exp,flow[i])))>0)
        {
            flow[i]-=tmp;
            flow[i^1]+=tmp;
            return tmp;
        }
    return 0;
}

int Dinic_flow()
{
    int i,ret=0,delta;
    while(Dinic_bfs())
    {
        for(i=0; i<node; ++i)work[i]=head[i];
        while(delta=Dinic_dfs(src,oo))ret+=delta;
    }
    return ret;
}
int n,m;
int main()
{
    while(~scanf("%d%d",&n,&m))
    {
         int x,y;
         scanf("%d%d",&x,&y);
         prepare(n*2+1,x,y+n);
         for(int i=1;i<=n;i++)
         {
             int x;
             scanf("%d",&x);
             addedge(i,i+n,x);
         }
         for(int i=0;i<m;i++)
         {
             int u,v;
             scanf("%d%d",&u,&v);
             addedge(u+n,v,oo);
             addedge(v+n,u,oo);
         }
         printf("%d\n",Dinic_flow());
    }
    return 0;
}


hdu 4289 利用最大流思想求图的最小割

标签:

原文地址:http://blog.csdn.net/lvshubao1314/article/details/44902833

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!