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

4人阅读 评论(0) 收藏 举报
分类:

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;
}
查看评论

hdu 3987(最小割的边数)

题意:给出一张有n个点的图,有的边又向,有的边无向,现在要你破坏一些路,使得从点0无法到达点n-1。破坏每条路都有一个代价。求在代价最小的前提下,最少需要破坏多少条道路。(就是说求在最小割的前提下,最...
  • hexianhao
  • hexianhao
  • 2016-05-31 11:32:56
  • 1627

HDU 3987 Harry Potter and the Forbidden Forest(最小割的最少割边数)

题目地址 题意:给你一个图,你希望0与n-1不能连通,你就需要删边了,每条边删除是需要代价的,问你在最少的删边代价下,删边个数最小是多少。 思路:有两种方法: 1、你需要把每条边删除的代价加上一个...
  • xjh_shin
  • xjh_shin
  • 2017-08-26 01:04:51
  • 320

【HDU】3987 Harry Potter and the Forbidden Forest 最小割

Harry Potter and the Forbidden Forest Time Limit: 5000/3000 MS (Java/Others)    Memory Limit: 65536...
  • u013368721
  • u013368721
  • 2014-07-07 17:04:16
  • 523

【最小割】HDU 3987 Harry Potter and the Forbidden Forest

得到的最小割得到sum  sum/
  • u012749539
  • u012749539
  • 2014-08-09 22:38:48
  • 567

hdu 3987 Harry Potter and the Forbidden Forest 最小割

题意:给定一个有向图,伏地魔在0号节点。城堡在n-1号节点。拆掉每一条路有一些代价,现在询问需要最少代价拆掉最少的边是多少。输出边数。 模型: 最小割找最少边。 状态压缩。。 代码: ...
  • CHCXCHC
  • CHCXCHC
  • 2014-12-02 21:55:56
  • 571

hdu 3987 Harry Potter and the Forbidden Forest (最小割)

Harry Potter and the Forbidden Forest Time Limit: 5000/3000 MS (Java/Others)    Memory Limit: 65536...
  • clover_hxy
  • clover_hxy
  • 2017-01-15 17:42:27
  • 278

HDU 3987 Harry Potter and the Forbidden Forest 最小割

/* 很经典的最大流最小割的题目 题意:求最小割,但因为最小割是不唯一的,题目要求得到最小割的条件下使得割边最少 搜到usaco类似的一个题目才出的,构造很巧妙 建边的时候每条边权 w=w*(E+1...
  • wsniyufang
  • wsniyufang
  • 2011-08-31 22:08:49
  • 771

HDU 3987 Harry Potter and the Forbidden Forest(最小割中的最少割边)经典

Harry Potter and the Forbidden Forest Time Limit: 5000/3000 MS (Java/Others)    Memory Limit: 65536...
  • u010372095
  • u010372095
  • 2015-08-02 21:04:49
  • 739

HDU 3987 Harry Potter and the Forbidden Forest (求割边最少的最小割)

此题与POJ3204值得对比一下,POJ3204是求割边,既关键边的数量,每一条关键边都会影响到最小割。而此题是求满足最小割的情况下,割边最要最少,求出最少的割边数量。做法是先跑一遍最大流,得到残留网...
  • acbron
  • acbron
  • 2013-05-07 15:45:52
  • 505

hdu 3987 Harry Potter and the Forbidden Forest 求割边最少的最小割

题意:     求最小割,但因为最小割是不唯一的,题目要求得到最小割的条件下,使得割边最少,输出最少割边数 思路:     有两种做法,但本质是一样的   第一种:     建边的时候每条边...
  • luyuncheng
  • luyuncheng
  • 2012-09-01 13:03:54
  • 684
    个人资料
    持之以恒
    等级:
    访问量: 7万+
    积分: 6565
    排名: 4556
    传送门
    最新评论