HDU 3987 Harry Potter and the Forbidden Forest 最小割

/*
很经典的最大流最小割的题目
题意:求最小割,但因为最小割是不唯一的,题目要求得到最小割的条件下使得割边最少

搜到usaco类似的一个题目才出的,构造很巧妙
建边的时候每条边权 w=w*(E+1)+1;
这样得到最大流maxflow/(E+1) 就是答案了
道理很简单,如果原先两类割边都是最小割,那么求出的最大流相等
但边权变换后只有边数小的才是最小割了,至于为什么乘的是(E+1)是为了保证边数叠加后依然是余数,不至于影响求最小割的结果
*/
#include <cstdio>
#include <iostream>
#include <memory.h>
#include<queue>
using namespace std;
const int maxn=1009;
const __int64 inf=(1LL)<<60;
typedef  __int64   LL;
//**************************************************  
//为dinic求最大流模版  
struct edge  
{   
    int v, next;   
    LL val;   
} net[ 500010 ];   
int n,m;
int level[maxn], Qu[maxn], out[maxn],next[maxn];  
class Dinic {   
public:   
    int end;  
    Dinic() {   
        end = 0;   
        memset( next, -1, sizeof(next) );   
    }   
    inline void insert( int x, int y, LL c) {   
        net[end].v = y, net[end].val = c,  
        net[end].next = next[x],   
        next[x] = end ++;   
        net[end].v = x, net[end].val = 0,  
        net[end].next = next[y],   
        next[y] = end ++;   
    }   
    bool BFS( int S, int E ) {   
        memset( level, -1, sizeof(level) );   
        int low = 0, high = 1;   
        Qu[0] = S, level[S] = 0;   
        for( ; low < high; ) {   
            int x = Qu[low];   
            for( int i = next[x]; i != -1; i = net[i].next ) {   
                if( net[i].val == 0 ) continue;   
                int y = net[i].v;   
                if( level[y] == -1 ) {   
                    level[y] = level[x] + 1;   
                    Qu[ high ++] = y;   
                }   
            }   
            low ++;   
        }   
        return level[E] != -1;   
    }    
    
    LL MaxFlow( int S, int E ){   
        LL maxflow = 0;   
        for( ; BFS(S, E) ; ) {   
            memcpy( out, next, sizeof(out) );   
            int now = -1;   
            for( ;; ) {   
                if( now < 0 ) {   
                    int cur = out[S];   
                    for(; cur != -1 ; cur = net[cur].next )    
                        if( net[cur].val && out[net[cur].v] != -1 && level[net[cur].v] == 1 )   
                            break;   
                    if( cur >= 0 ) Qu[ ++now ] = cur, out[S] = net[cur].next;   
                    else break;   
                }   
                int u = net[ Qu[now] ].v;   
                if( u == E ) {   
                    LL flow = inf;   
                    int index = -1;   
                    for( int i = 0; i <= now; i ++ ) {   
                        if( flow > net[ Qu[i] ].val )   
                            flow = net[ Qu[i] ].val, index = i;   
                    }   
                    maxflow += flow;   
                    for( int i = 0; i <= now; i ++ )   
                        net[Qu[i]].val -= flow, net[Qu[i]^1].val += flow;   
                    for( int i = 0; i <= now; i ++ ) {   
                        if( net[ Qu[i] ].val == 0 ) {   
                            now = index - 1;   
                            break;   
                        }   
                    }   
                }   
                else{   
                    int cur = out[u];   
                    for(; cur != -1; cur = net[cur].next )    
                        if (net[cur].val && out[net[cur].v] != -1 && level[u] + 1 == level[net[cur].v])   
                            break;   
                    if( cur != -1 )   
                        Qu[++ now] = cur, out[u] = net[cur].next;   
                    else out[u] = -1, now --;   
                }   
            }   
        }   
        return maxflow;   
    }   
};   

int main()
{
    int ca,t,u,v;
    LL w;
    scanf("%d",&ca);
    for(int k=1;k<=ca;k++)
    {
        Dinic my;
        scanf("%d%d",&n,&m);
        while(m--)
        {
            scanf("%d%d%I64d%d",&u,&v,&w,&t);
            my.insert(u,v,w*100001+1);
            if(t==1)
            {
                my.insert(v,u,w*100001+1);
            }
        }
        LL ans=my.MaxFlow(0,n-1);
        //cout<<ans<<endl;
        printf("Case %d: %I64d\n",k,(ans%100001));
    }
    return 0;
}
          

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值