HDU3987: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): 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
  
  
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
 

Author
aMR @ WHU
 

Source
题意:给个图,求出最小割下的最少割边数。

思路:首先最小割=最大流,如何求最少边数呢?原最小割为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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值