【HDU - 3987】Harry Potter and the Forbidden Forest 【最小割 -- 最小化割边数】

Harry Potter notices some Death Eaters try to slip into Castle. The Death Eaters hide in the most depths of Forbidden Forest. Harry need stop them as soon as.

The Forbidden Forest is mysterious. It consists of N nodes numbered from 0 to N-1. All of Death Eaters stay in the node numbered 0. The position of Castle is node n-1. The nodes connected by some roads. Harry need block some roads by magic and he want to minimize the cost. But it’s not enough, Harry want to know how many roads are blocked at least.
Input
Input consists of several test cases.

The first line is number of test case.

Each test case, the first line contains two integers n, m, which means the number of nodes and edges of the graph. Each node is numbered 0 to n-1.

Following m lines contains information about edges. Each line has four integers u, v, c, d. The first two integers mean two endpoints of the edges. The third one is cost of block the edge. The fourth one means directed (d = 0) or undirected (d = 1).

Technical Specification

  1. 2 <= n <= 1000
  2. 0 <= m <= 100000
  3. 0 <= u, v <= n-1
  4. 0 < c <= 1000000
  5. 0 <= d <= 1
    Output
    For each test case:
    Output the case number and the answer of how many roads are blocked at least.
    Sample Input
    3

4 5
0 1 3 0
0 2 1 0
1 2 1 1
1 3 1 1
2 3 3 1

6 7
0 1 1 0
0 2 1 0
0 3 1 0
1 4 1 0
2 4 1 0
3 5 1 0
4 5 2 0

3 6
0 1 1 0
0 1 2 0
1 1 1 1
1 2 1 0
1 2 1 0
2 1 1 1
Sample Output
Case 1: 3
Case 2: 2
Case 3: 2

分析: 最小割的裸题,但是要求最小割的 最小割边数,最最小割大小一定的情况下,有时候会出现割的边数不一样的情况。
注意: 在残余网络中,满流边一定是 一个割或者多个割的割边。
如果我们将 满流的正向边容量改为1,不满流 正向边改为inf,这样我们再跑一次,最大流,岂不就是 最小割情况下的最小割(而最小割的最小割的意义 不就是最小割的最小边数)。
代码

#include<cstdio>
#include<cstring>
#include<iostream>
#include<queue>
#include<algorithm>
using namespace std;
#define LL long long

const int N = 1000+11;
const int M = 100000*4+11;
const int inf = 0x3f3f3f3f;

struct Edge {
    int form,to,cap,flow,nexts;
}edge[M];
int head[N],top;
void init(){
    memset(head,-1,sizeof(head));
    top=0;
}
void addedge(int a,int b,int c){
    Edge e={a,b,c,0,head[a]};
    edge[top]=e;head[a]=top++;

    Edge ee={b,a,0,0,head[b]};
    edge[top]=ee;head[b]=top++;
}
int vis[N],dis[N];
int cur[N];
bool bfs(int st,int ed){
    queue<int>Q;
    memset(vis,0,sizeof(vis));
    memset(dis,-1,sizeof(dis));
    Q.push(st);vis[st]=1;dis[st]=1;
    while(!Q.empty()){
        int now=Q.front();Q.pop();
        for(int i=head[now];i!=-1;i=edge[i].nexts){
            Edge e=edge[i];
            if(!vis[e.to]&&e.cap-e.flow>0){
                vis[e.to]=1;
                dis[e.to]=dis[now]+1;
                if(e.to==ed) return 1;
                Q.push(e.to);
            }
        }
    }
    return 0;
}
int dfs(int now,int a,int ed){
    if(a==0||now==ed) return a;
    int flow=0,f;
    for(int &i=cur[now];i!=-1;i=edge[i].nexts){
        Edge &e=edge[i];
        if(dis[e.to]==dis[now]+1&&(f=dfs(e.to,min(e.cap-e.flow,a),ed))>0){
            e.flow+=f;
            flow+=f;
            edge[i^1].flow-=f;
            a-=f;
            if(a==0) break;
        }
    }
    return flow;
}

int max_flow(int st ,int ed){
    int flow=0;
    while(bfs(st,ed)){
        memcpy(cur,head,sizeof(head));
        flow+=dfs(st,inf,ed);
    }
    return flow;
}


int main(){
    int t;scanf("%d",&t); int cas=1;
    while(t--){
        init();
        int n,m;scanf("%d%d",&n,&m);
        int a,b,c,d;
        while(m--){
            scanf("%d%d%d%d",&a,&b,&c,&d);
            addedge(a,b,c);
            if(d) addedge(b,a,c);
        }
        max_flow(0,n-1);
        for(int i=0;i<top;i+=2){
            if(edge[i].cap==edge[i].flow) {
                edge[i].cap=1 ;edge[i].flow=0;
            }else {
                edge[i].cap=inf; edge[i].flow=0;
            }
            edge[i^1].cap=edge[i^1].flow=0;  // 注意清空反向边
        }
        printf("Case %d: %d\n",cas++,max_flow(0,n-1));
    }
return 0;
}
阅读更多
版权声明:将来的你一定会感谢现在努力的你!!!! https://blog.csdn.net/qq_37383726/article/details/79974252
个人分类: 网络流
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭
关闭