hdu3987 Harry Potter and the Forbidden Forest 最小割边数

题目:

http://acm.hdu.edu.cn/showproblem.php?pid=3987

题意:

给一个网络,边有有向边和无向边两种,挑选一些边破坏,使得从0无法到达n-1,每条边都有一个破环的成本,求在成本最低的情况下需要破坏的最少边数

思路:

明显最小成本就是最小割,求边数的话,扩大边权然后取余即可

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
const int N = 1000 + 10;
const ll INF = 0x3f3f3f3f3f3f3f3f;
struct edge
{
    int to, next;
    ll cap;
}g[N*N*2];
int level[N], cur[N], pre[N], gap[N];
int cnt, head[N];
int nv;
void init()
{
    cnt = 0;
    memset(head, -1, sizeof head);
}
void add_edge(int v, int u, ll cap)
{
    g[cnt].to = u, g[cnt].cap = cap, g[cnt].next = head[v], head[v] = cnt++;
    g[cnt].to = v, g[cnt].cap = 0, g[cnt].next = head[u], head[u] = cnt++;
}
ll sap(int s, int t)
{
    memset(level, 0, sizeof level);
    memset(gap, 0, sizeof gap);
    memcpy(cur, head, sizeof head);
    gap[0] = nv;
    int v = pre[s] = s;
    ll flow = 0, aug = INF;
    while(level[s] < nv)
    {
        bool flag = false;
        for(int &i = cur[v]; i != -1; i = g[i].next)
        {
            int u = g[i].to;
            if(g[i].cap > 0 && level[v] == level[u] + 1)
            {
                flag = true;
                pre[u] = v;
                v = u;
                aug = min(aug, g[i].cap);
                if(v == t)
                {
                    flow += aug;
                    while(v != s)
                    {
                        v = pre[v];
                        g[cur[v]].cap -= aug;
                        g[cur[v]^1].cap += aug;
                    }
                    aug = INF;
                }
                break;
            }
        }
        if(flag) continue;
        int minlevel = nv;
        for(int i = head[v]; i != -1; i = g[i].next)
        {
            int u = g[i].to;
            if(g[i].cap > 0 && level[u] < minlevel)
                minlevel = level[u], cur[v] = i;
        }
        if(--gap[level[v]] == 0) break;
        level[v] = minlevel + 1;
        gap[level[v]]++;
        v = pre[v];
    }
    return flow;
}
int main()
{
    int t, n, m, cas = 0;
    scanf("%d", &t);
    while(t--)
    {
        init();
        scanf("%d%d", &n, &m);
        int a, b, c, d;
        for(int i = 1; i <= m; i++)
        {
            scanf("%d%d%d%d", &a, &b, &c, &d);
            a++, b++;
            add_edge(a, b, 1LL*c*(m+1) + 1);
            if(d) add_edge(b, a, 1LL*c*(m+1) + 1);//边权扩大(m+1)倍再+1
        }
        int ss = 1, tt = n;
        nv = n;
        printf("Case %d: %lld\n", ++cas, sap(ss, tt) % (m+1));
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值