hdu 2435 There is a war 最小割-带加边条件

/*
题意:给定一个有向网络,边权为拆掉边的代价,现在1要到n去,n试图阻止1到底,它至少花多大代价。
      有一个条件,1可以在任意两点(不含1和n)加入一条边(此边不可被拆除),求n要花费的最小代价最大值
题解:如果没有后面一个条件,答案就是最小割。易知加入两条边必须从源集到汇集(不然原最小割可以割断联系)
      对于求一遍1到n的最大流网络状态network和最小割tmp,加入一条满足上述条件的边,然后再求1到n的最大流,得到的就是附加代价,
      只需要使得这个附加代价最大即可。
      枚举源集到汇集的所有边,得到最大的附加代价ans(每次前要恢复到network状态,并删除上次加入的边)。
      最后答案就是tmp+ans;
      
      
*/
#include <iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<algorithm>
#include<set>
#include<ctime>
#include<map>
#include<queue>
#include<vector>
typedef int LL;
using namespace std;
const int maxn=109;
const int inf=1<<30;
//**************************************************
//为dinic求最大流模版
struct edge
{
    int v, next;
    LL val;
} net[ maxn*maxn*2 ],net_[ maxn*maxn*2];
int n,m,size;
int level[maxn], Qu[maxn], out[maxn],next[maxn],_next[maxn];
class Dinic {
public:
    int end;
    int low,high;
    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) );
        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;
                    if(y==E)
                    return true;
                }
            }
            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 --;
                }
            }
        }
        size=high;
        return maxflow;
    }
};
set<int> S;
int bs[maxn];
int main()
{
    int ca,a,b,c;
    scanf("%d",&ca);
    while(ca--)
    {
        Dinic my;
        scanf("%d%d",&n,&m);
        for(int i=0;i<m;i++)
        {
            scanf("%d%d%d",&a,&b,&c);
            my.insert(a,b,c);
        }
        int tmp=my.MaxFlow(1,n);//求一遍最小割
        
        //记录当前的网络状态network
        for(int i=1;i<=n;i++)
        _next[i]=next[i];
        for(int i=0;i<my.end;i++)
        net_[i]=net[i];
        //*************************
        
        int ans=0;
        S.clear();
        for(int i=0;i<size;i++)
        {
            S.insert(Qu[i]);//Qu是源集
        }
        int num=0;
        for(int j=2;j<n;j++)
        {
            if(S.count(j)==0)
            {
                bs[num++]=j;//记录汇集
            }
        }
        for(int i=0;i<size;i++)if(Qu[i]!=1)
        {
            for(int j=0;j<num;j++)
            {
                for(int f=1;f<=n;f++)//恢复到network状态
                next[f]=_next[f];
                for(int f=0;f<my.end;f++)
                net[f]=net_[f];

                my.insert(Qu[i],bs[j],inf);
                int k=my.MaxFlow(1,n);//得到附加代价
                if(k>ans)
                ans=k;
                
                //删除刚加入的边
                next[bs[j]]=net[my.end-1].next;
                next[Qu[i]]=net[my.end-2].next;
                my.end-=2;
            }
        }
        printf("%d\n",ans+tmp);
    }
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值