hdu4289——Control(最大流最小割+SAP)

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

题意就是给一个无向图,去掉每个点有费用,求至少需要多少费用才能使起点到终点不连通。
一开始我很困惑,为什么求的是最少费用却要用最大流?又翻了翻别的解题报告,看到最小割三个字就恍然大悟,立马想到算法导论上讲最大流的图中每个割都是最小割,并且这个最小割在数值上等于最大流。看来只看书是没用的,还得联系实际做题。
还有就是又深入了解了下模板,有一些地方还是没注意。

#include <iostream>
#include <cstring>
#include <string>
#include <vector>
#include <queue>
#include <cstdio>
#include <set>
#include <cmath>
#include <map>
#include <algorithm>
#define INF 0x3f3f3f3f
#define MAXN 11000
#define Mod 10001
using namespace std;
struct E
{
    int to, frm, nxt, cap;
}edge[MAXN<<5];
int head[MAXN], e, n, m, src, des;
int dep[MAXN], gap[MAXN]; //gap[x]=y:说明残留网络中dep[i]=x的个数为y
void addedge(int u, int v, int c)
{
    edge[e].frm = u;
    edge[e].to = v;
    edge[e].cap = c;
    edge[e].nxt = head[u];
    head[u] = e++;
    edge[e].frm = v;
    edge[e].to = u;
    edge[e].cap = 0;
    edge[e].nxt = head[v];
    head[v] = e++;
}
int Q[MAXN];
void BFS(int src, int des)
{
    memset(dep, -1, sizeof(dep));
    memset(gap, 0, sizeof(gap));
    gap[0] = 1;   //说明此时有1个dep[i] = 0
    int front = 0, rear = 0;
    dep[des] = 0;
    Q[rear++] = des;
    int u, v;
    while (front != rear)
    {
        u = Q[front++];
        //cout<<u<<endl;
        front = front%MAXN;
        for (int i=head[u]; i!=-1; i=edge[i].nxt)
        {
            //cout<<i<<endl;
            v = edge[i].to;
            if (edge[i].cap != 0 || dep[v] != -1)
                continue;
            Q[rear++] = v;
            rear = rear % MAXN;
            ++gap[dep[v] = dep[u] + 1];  //求出各层次的数量
        }
    }
}
int S[MAXN],cur[MAXN];
int SAP()
{
    int res = 0;
    BFS(src, des);
    int  top = 0;
    memcpy(cur, head, sizeof(head));
    int u = src, i;
    while (dep[src] < n)   //n为结点的个数
    {
        if (u == des)
        {
            int temp = INF, inser = n;
            for (i=0; i!=top; ++i)
                if (temp > edge[S[i]].cap)
                {
                    temp = edge[S[i]].cap;
                    inser = i;
                }
            for (i=0; i!=top; ++i)
            {
                edge[S[i]].cap -= temp;
                edge[S[i]^1].cap += temp;
            }
            res += temp;
            top = inser;
            u = edge[S[top]].frm;
        }

        if (u != des && gap[dep[u] -1] == 0)//出现断层,无增广路
            break;
        for (i = cur[u]; i != -1; i = edge[i].nxt)//遍历与u相连的未遍历结点
            if (edge[i].cap != 0 && dep[u] == dep[edge[i].to] + 1) //层序关系, 找到允许
                break;

        if (i != -1)//找到允许弧
        {
            cur[u] = i;
            S[top++] = i;//加入路径栈
            u = edge[i].to;//查找下一个结点
        }
        else   //无允许的路径,修改标号 当前点的标号比与之相连的点中最小的多1
        {
            int min = n;
            for (i = head[u]; i != -1; i = edge[i].nxt) //找到与u相连的v中dep[v]最小的点
            {
                if (edge[i].cap == 0)
                    continue;
                if (min > dep[edge[i].to])
                {
                    min = dep[edge[i].to];
                    cur[u] = i;          //最小标号就是最新的允许弧
                }
            }
            --gap[dep[u]];          //dep[u] 的个数变化了 所以修改gap
            ++gap[dep[u] = min + 1]; //将dep[u]设为min(dep[v]) + 1, 同时修改相应的gap[]
            if (u != src) //该点非源点&&以u开始的允许弧不存在,退点
                u = edge[S[--top]].frm;
        }
    }
    return res;
}
int main()
{
    while(~scanf("%d%d",&n,&m))
    {
        int s,d;
        scanf("%d%d",&s,&d);
        src=s,des=d+n;
        e=0;
        memset(head,-1,sizeof(head));
        for(int i=1;i<=n;++i)
        {
            int w;
            scanf("%d",&w);
            addedge(i,i+n,w);
        }
        for(int i=1;i<=m;++i)
        {
            int a,b;
            scanf("%d%d",&a,&b);
            addedge(a+n,b,INF);
            addedge(b+n,a,INF);
        }
        n=2*n; //n为点的个数!
        int ans=SAP();
        printf("%d\n",ans);
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值