Control (对最小割容量的理解)(拆点+最大流)

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条互相相通的边,已知恐怖分子从source点将炸药运往sink点,每个点有对应的权值,问去掉几个点使得source与sink点隔开,且花费最小。

乍一眼看去我一点头绪也没有,但是想到点是有权值的,而平时遇到的都是辺有权值,因此就想能否将点变成边。而这种source与sink相隔的题一般都是求最小割。因而想到最小割的容量等于最大流。而每个点显然是只走一次的。拆点+跑最大流。

代码:

#include<iostream>
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
char x;
const int maxE = 220000;
const int maxN = 800000;
const int maxQ = 220000;

const int oo = 1e+9;
const int inf=0x3f3f3f3f;

struct Edge {
   int v;//弧尾
   int c;//容量
   int n;//指向下一条从同一个弧头出发的弧
} edge[maxE];//边组


int adj[maxN], cntE;//前向星的表头
int Q[maxQ], head, tail;//队列
int d[maxN], cur[maxN], pre[maxN], num[maxN];
int source, sink, nv;//sourse:源点,sink:汇点,nv:编号修改的上限


void add(int u, int v, int c) {//添加边
    //正向边
    edge[cntE].v = v;
    edge[cntE].c = c;//正向弧的容量为c
    edge[cntE].n = adj[u];
    adj[u] = cntE++;

    //反向边
    edge[cntE].v = u;
    edge[cntE].c = 0;//反向弧的容量为0
    edge[cntE].n = adj[v];
    adj[v] = cntE++;
}

void rev_bfs () {//反向BFS标号
    memset(num,0,sizeof(num));
    memset(d,-1,sizeof(d));//没标过号则为-1

    d[sink] = 0;//汇点默认为标过号
    num[0] = 1;
    head = tail = 0;
    Q[tail++] = sink;

    while (head != tail) {
        int u = Q[head++];
        for (int i = adj[u]; ~i; i = edge[i].n) {
            int v = edge[i].v;
            if (~d[v]) continue;//已经标过号
            d[v] = d[u] + 1;//标号
            Q[tail++] = v;
            num[d[v]]++;
        }
    }
}

int ISAP() {
    //copy (cur, adj);//复制,当前弧优化
    memcpy(cur,adj,sizeof(cur));
    rev_bfs ();//只用标号一次就够了,重标号在ISAP主函数中进行就行了
    int flow = 0, u = pre[source] = source, i;

    while (d[sink] < nv) {//最长也就是一条链,其中最大的标号只会是nv - 1,如果大于等于nv了说明中间已经断层了。
        if (u == sink) {//如果已经找到了一条增广路,则沿着增广路修改流量
            int f = oo, neck;
            for (i = source; i != sink; i = edge[cur[i]].v) {
                if (f > edge[cur[i]].c){
                    f = edge[cur[i]].c;//不断更新需要减少的流量
                    neck = i;//记录回退点,目的是为了不用再回到起点重新找
                }
            }
            for (i = source; i != sink; i = edge[cur[i]].v) {//修改流量
                edge[cur[i]].c -= f;
                edge[cur[i] ^ 1].c += f;
            }
            flow += f;//更新
            u = neck;//回退
        }
        for (i = cur[u]; ~i; i = edge[i].n) if (d[edge[i].v] + 1 == d[u] && edge[i].c) break;
        if (~i) {//如果存在可行增广路,更新
            cur[u] = i;//修改当前弧
            pre[edge[i].v] = u;
            u = edge[i].v;
        }
        else {//否则回退,重新找增广路
            if (0 == (--num[d[u]])) break;//GAP间隙优化,如果出现断层,可以知道一定不会再有增广路了
            int mind = nv;
            for (i = adj[u]; ~i; i = edge[i].n) {
                if (edge[i].c && mind > d[edge[i].v]) {//寻找可以增广的最小标号
                    cur[u] = i;//修改当前弧
                    mind = d[edge[i].v];
                }
            }
            d[u] = mind + 1;
            num[d[u]]++;
            u = pre[u];//回退
        }
    }

    return flow;
}
void init () {//初始化
    memset(adj,-1,sizeof(adj));
    cntE = 0;
}

int main()
{
    int m,n;
    int a,b;
    int cost;
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        init();
        //source=0;
        //sink=n*2+1;
        cin>>a>>b;
        source=a;
        sink=b+n;
        nv=2*n+1;
        for(int i=1;i<=n;i++)
        {
            cin>>cost;
            add(i,i+n,cost);
        }
        for(int i=1;i<=m;i++)
        {
            cin>>a>>b;
            add(a+n,b,inf);
            add(b+n,a,inf);//出来的点相连
        }
        cout<<ISAP()<<endl;
    }
    return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值