3376!!70分求教神犇

P3376 【模板】网络最大流

king12138 @ 2019-07-11 09:23:15

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
using namespace std;
const int N = 100005, M = 200005,oo = 0x7fffffff;
int next[M],to[M],w[M]; 
int n,m,begin,end;
int dist[N],head[N],en = 1;
queue<int> q;
inline void add(int x,int y,int c){
    to[en] = y;
    w[en] = c;
    next[en] = head[x];
    head[x] = en++;

    to[en] = x;
    w[en] = 0;
    next[en] = head[y];
    head[y] = en++;
}
bool bfs(){
    memset(dist,0,sizeof(dist));
    while(q.size()) q.pop();
    q.push(begin);dist[begin] = 1;
    while(q.size()){
        int x = q.front(); q.pop();
        for(int p=head[x];p;p=next[p])
            if(w[p]&&!dist[to[p]]){
                q.push(to[p]);
                dist[to[p]] = dist[x] + 1;
                if(to[p] == end) return true; 
        }       
    } 
    return false;
}
int dinic(int x,int flow){
    if(x==end) return flow;
    int rest = flow,k;
    for(int p = head[x];p&&rest;p = next[p])
        if(w[p]&&dist[to[p]] == dist[x] + 1){
            k = dinic(to[p],min(rest,w[p]));
            if(!k) dist[to[p]] = 0;
            w[p] -= k;
            w[p^1] += k;
            rest -= k;
        }
    return flow - rest;
}
int main(){
    cin>>n>>m>>begin>>end;
    for(int i=1,x,y,w;i<=m;i++){
        cin>>x>>y>>w;
        add(x,y,w);
    }
    int flow = 0,ans = 0;
    while(bfs()){
        while(flow = dinic(begin,oo)) ans += flow; 
    }
    cout<<ans<<endl;
    return 0;
} 

by powerfire @ 2019-07-11 09:49:47

TQL QAQ


by EternalEpic @ 2019-07-11 09:54:37

@king12138 你的链式前向星写错了,根据异或成对存储,你的en变量有问题。

正确方式:

int head[Maxn];
int ver[Maxm], edge[Maxm], nxt[Maxm], tot = 1;

inline void add(int x, int y, int z) {
    ver[++tot] = y; nxt[tot] = head[x];
    edge[tot] = z; head[x] = tot;
    ver[++tot] = x; nxt[tot] = head[y];
    edge[tot] = 0; head[y] = tot;
}

by EternalEpic @ 2019-07-11 09:56:25

1 ^ 1 = 0, 0 ^ 1 = 1; 2 ^ 1 = 3, 3 ^ 1 = 2; 我是直接从2存的(就是二三一对,你一二一对肯定错啦!)@king12138


by king12138 @ 2019-07-11 10:16:44

@刘兆洲 谢谢大佬


|