HDU-3987 Harry Potter and the Forbidden Forest(最大流)

题目:http://acm.hdu.edu.cn/showproblem.php?pid=3987
题意:求边数最少的割集
思路:与上文一样
代码:

#include<bits/stdc++.h>
using namespace std;

typedef long long ll;
const ll INF = 1e18;
const int N = 500005;
int n,m,s,t,tot;
int head[N],dep[N],cur[N];

struct edge
{
    int to,nxt;
    ll cap;
    edge(int t = 0,int n = 0,ll c = 0):to(t),nxt(n),cap(c){}
}E[N];

void init()
{
    memset(head,-1,sizeof(head));
    tot = 0;
}
void add(int u,int v,ll cap)
{
    E[tot] = edge(v,head[u],cap);
    head[u] = tot++;
}
bool bfs(int s,int t)
{
    memset(dep,-1,sizeof(dep));
    queue<int> q;
    dep[s] = 0;
    q.push(s);
    while(!q.empty())
    {
        int u = q.front();q.pop();
        for(int i = head[u];~i;i = E[i].nxt)
        {
            int v = E[i].to;
            if(E[i].cap > 0 && dep[v] == -1)
            {
                dep[v] = dep[u] + 1;
                q.push(v);
            }
        }
    }
    return dep[t] != -1;
}
ll dfs(int u,ll flow)
{
    if(u == t)
        return flow;
    ll w ,used = 0;
    for(int i = head[u];~i;i = E[i].nxt)
    {
        int v = E[i].to;
        if(dep[v] == dep[u]+1)
        {
            w = flow - used;
            w = dfs(v,min(w,E[i].cap));
            E[i].cap -= w;
            E[i^1].cap += w;
            if(v) cur[u] = i;
            used += w;
            if(used == flow) return flow;
        }
    }
    if(!used) dep[u] = -1;
    return used;
}
ll dinic(int s,int t)
{
    ll res = 0;
    while(bfs(s,t))
    {
         for(int i = 1;i <= t;i++)
            cur[i] = head[i];
         res += dfs(s,INF);
    }
    return res;
}
int main()
{
    int T;
    scanf("%d",&T);
    for(int ca = 1;ca <= T;ca++)
    {
        init();
        scanf("%d%d",&n,&m);
        s = 1,t = n;
        int u,v,d;
        ll w;
        while(m--)
        {
            scanf("%d%d%lld%d",&u,&v,&w,&d);
            ++u;++v;
            add(u,v,w*1000000+1);
            if(d)
                add(v,u,w*1000000+1);
            else
                add(v,u,0);
        }
        printf("Case %d: %lld\n",ca,dinic(s,t)%1000000);
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值