hdu4289 最小割最大流 (拆点最大流)

最小割最大流定理:(参考刘汝佳p369)增广路算法结束时,令已标号结点(a[u]>0的结点)集合为S,其他结点集合为T=V-S,则(S,T)是图的s-t最小割。

 

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 10 7.   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

 

大致题意:给出一个由n个点,m条组成的无向图,给出两个点是s,t。对于图中的每个点,去掉这个点都需要一定的花费,求至少多少花费才能使s和t之间不连通。     

思路:最基础的拆点最大流,把每个点拆作两个点i和i0,连接 I——>I0费用为去掉这个点的花费,如果原图中有一条边a和b,则连接a0和b0。(总之这四个点连完之后必须全部在环上)对图求最大流即可。

//这道题跨越了快一个月的时间,终于搞懂了,好开心—2016.9.9 ^_^。

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<queue>
using namespace std;
#define INF 0x7fffffff

struct Edge
{
    int st,ed;
    int c;
    int next;
} edge[255025];

int N,M,St,Ed;
int d[505],head[505];
int I;

void Addedge(int u,int v,int c)
{
    edge[I].st=u;
    edge[I].ed=v;
    edge[I].c=c;
    edge[I].next=head[u];
    head[u]=I++;

    edge[I].st=v;
    edge[I].ed=u;
    edge[I].c=0;
    edge[I].next=head[v];
    head[v]=I++;
}

bool bfs()
{
    memset(d,-1,sizeof(d));
    int cur;
    queue<int>q;
    d[St]=0;
    q.push(St);
    while(!q.empty())
    {
        cur=q.front();
        q.pop();
        if(cur==Ed+N) return true;
        for(int i=head[cur]; i!=-1; i=edge[i].next)
        {
            if(d[edge[i].ed]==-1 && edge[i].c>0)
            {
                d[edge[i].ed]=d[cur]+1;
                q.push(edge[i].ed);
            }
        }
    }
    return false;
}

int dinic(int n,int flow)
{
    if(n==Ed+N) return flow;
    int a,mflow=0;
    for(int i=head[n]; i!=-1; i=edge[i].next)
    {
        if(d[edge[i].ed]==d[n]+1 && edge[i].c)
        {
            a=dinic(edge[i].ed, min(flow-mflow,edge[i].c));
            edge[i].c -= a;
            edge[i^1].c+=a;
            mflow+=a;
            if(mflow==flow) break;
        }
    }
    if(mflow==0) d[n]=-1;
    return mflow;
}

int main()
{
    int a,b,x;
    while(scanf("%d%d",&N,&M)!=EOF)
    {
        scanf("%d%d",&St,&Ed);
        memset(head,-1,sizeof(head));
        I=0;
        for(int i=1; i<=N; i++)
        {
            scanf("%d",&x);
            Addedge(i,i+N,x);
        }
        for(int i=1; i<=M; i++)
        {
            scanf("%d%d",&a,&b);
            Addedge(N+a,b,INF);
            Addedge(N+b,a,INF);
        }
        int ans=0;
        while(bfs())
            ans+=dinic(St,INF);
        printf("%d\n",ans);
    }
    return 0;
}

对简单的dinic再进一步优化。

 

转载于:https://www.cnblogs.com/a-clown/p/5917705.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值