Harry Potter and the Forbidden Forest (minimum cut of cutting edge)

Source: Internet
Author: User

Harry Potter and the Forbidden ForestTime limit:MS Memory Limit:65536KB 64bit IO Format:%i64d &A mp %i64u

Description

Harry Potter notices some Death Eaters try to slip into Castle. The Death Eaters hide in the 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 is blocked at least.

Input

Input consists of several test cases.

The first line was number of test case.

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

Following m lines contains information about edges. Each of the line have four integers u, V, C, D. The first and integers mean and 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 is blocked at least.

Sample Input

Sample Output

Case 1:3case 2:2case 3:2 test instructions: Give a mixed graph, ask for s can not reach T, at least which edges should be deleted, so that the deletion of the edge of the Benquan and the smallest, and requires the deletion of the number of edges to a minimum; a typical minimum cut problem, but the topic in the most On the basis of a small cut to add a condition, is to require cutting edge as small as possible. First we can find the maximum flow of the mixed graph, the minimum cut equals the maximum flow, and then for the cutting edge processing, you can do so. Add 1 to the Benquan of each side, then run the maximum flow once. This can find the cutting edge of the smallest cut set, because the more cutting edge, the smallest cut will be larger, assuming there is cut edge smaller cut set, then you run the maximum flow will run out of smaller traffic out. Finally, the result of direct two-time maximum flow is the answer; AC Code:
/* ***********************************************author:xdlovecreated time:2015 July 31 Friday 12:37 29 seconds file Name : A.cpp ************************************************ * * #include <stdio.h> #include <string.h># Include <iostream> #include <algorithm> #include <vector> #include <queue> #include <set> #include <map> #include <string> #include <math.h> #include <stdlib.h> #include <time.h>  The using namespace std;/** macro defines the class * **/#define for (i,s,t) for (int i = (s); I < (t), i++) #define FOR_REV (i,s,t) for (int i = (s) -1); I >= (t); i--) #define MID ((L + R) >> 1) #define Lson l,mid,u<<1#define rson mid+1,r,u<<1|1#define ls u<<1#de Fine Rs u<<1|1typedef Long long ll;typedef unsigned long long ull;const int INF = 0x3f3f3f3f;const double pi = ACOs (            -1.0);/** input Output Hang class template * **/class fast{public:inline Void Rd (int &ret) {char C;            int sgn; if (c = getcHar (), c = = EOF) return;            while (c = '-'-' && (C < ' 0 ' | | c > ' 9 ')) C = GetChar (); SGN = (c = = '-')?            -1:1; ret = (c = = '-')?            0: (C-' 0 ');            while (c = GetChar (), C >= ' 0 ' && C <= ' 9 ') ret = RET * + C-' 0 ';        RET *= SGN;                } public:inline void pt (int x) {if (x < 0) {Putchar ('-');            x =-X;            } if (x > 9) pt (X/10);        Putchar (x 10 + ' 0 '); }};    Fast in,out;const int maxn = 1e3 + 5;const int MAXM = 1e5 * 2 + 5;struct node{int from,to,next; int cap;} EDGE[MAXM * 2];int tol;int head[maxn];int que[maxn];int DEP[MAXN];    DEP is the level of the point int stack[maxn];//stack to the stack, storing the current augmented path int cur[maxn];//to store the current point's subsequent void Init () {tol = 0; memset (head,-1,sizeof (Head));}    void Add_edge (int u, int v, int w) {edge[tol].from = u;    Edge[tol].to = v;    Edge[tol].cap = W;    Edge[tol].next = Head[u]; Head[u] = tol++;    Edge[tol].from = v;    edge[tol].to = u;    Edge[tol].cap = 0;    Edge[tol].next = Head[v]; HEAD[V] = tol++;}    int BFS (int start, int end) {int front, rear;    Front = rear = 0;    memset (DEP,-1, sizeof (DEP));    que[rear++] = start;    Dep[start] = 0;        while (front! = rear) {int u = que[front++];        if (front = = MAXN) Front = 0;            for (int i = head[u]; i =-1; i = edge[i].next) {int v = edge[i].to;                if (Edge[i].cap > 0 && dep[v] = =-1) {Dep[v] = Dep[u] + 1;                que[rear++] = v;                if (rear >= maxn) rear = 0;            if (v = = end) return 1; }}} return 0;}    int dinic (int start, int end) {int res = 0;    int top;        while (BFS (start, end)) {memcpy (cur, head, sizeof (head));        int u = start;        top = 0;       while (true) {if (U = = end) {int min = INF;         int Loc;                        for (int i = 0; i < top; i++) if (min > Edge[stack[i]].cap) {                        min = Edge[stack[i]].cap;                    loc = i;                    } for (int i = 0; i < top; i++) {edge[stack[i]].cap = min;                Edge[stack[i] ^ 1].cap + = min;                } res + = min;                top = loc;            u = edge[stack[top]].from; } for (int i = cur[u]; i =-1; cur[u] = i = edge[i].next) if (edge[i].cap! = 0 && Dep[u            ] + 1 = = dep[edge[i].to]) break;                if (cur[u]! =-1) {stack[top++] = Cur[u];            u = edge[cur[u]].to;                } else {if (top = = 0) break;                Dep[u] =-1;            u = edge[stack[--top]].from; }}} return Res;}    struct rnode{int u,v,c,d;    Rnode (int u,int v,int c,int D): U (U), V (v), C (c), D (d) {} rnode () {}}p[maxm];void creatgraph (int m) {Init ();       For (i, 0, m) {Add_edge (p[i].u,p[i].v,p[i].c + 1);    if (P[I].D) Add_edge (p[i].v,p[i].u,p[i].c + 1);    }}int Main () {//freopen ("In.txt", "R", stdin);    Freopen ("OUT.txt", "w", stdout);    int t,cnt = 0;    cin>>t;        while (t--) {int n,m;        Init ();        scanf ("%d%d", &n,&m);            For (i, 0, m) {int u,v,c,d;            IN.RD (U), In.rd (v), IN.RD (c), In.rd (d);            Add_edge (U,V,C);            if (d) Add_edge (V,U,C);        P[i] = Rnode (u,v,c,d);        } int ans = dinic (0,n-1);        cout<<ans<<endl;        Creatgraph (m);    printf ("Case%d:%d\n", ++cnt,dinic (0,n-1)-ans); } return 0;}


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Harry Potter and the Forbidden Forest (minimum cut of cutting edge)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.