hdu 3987 Harry Potter and the Forbidden Forest【网路流最小割模型】

Harry Potter and the Forbidden Forest

Time Limit: 5000/3000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)

Total Submission(s): 1549    Accepted Submission(s): 528

Problem Description

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

分析:题意为求最小割边中边数最小的割的割边数。方法有很多种:

    类型一:跑第一遍最大流,把残余网络中的割边(即剩余流量为0的变)赋权值为1,其余为inf,再一遍最大流即可。

    类型二:通过改变边的权值,想象如果每一条边容量都加1,那么最小割中,边数多的的割加的流量多于边数少的割的流量,那么边数多的就不是割了(流量打了)

           当然不是绝对加一,使边的权值w变为w*(E+1)+1,是最大流%(E+1),此为最小割最小边数,这样防止改变最小割的结果。

    代码示例(类型1):

    #include<stdio.h>
#include<string.h>
#include<algorithm>
#include<iostream>
#include<queue>
#define Lh 2000
#define Le 2000000
#define max 1000000000
using namespace std;
typedef struct
{
    int to;
    int w;
    int next;
}node;
typedef struct
{
    int x;
    int t;
}DEP;
node E[Le];
DEP fir,nex;
int head[Lh],headx[Lh],deep[Lh],cnt;
void init()
{
    memset(head,-1,sizeof(head));
    cnt=0;
}
void add(int a,int b,int c)
{
    E[cnt].to=b;
    E[cnt].w=c;
    E[cnt].next=head[a];
    head[a]=cnt++;
    E[cnt].to=a;
    E[cnt].w=0;
    E[cnt].next=head[b];
    head[b]=cnt++;
}
int min(int x,int y)
{
    return x<y?x:y;
}
int bfs(int s,int t,int n)
{
    memset(deep,255,sizeof(deep));
    queue<DEP>Q;
    fir.x=s;
    fir.t=0;
    deep[s]=0;
    Q.push(fir);
    while(!Q.empty())
    {
        fir=Q.front();
        Q.pop();
        for(int i=head[fir.x];i!=-1;i=E[i].next)
        {
            nex.x=E[i].to;
            nex.t=fir.t+1;
            if(deep[nex.x]!=-1||!E[i].w)
            continue;
            deep[nex.x]=nex.t;
            Q.push(nex);
        }
    }
    for(int i=0;i<=n;i++)
    headx[i]=head[i];
   return deep[t]!=-1;
}
int dfs(int s,int t,int flow)
{
    if(s==t)
    return flow;
    int newflow=0;
    for(int i=headx[s];i!=-1;i=E[i].next)
    {   
        headx[s]=i;
        int to=E[i].to;
        int w=E[i].w;
        if(!w||deep[to]!=deep[s]+1)
        continue;
        int temp=dfs(to,t,min(w,flow-newflow));
        newflow+=temp;
        E[i].w-=temp;
        E[i^1].w+=temp;
        if(newflow==flow)
        break;
    }
    if(!newflow)deep[s]=0;
    return newflow;
}
int Dinic(int s,int t,int m)
{
    int sum=0;
    while(bfs(s,t,m))
    {   
        sum+=dfs(s,t,max);
    }
    return sum;
}
int main()
{
    int a,b,c,d;
    int n,m,T;
    scanf("%d",&T);
    for(int t=1;t<=T;t++)
    {
        scanf("%d%d",&n,&m);
        init();
        for(int i=0;i<m;i++)
        {
            scanf("%d%d%d%d",&a,&b,&c,&d);
            add(a,b,c);
            if(d)
            add(b,a,c);
        }
        Dinic(0,n-1,n);
        for(int i=0;i<=cnt;i+=2)
        {
          if(!E[i].w)
          E[i].w=1;
          else
          E[i].w=max;
        }
        printf("Case %d: %d\n",t,Dinic(0,n-1,n));
    }
    return 0;
}
    
    
时间: 2024-10-20 17:32:46

hdu 3987 Harry Potter and the Forbidden Forest【网路流最小割模型】的相关文章

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

view code//hdu 3987 #include <iostream> #include <cstdio> #include <algorithm> #include <cstring> #include <queue> using namespace std; typedef long long ll; const ll INF = 1LL<<59; const ll E = 100001; const int N = 10

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

Harry Potter and the Forbidden Forest Time Limit: 5000/3000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 1791    Accepted Submission(s): 596 Problem Description Harry Potter notices some Death Eaters try to slip

hdoj 3987 Harry Potter and the Forbidden Forest 【求所有最小割里面 最少的边数】

Harry Potter and the Forbidden Forest Time Limit: 5000/3000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 1802    Accepted Submission(s): 602 Problem Description Harry Potter notices some Death Eaters try to slip

Harry Potter and the Forbidden Forest(割边最小的最小割)

Harry Potter and the Forbidden Forest Time Limit:3000MS    Memory Limit:65536KB    64bit IO Format:%I64d & %I64u Description Harry Potter notices some Death Eaters try to slip into Castle. The Death Eaters hide in the most depths of Forbidden Forest.

HDU 3046Pleasant sheep and big big wolf(网络流之最小割)

题目地址:HDU 3046 最小割第一发!其实也没什么发不发的...最小割==最大流.. 入门题,但是第一次入手最小割连入门题都完全没思路...sad..对最小割的本质还是了解的不太清楚.. 这题就是对每两个相邻的格子的边界都要进行加边,然后求最大流就OK了. RE了好长时间,注意遍历加边的时候要从1开始,而不是0开始,因为0是源点的...(也许只有我才犯这种错误吧...)建图不多说了..只要了解了最小割,建图还是很容易想的. 代码如下: #include <iostream> #includ

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

方法1:两遍最大流.一遍最大流后,把满流边容量+1,非满流边改为INF:再求最小割即为答案. 我大概想了下证明:能构成最小割的边在第一次跑最大流时都满流,然后按那样改变边容量再求一次最小割,就相当于再在那些 满流可能是属于最小割的边 中挑出最少的边形成ST割. 1 #include<cstdio> 2 #include<cstring> 3 #include<queue> 4 #include<algorithm> 5 using namespace std

【HDU 5855】Less Time, More profit(网络流、最小割、最大权闭合子图)

Less Time, More profit Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Problem Description The city planners plan to build N plants in the city which has M shops. Each shop needs products from some plants to make p

HDU 5294 多校第一场1007题 最短路+最小割

Tricks Device Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 1285    Accepted Submission(s): 302 Problem Description Innocent Wu follows Dumb Zhang into a ancient tomb. Innocent Wu’s at the ent

2019 HDU 多校赛第二场 HDU 6598 Harmonious Army 构造最小割模型

题意: 有n个士兵,你可以选择让它成为战士还是法师. 有m对关系,u和v 如果同时为战士那么你可以获得a的权值 如果同时为法师,你可以获得c的权值, 如果一个为战士一个是法师,你可以获得b的权值 问你可以获得的最大权值是多少? 题解: 对每个士兵建立一个点x ,点x 向源点s 连一条边,向汇点t 连一条边, 分别表示选择两种职业,然后就可以先加上所有的贡献,通过两点关系用 最小割建模,如下图所示 设一条边的三种贡献为A, B, C,可以得到以下方程: 如果x,y都是法师,你可以获得C的权值,但是