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

版权声明:将来的你一定会感谢现在努力的你!!!! https://blog.csdn.net/qq_37383726/article/details/79974252

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;
}
阅读更多
换一批

Harry Potter and the Binary Kingdom

02-13

问题描述 :nnnAfter defeating the Voldemort, Harry is enjoying his senior year in Hogwarts with his friends. They decide to make a graduation trip, using their newest magical broom. In the journey, Harry is trapped in a Kingdom named Binary Kingdom, maybe because the smart but isolated people still have no idea of the great reputation of this young wizard. Loving numbers and binary very much, the subjects of Binary Kingdom claim only Harry solve the problem can he leave there.nHarry Potter and J.K.Rowlingn nThe problem begins with an empty number multi-set, with six kinds of operation:nn1. INS M: insert M into the setn2. DEL M: delete all numbers equal to M if any one exists; otherwise report “Del error”n3. ADD M: increase the value of every number by Mn4. SUB M: decrease the value of every number by M if after decreasing no number will be less than zero; otherwise report “Sub error”n5. QNUM M: query how many numbers equal to M in the setn6. QBIT M: query how many numbers whose M-th bit is 1 in its binary, M is counting from lower to higher, beginning with 0nnnThough our brave Harry Potter can proficient in many fields, he is not good at math, now, it’s your time. nn输入:nnnThe first line contains a single integer T, indicating the number of test cases.nEach test case begins with an integer M, following M operations. nnTechnical Specificationnnn1. 1 <= T <= 12n2. 1 <= M <= 100 000n3. The QBIT operations’ M will in the range [0, 17], the other five operations’ M in the range [0, 1 000 000 000]. nn输出:nnnThe first line contains a single integer T, indicating the number of test cases.nEach test case begins with an integer M, following M operations. nnTechnical Specificationnnn1. 1 <= T <= 12n2. 1 <= M <= 100 000n3. The QBIT operations’ M will in the range [0, 17], the other five operations’ M in the range [0, 1 000 000 000].nn样例输入:nn1n10nINS 1nINS 2nDEL 1nINS 3nDEL 1nADD 1nSUB 4nQNUM 2nQBIT 1nQBIT 2n样例输出:nnCase 1:nDel errornSub errorn0n1n1

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