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

来源:互联网 发布:外汇数据下载 编辑:程序博客网 时间:2024/05/17 06:14

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 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
34 50 1 3 00 2 1 01 2 1 11 3 1 12 3 3 16 70 1 1 00 2 1 00 3 1 01 4 1 02 4 1 03 5 1 04 5 2 03 60 1 1 00 1 2 01 1 1 11 2 1 01 2 1 02 1 1 1
 

Sample Output
Case 1: 3Case 2: 2Case 3: 2
 

Author
aMR @ WHU
 

Source
2011 Multi-University Training Contest 15 - Host by WHU
题意:给个图,求出最小割下的最少割边数。

思路:首先最小割=最大流,如何求最少边数呢?原最小割为a,所有边权+1后跑最小割为b,b-a即是答案,因为边数多的流量仍然被边数少的限制了。为了方便起见我们把边权扩大K倍再+1,这样子求出最小割%K就是答案了,K为多少才合适呢?我们%K时得保证+1后增加的流量<K,不然取模就错误没意义了,极端情况下+1后增加的流量为边数,那么我们把K设为边数+1就行。

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;}



阅读全文
0 0