70分 RE

P3376 【模板】网络最大流

ayasaki @ 2019-07-25 17:43:15


#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;
const int MAXN = 1010;
const int MAXM = 200010;
const int INF = (1 << 31) - 1;
int n, m, s, t;
struct V
{
    int to;
    int rev;
    int cap;
    int nt;
}edge[MAXM];
int st[MAXN], top=-1;
int vis[MAXN];
void add(int x, int y, int val)
{
    top++;  edge[top] = { y, top + 1, val, st[x] }; st[x] = top;
    top++;  edge[top] = { x, top - 1, 0  , st[y] }; st[y] = top;
}
int dfs(int num, int srm)
{
    if (num == t)
        return srm;
    vis[num] = 1;
    for (int i = st[num]; i!=-1; i = edge[i].nt)
    {
        int to = edge[i].to;
        if (!vis[to] && edge[i].cap > 0)
        {
            int minn = min(srm, edge[i].cap);
            int d = dfs(to, minn);
            if (d <= 0)
                continue;
            edge[i].cap -= d;
            edge[i^1].cap += d;
            return d;
        }
    }
    return 0;
}
int max_flow()
{
    int flow = 0;
    while (1)
    {
        memset(vis, 0, sizeof(vis));
        int d = dfs(s, INF);
        if (!d) break;
        flow += d;
    }
    return flow;
}
int main()
{
    scanf("%d%d%d%d", &n, &m, &s, &t);
    memset(st, -1, sizeof(st));
    for (int i = 1; i <= m; i++)
    {
        int x, y, z;
        scanf("%d%d%d", &x, &y, &z);
        add(x, y, z);
    }
    printf("%d", max_flow());
    return 0;
}

by ayasaki @ 2019-07-25 17:44:19

找到错了,打扰了。。


|