poj3204 Ikki's Story I - Road Reconstruction 网络流dinic

poj3204
暴搞。


显然,增加的必定是割边的容量。如果不是割边,就有剩余容量,原来的容量都流不满,再加也没用。

做一遍网络流,记录割边。将割边容量加一点点(就是1),在残量网络上继续网络流。如果有可行流说明这条边是OK的啦!

注意如果直接增加割边容量,从空图开始网络流可能会超时。记录一下原图的流量情况,每次枚举恢复一下原图就ok了。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;
#define INF 5200000
#define NN 520
#define MM 10100


struct edge{int u,v,c;}ed[MM];//MM要为连接关系数量的两倍
int fi[NN],ne[MM],te,lev[NN];

int pe[NN],cap[NN],tc[NN],sta[NN];//模拟栈用数组

void init(){
    memset(fi,-1,sizeof(fi));
    te=0;
}

void insert(int u,int v,int c){
    ed[te].u=u;ed[te].v=v;ed[te].c=c;
    ne[te]=fi[u];fi[u]=te++;
    ed[te].u=v;ed[te].v=u;ed[te].c=0;
    ne[te]=fi[v];fi[v]=te++;
}

int dinic_bfs(int s,int t){
    memset(lev,0,sizeof(lev));
    lev[s]=1;
    queue<int> q;
    q.push(s);
    int u,v,e,c;
    while(!q.empty()){
        u=q.front();q.pop();
        for(e=fi[u];e!=-1;e=ne[e]){
            v=ed[e].v;c=ed[e].c;
            if (!lev[v]&&c){
                lev[v]=lev[u]+1;
                q.push(v);
            }
        }
        if (u==t) break;
    }
    return lev[t];
}

int dinic_dfs1(int u,int t,int cap){//直接深搜
    int tc=cap;
    if (u==t) return cap;
    int v,e,c,tf;
    for(e=fi[u];e!=-1&&tc;e=ne[e]){
        v=ed[e].v;
        c=ed[e].c;
        if (lev[v]==lev[u]+1&&c>0){
            tf=dinic_dfs1(v,t,min(tc,c));
            tc-=tf;
            ed[e].c-=tf;
            ed[e^1].c+=tf;
        }
    }
    if (e==-1) lev[u]=-10;//重要优化
    return cap-tc;
}



int dinic_dfs(int s,int t,int tn){//模拟栈深搜
    int top=0,i,flow=0;
    sta[++top]=s;
    cap[s]=tc[s]=INF;
    pe[s]=fi[s];
    int u,v,c,tf;
    while(top){
        u=sta[top];
        if (u!=t)
        for(int &e=pe[u];e!=-1&&tc[u];e=ne[e]){
            v=ed[e].v;c=ed[e].c;
            if (lev[v]==lev[u]+1&&c){
                sta[++top]=v;pe[v]=fi[v];tc[v]=cap[v]=min(c,tc[u]);
                break;
            }
        }
        else tc[u]=0;
        if (tc[u]==0||pe[u]==-1) {
            tf=cap[u]-tc[u];
            if (pe[u]==-1) lev[u]=-10;//重要优化
            top--;
            if (top){
                tc[sta[top]]-=tf;
                ed[pe[sta[top]]].c-=tf;
                ed[pe[sta[top]]^1].c+=tf;
                pe[sta[top]]=ne[pe[sta[top]]];
            }
            else flow+=tf;
        }
    }
    return flow;
}



int dinic(int s,int t,int tn){//起点终点相等需要特判
    int flow=0;
    int tf=0;
    while(dinic_bfs(s,t)){
        flow+=dinic_dfs(s,t,t+1);
    }
    return flow;
}

int aa[MM],s,t,n,m,ta,sc[MM];

int main(){
    int i,a,b,c,j,ans,ans1,ans2;
    scanf("%d%d",&n,&m);
    init();
    for(i=1;i<=m;++i){
        scanf("%d%d%d",&a,&b,&c);
        insert(a,b,c);
    }
    s=0;t=n-1;
    ans1=dinic(s,t,t+1);

    ta=0;
    for(i=0;i<te;i+=2)if (ed[i].c==0){//记录割边
        aa[++ta]=i;
    }
    for(i=0;i<te;++i) sc[i]=ed[i].c;
    ans=0;

    for(i=1;i<=ta;++i){

        for(j=0;j<te;++j){
            ed[j].c=sc[j];
        }
        ed[aa[i]].c=sc[aa[i]]+1;
        ans2=dinic(s,t,t+1);
        if (ans2>0) ans++;

    }
    printf("%d\n",ans);
    return 0;
}




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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值