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

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;
}
时间: 2025-01-19 21:28:44

hdu 4289 利用最大流思想求图的最小割的相关文章

HDU 4289 Control (最大流+拆点)

http://acm.hdu.edu.cn/showproblem.php?pid=4289 题目讲的是有一些恐怖分子要从S市去往D市,要求在一些城市中安排特工,保证一定能够抓住恐怖分子,因为安排特工需要一定的费用,所以希望找出最小的花费. 思路:可以把每个城市,即每个点拆分成进来的点和出去的点,如x点分成x和x+n,两点连接的边权值为x点上安排特工的费用.而如果x和y两点有连线,则连接x+n,y,然后求从S市到达D市的最大流.之所以能这样求,是因为在求最大流的过程中每次所更新的流量是所有边中最

POJ 2987 Firing (最大权闭合图,最小割)

http://poj.org/problem?id=2987 Firing Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 7865   Accepted: 2377 Description You've finally got mad at "the world's most stupid" employees of yours and decided to do some firings. You're n

关于利用快排思想求第K小数的分析

最近复习快排算法,记得当时最有意思的是可以用快排的partition函数求出第K小数 于是上网搜索一番,发现都只是贴出了代码.无奈只好自己研究了下 利用快排partition求第k小数不得不从partition函数开始说: 快速排序的思想是在一组待排序的数中,找出一个数作为分界,使得它前面的数都比它小,后面的数都比它大.这个数叫做枢轴 当求出一组数的枢轴以后,一组数就可以以枢轴为界限分成两组,我们可以递归的找出这两个组的枢轴,只到每组只有一个数 这里我们应该注意的是,partition函数返回的

I - Control - HDU 4289 (最大流)

题意:有N个城市,现在城市S出现了一伙歹徒,他们想运送一些炸弹到D城市,不过警方已经得到了线报知道他们的事情,不过警察不知道他们所在的具体位置,所以只能采取封锁城市的办法来阻断暴徒,不过封锁城市是需要花费一定代价的,由于警局资金比较紧张,所以想知道如果完全阻断暴徒从S城市到达D城市的最小需要花费的代价. 输入: 首先输入的是N,M表示有N个城市M条道路,接下来有N行每行一个数字表示封锁这个城市的代价,然后M行道路,每个道路都连接两个城市(路没有交叉,并且是双向路). 分析:这应该叫做最小割,有向

noi 2006 最大收益 最大权闭合图转最小割转最大流

题意:一个公司有n个可以建造通讯战的地方,建造成本分别为pi,然后第i个公司会选择使用通讯站ai与bi,使用费用是ci,然后问这个通讯公司怎么建站能够获利最大.(净获利=总收益-总成本): 网上看到一篇题解,直接说这是个最小割,求最小割然后总收益-最小割就是了.这种题解就是一点用也没有,为什么是最小割,总得解释解释吧,撂下结论就跑了,这种题解写来何用. 之后查了一篇国家集训队的论文<最小割模型在信息学竞赛中的应用>才明白原来是最大权闭合图转的最小割,至于为什么能转成最小割,自己去看论文的证明吧

HDU 3987 Harry Potter and the Forbidden Forest(最小割中的最少割边)经典

Harry Potter and the Forbidden Forest Time Limit: 5000/3000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 1791    Accepted Submission(s): 596 Problem Description Harry Potter notices some Death Eaters try to slip

【BZOJ1266】【AHOI2006】上学路线route 最短路建图转最小割

题解: 首先那个裸的单源最短路过程就过了吧. 然后说转的最小割. 就是我们考虑到从源点到汇点有多条最短路,我们需要切断一些边,使得所有的最短路都被切断. 首先这是个很裸的模型,切断?最小割! 如果你想不到,那不妨这么想: 我们切断所有最短路,那么每条最短路都有一个路径,上面有若干条边,那么我们需要至少切断其中的一部分. 而所有的局部最短路都满足一个性质: 就是从源点到某点的最短路长度固定(这个很显然吧,都"最"短了) 那么我们让所有的最短路径都被切断,就会使得一些在最短路径之一的边被切

OpenJudge 3765(最大权闭合图,最小割

题目大意:需要解雇若干人,解雇每个人有一个损失或收益,解雇一个人就必须结果他的所有下属,问最大收益. 思路:裸最大权闭合图问题,对于权值为q的点,负权向汇点连-p的边,正权源点向其连p的边,上下级关系上级向下级连oo的边,然后求最大流最小割,最大收益即为总正点权减最小割,需要裁员的人即为残余图中从源点可达的点. 参考:http://blog.csdn.net/scorpiocj/article/details/6085637 #include <bits/stdc++.h> #define p

HDU 3046 Pleasant sheep and big big wolf(最小割)

HDU 3046 Pleasant sheep and big big wolf 题目链接 题意:一个n * m平面上,1是羊,2是狼,问最少要多少围墙才能把狼全部围住,每有到达羊的路径 思路:有羊和狼,要分成两个集合互不可达,显然的最小割,建图源点连狼,容量无穷,羊连汇点,容量无穷,然后相邻格子连边,容量为1 代码: #include <cstdio> #include <cstring> #include <queue> #include <algorithm