【hdu 3987】Harry Potter and the Forbidden Forest

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

Description

给出一张有n个点的图,有的边又向,有的边无向,现在要你破坏一些路,使得从点0无法到达点n-1。破坏每条路都有一个代价。求在代价最小的前提下,最少需要破坏多少条道路。(就是说求在最小割的前提下,最小的割边数)

Solution

我们先在原图上跑一次最大流;
求出跑完最大流之后的剩余网络.
显然,最后剩余网络上容量变成0的(也就是满流的边);
它才可能是最小割的边.
接下来;
把那些可能是最小割的边的边的容量重新赋值为1;
然后,其余边都赋值成INF;
再跑一遍最大流;
求出此时的最小割.就是答案了.
也即最小割边数目.

NumberOf WA

0

Reviw

最小割模型

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 = 1000;
const int M = 4e5;
const LL INF = 1e18;

int n,m,en[M+10],fir[N+10],tfir[N+10],nex[M+10],totm;
int deep[N+10];
LL flow[M+10];
queue <int> dl;

void add(int x,int y,int z){
    nex[totm] = fir[x];
    fir[x] = totm;
    flow[totm] = z;
    en[totm] = y;
    totm++;

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

bool bfs(){
    ms(deep,255);
    dl.push(1);
    deep[1] = 0;
    while (!dl.empty()){
        int x = dl.front();
        dl.pop();
        for (int i = fir[x];i != -1;i = nex[i]){
            int y = en[i];
            if (flow[i] >0 && deep[y]==-1){
                deep[y] = deep[x] + 1;
                dl.push(y);
            }
        }
    }
    return deep[n]!=-1;
}

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

int main(){
    //Open();
    //Close();
    int T,kk = 0;
    ri(T);
    while (T--){
        ms(fir,255);
        totm = 0;
        ri(n),ri(m);
        rep1(i,1,m){
            int x,y,z,d;
            ri(x),ri(y),ri(z),ri(d);
            x++,y++;
            add(x,y,z);
            if (d == 1) add(y,x,z);
        }
        while ( bfs() ) {
            rep1(i,1,n) tfir[i] = fir[i];
            dfs(1,INF);
        }

        for (int i = 0;i < totm;i+=2)
            if (flow[i]==0){
                flow[i] = 1;
                flow[i^1] = 0;
            }else{
                flow[i] = INF;
                flow[i^1] = 0;
            }

        LL ans = 0;

        while ( bfs() ) {
            rep1(i,1,n) tfir[i] = fir[i];
            ans+=dfs(1,INF);
        }
        os("Case ");oi(++kk);os(": ");ol(ans);puts("");
    }
    return 0;
}
posted @ 2017-10-04 18:44  AWCXV  阅读(63)  评论(0编辑  收藏  举报