【hdu 4289】Control

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

Description

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

Solution

最小割问题.
根据最大流最小割定理;
跑一次最大流即可;
因为是去掉点;
所以,把每个点转换成2个点;
2个点之间建一条边,容量为删掉它的花费.
这两个泛化出来的点,一个点作为原本点的“进点”,另一个作为“出点”,然后和其他点的边,容量都改成INF即可.
这样割就变成割那些泛化出来的点之间的不是INF的边了.
用dicnic算法搞.

NumberOf WA

10

Reviw

网络在建边的时候,要建双向边.
EK算法有负向边的.

Code

#include <bits/stdc++.h>
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define LL long long
#define rep1(i,a,b) for (int i = a;i <= b;i++)
#define rep2(i,a,b) for (int i = a;i >= b;i--)
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define ms(x,y) memset(x,y,sizeof x)
#define ri(x) scanf("%d",&x)
#define rl(x) scanf("%lld",&x)
#define rs(x) scanf("%s",x+1)
#define oi(x) printf("%d",x)
#define ol(x) printf("%lld",x)
#define oc putchar(' ')
#define os(x) printf(x)
#define all(x) x.begin(),x.end()
#define Open() freopen("F:\rush.txt","r",stdin)
#define Close() ios::sync_with_stdio(0)

typedef pair<int,int> pii;
typedef pair<LL,LL> pll;

const int dx[9] = {0,1,-1,0,0,-1,-1,1,1};
const int dy[9] = {0,0,0,-1,1,-1,1,-1,1};
const double pi = acos(-1.0);
const int N = 200;
const int M = 2e4;
const LL INF = 1e18;

struct abc{
    int en,nex;
    LL flow;
};

int n,m,s,t,cost[N+10],deep[N*2+20];
int fir[N*2+10],tfir[N*2+10],totm;
abc bian[M*4 + N * 2 + 10];
queue <int> dl;

void add(int x,int y,LL cost){
    bian[totm].nex = fir[x];
    fir[x] = totm;
    bian[totm].en = y,bian[totm].flow = cost;
    totm++;

    bian[totm].nex = fir[y];
    fir[y] = totm;
    bian[totm].en = x,bian[totm].flow = 0;
    totm++;
}

bool bfs(int s,int t){
    dl.push(s);
    ms(deep,255);
    deep[s] = 0;

    while (!dl.empty()){
        int x = dl.front();
        dl.pop();
        for (int temp = fir[x]; temp!= -1 ;temp = bian[temp].nex){
            int y = bian[temp].en;
            if (deep[y]==-1 && bian[temp].flow){
                deep[y] = deep[x] + 1;
                dl.push(y);
            }
        }
    }
    return deep[t]!=-1;
}

LL dfs(int x,int t,LL limit){
    if (x == t) return limit;
    if (limit == 0) return 0;
    LL cur,f = 0;
    for (int temp = tfir[x];temp!=-1;temp = bian[temp].nex){
        tfir[x] = temp;
        int y = bian[temp].en;
        if (deep[y] == deep[x] + 1 && (cur = dfs(y,t,min(limit,(LL)bian[temp].flow))) ){
            f += cur;
            limit -= cur;
            bian[temp].flow -= f;
            bian[temp^1].flow += f;
            if (!limit) break;
        }
    }
    return f;
}

int main(){
    //Open();
    //Close();
    while (~ri(n)){
        ri(m);
        ri(s),ri(t);

        totm = 0;
        rep1(i,1,2*N+5) fir[i] = -1;

        rep1(i,1,n) {
            ri(cost[i]);
            add(2*i-1,2*i,cost[i]);
        }

        rep1(i,1,m){
            int x,y;
            ri(x),ri(y);
            add(2*x,2*y-1,INF);
            add(2*y,2*x-1,INF);
        }

        s = 2*s-1,t = 2*t;
        LL ans = 0;

        while ( bfs(s,t) ){
            rep1(i,1,2*n) tfir[i] = fir[i];
            ans += dfs(s,t,INF);
        }

        ol(ans);puts("");
    }
    return 0;
}
原文地址:https://www.cnblogs.com/AWCXV/p/7626126.html