萌新求助

P3376 【模板】网络最大流

Sai0511 @ 2019-03-15 20:09:33

多年不写dinic然后找不出错了。。身拜名裂。
为何70?

#include <bits/stdc++.h>
#define il inline
const int maxn = 1e4 + 10;
const int maxm = 2e5 + 10;
const int inf = 0x3f3f3f3f;
using namespace std;
namespace Fast_Input {
    template<class T> il void read(T& res) {
        res = 0;char ch;bool sign = 0;
        do { ch = getchar(); sign |= ch == '-'; } while(!isdigit(ch));
        while(isdigit(ch)) {
            res = (res << 1) + (res << 3) + (ch & 15);
            ch = getchar();
        }
        (sign) && (res = -res);
    } 
}
using Fast_Input::read;
int n,m,i,j,k,s,t,ecnt;
int head[maxn],depth[maxn];
int wei[maxm << 1],nxt[maxm << 1],ver[maxm << 1];
il int _min(int x,int y) {
    return x < y ? x : y;   
}
il void addedge(int u,int v,int w) {
    wei[++ecnt] = w;
    ver[ecnt] = v;
    nxt[ecnt] = head[u];
    head[u] = ecnt;
    return;
}
il bool bfs() {
    memset(depth,0,sizeof(depth));
    queue<int> q;q.push(s);depth[s] = 1;
    while(!q.empty()) {
        int u = q.front();q.pop();   
        for(int i = head[u];~i;i = nxt[i]) {
            int v = ver[i];        
            if(!depth[v] && wei[i] > 0) {
                depth[v] = depth[u] + 1;
                q.push(v);
            }
        }
    }
    return depth[t] > 0;
}
int dfs(int u,int flow) {
    if(u == t) return flow;  
    for(int i = head[u];~i;i = nxt[i]) {
        int v = ver[i],w = wei[i];
        if(depth[v] == depth[u] + 1 && w != 0) {
            int tmp = dfs(v,_min(w,flow));       
            if(tmp > 0) {
                wei[i] -= tmp;
                wei[i ^ 1] += tmp;
                return tmp;
            }
        }
    }
    return 0;
}
il int dinic() {
    int res = 0;
    while(bfs()) { 
        while(int t = dfs(s,inf)) res += t;   
    }
    return res;
}
int main() {
    read(n);read(m);read(s);read(t);
    memset(head,-1,sizeof(head));
    for(int i = 1,u,v,w;i <= m;i++) {
        read(u);read(v);read(w);
        addedge(u,v,w);
        addedge(v,u,0);
    }
    printf("%d\n",dinic());
    return 0;
}

by GKxx @ 2019-03-15 20:13:10

@Sai_0511 ecnt没有初始化为-1


by Caishifeng666 @ 2019-03-15 20:21:09

又一个红名萌新……


by 铃宕 @ 2019-03-15 20:23:06

去你的萌新


|