Hdu3987:harry Potter and the Forbidden Forest (min. cut edge number)

Source: Internet
Author: User
Tags cas min

Harry Potter and the Forbidden Forest Time limit:5000/3000 MS (java/others) Memory limit:65536/65536 K (java/others)
Total submission (s): 2272 Accepted Submission (s): 779


Problem 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

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 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
Author AMR @ whu
Source multi-university Training Contest 15-host by WHU Test instructions: Give a picture to find the minimum cut of the number of cutting edges.

idea: first min cut = maximum flow, how to find the minimum number of sides. The original minimum cut is a, all edge right +1 after the minimum cut to b,b-a is the answer, because the number of sides of the traffic is still limited by the number of edges. For the sake of convenience we put the edge of the right to expand K times again +1, this way to find out the smallest cut%k is the answer, K for how much is appropriate. We%k when we have to ensure that the increase in traffic after +1 <k, otherwise modulo error is meaningless, in extreme cases, the increase in traffic after +1 is the number of sides, then we set the K to the number of sides +1 on the line.

SAP from Kuangbin:

# include <iostream> # include <cstring> # include <cstdio> # include <algorithm> using namespace
Std
typedef long Long LL;
const int MAXN = 2E4;
const int MAXM = 4E5+30;
Const LL INF = 0x3f3f3f3f3f3f3f3f;
    inline void s (int &ret) {char C; ret=0; while (C=getchar ()) < ' 0 ' | |
    C> ' 9 ');
while (c>= ' 0 ' &&c<= ' 9 ') ret=ret*10+ (c ' 0 '), C=getchar ();
    } struct Edge {int to, next;
LL cap, Flow;
}EDGE[MAXM];
int tot, HEAD[MAXN], GAP[MAXN], DEP[MAXN], PRE[MAXN], CUR[MAXN];
    void init () {tot = 0;
Memset (Head,-1, sizeof (head));
    } void Add (int u, int v, LL W, ll Rw=0) {Edge[tot] = {V, head[u], W, 0};
    Head[u] = tot++;
    Edge[tot] = {u, head[v], rw, 0};
HEAD[V] = tot++;
    } LL SAP (int start, int End, int N) {memset (gap, 0, sizeof (GAP));
    memset (DEP, 0, sizeof (DEP));
    memcpy (cur, head, sizeof (head));
    int u = start;
    Pre[u] =-1;
    Gap[0] = N;
    LL ans = 0; while (Dep[start] < N) {if (U = = End) {LL Min = INF;  for (int i = pre[u];i! =-1; i = pre[edge[i^1].to]) if (min > Edge[i].cap-edge[i].flow) Min
            = Edge[i].cap-edge[i].flow;
                for (int i = pre[u];i! =-1; i = pre[edge[i^1].to]) {edge[i].flow + = Min;
            Edge[i^1].flow = Min;
            } u = start;
            Ans + = Min;
        Continue
        } bool flag = FALSE;
        int V;
            for (int i = cur[u]; I! = -1;i = edge[i].next) {v = edge[i].to;
                if (edge[i].cap-edge[i].flow && dep[v]+1 = = Dep[u]) {flag = true;
                Cur[u] = pre[v] = i;
            Break
            }} if (flag) {u = V;
        Continue
        } int Min = N; for (int i = head[u]; I! = -1;i = Edge[i].next) if (Edge[i].cap-edge[i].flow && dep[edGe[i].to] < min) {min = dep[edge[i].to];
        Cur[u] = i;
        } gap[dep[u]]--;
        if (!gap[dep[u]]) return ans;
        Dep[u] = min+1;
        gap[dep[u]]++;
    if (u! = start) u = edge[pre[u]^1].to;
} return ans;
    } int main () {int T, Cas=1, N, M, A, B, C, D;
    S (T);
        while (t--) {init ();
        S (n), S (m);
            for (int i=0; i<m; ++i) {s (a), S (b), S (c), S (d);
            Add (A, B, (LL) c* (m+1) +1);
        if (d) Add (b, A, (LL) c* (m+1) +1);
    } printf ("Case%d:%lld\n", cas++, SAP (0, n-1, N)% (m+1));
} return 0;
 }



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.