蒟蒻80分WAdinic求大佬

P3376 【模板】网络最大流

littlejuruo @ 2019-08-04 11:28:03

RT

#include <bits/stdc++.h>
using namespace std;
const int MAXN=1000000+10,MAXn=100000+10,INF=0x3f3f3f3f;
struct node{
    int to,next,v;
}edge[MAXN];
queue<int> q;
int s,t,n,m,depth[MAXN],cnt,ans,head[MAXn],cur[MAXn];
void addedge(int x,int y,int v)
{
    edge[cnt++].next=head[x];
    edge[cnt].to=y;
    edge[cnt].v=v;
    head[x]=cnt;
}
int dfs(int x,int f)
{
    int w,used=0;
    if(x==t||!f){
        return f;
    }   
    for(int i=cur[x];i;i=edge[i].next){
        if(depth[edge[i].to]==depth[x]+1 &&edge[i].v>0){
            w=f-used;
            w=dfs(edge[i].to,min(edge[i].v,w));
            edge[i].v-=w;
            edge[^i].v+=w;
            if(edge[i].v){
                cur[x]=i;
            }

            used+=w;
            if(used==f){
                return f;
            }
        }

    }
    if(!used){
        depth[x]=-1;
    }
    return used;
} 
bool bfs(){
    memset(depth,-1,sizeof(depth));

    for(int i=1;i<=n;++i){
        cur[i]=head[i];
    }
    while(!q.empty()){
        q.pop();
    }
    depth[s]=0;
    q.push(s);

    while(!q.empty()){
//      cout<<"bfs\n";
        int now=q.front();q.pop();
//      cout<<now<<"now\n";
        for(int i=head[now];i;i=edge[i].next){
            if(edge[i].v&&depth[edge[i].to]==-1){
                /*cout<<now<<" "<<edge[i].to
                <<" "<<depth[now]+1
                <<" "<<edge[i].v<<" "
                <<depth[edge[i].to]<<"bfs\n";*/
                depth[edge[i].to]=depth[now]+1;
                q.push(edge[i].to);
            }
        }
    }   
//  cout<<depth[t]<<"depth\n";
    return depth[t]!=-1;
}
void dinic()
{
    while(bfs()){
        int a=dfs(s,INF);

        ans+=a;
//        cout<<ans<<"ans\n";
    }
}
int main()
{
//  freopen("*.in","r",stdin);
//  freopen("*.out","w",stdout);
    cin>>n>>m>>s>>t;
    int x,y,w;
    for(int i=1;i<=m;++i){
        cin>>x>>y>>w;
        if(x==t){
            i--;
            m--;
            continue;
        }else if(y==s){
            i--;
            m--;
            continue;
        }
        addedge(x,y,w);
        addedge(y,x,0);
    }
    dinic();
//  cout<<m<<"m\n";
    cout<<ans<<endl;
    return 0;
}

输出中间信息,发现好像是到了得到了答案却没停,继续増广。 也看了一些帖子,改过把head从0开始存,结果答案错的更离谱,直接一次増广就停了QAQ


by t162 @ 2019-08-04 11:29:41

void addedge(int x,int y,int v)
{
    edge[cnt++].next=head[x];
    edge[cnt].to=y;
    edge[cnt].v=v;
    head[x]=cnt;
}

你把一条边的各种信息给拆了


by t162 @ 2019-08-04 11:32:09

@littlejuruo


by littlejuruo @ 2019-08-04 11:32:37

发错代码惹

dfs里的^i应该是i^1

不小心发了旧版本的qwq


by littlejuruo @ 2019-08-04 11:35:53

谢谢大佬%%% @Bambusoideae


|