ISAP#1WA求解

P3376 【模板】网络最大流

KKarshilov @ 2017-10-06 00:06:45

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <functional>
#include <queue>
using namespace std;
const int MAXN = 1000003;
const int MAXE = 6000003;
const int INF = 0x3f3f3f3f;
struct Edge
{
    int to, nxt, cap, flow;
    Edge() {}
    Edge(int to, int nxt, int cap, int flow):to(to), nxt(nxt), cap(cap), flow(flow) {}
}e[MAXE];
int n, m;
int h[MAXN], p;
int source, sink, tot;
int pre[MAXN], num[MAXN], cur[MAXN];
int d[MAXN];
bool vis[MAXN];
queue<int> q;
inline void add_edge(int x, int y, int cap) {
    e[p] = Edge(y, h[x], cap, 0); h[x] = p++;
    e[p] = Edge(x, h[y], 0, 0); h[y] = p++;
}
void bfs() {
    memset(vis, 0, sizeof(vis));
    q.push(sink);
    vis[sink] = 1;
    d[sink] = 0;
    while(!q.empty()) {
        int u = q.front(); q.pop();
        num[d[u]]++;
        for(int i = h[u]; ~i; i = e[i].nxt) {
            int v = e[i].to;
            if(!vis[v]) {
                vis[v] = 1;
                d[v] = d[u] + 1;
                q.push(v);
            }
        }
    }
}
void augment(int flow) {
    for(int i = source; i != sink; i = e[cur[i]].to) {
        e[cur[i]].flow += flow;
        e[cur[i] ^ 1].flow -= flow;
    }
}
int isap() {
    memset(num, 0, sizeof(num));
    bfs();
    memcpy(cur, h, sizeof(h));
    int u = source, flow = 0, f = INF;
    while(d[source] < tot) {
        bool fg = 0;
        for(int i = cur[u]; ~i; i = e[i].nxt) {
            if(e[i].cap > e[i].flow && d[u] == d[e[i].to] + 1) {
                pre[e[i].to] = u;
                cur[u] = i;
                u = e[i].to;
                f = min(f, e[i].cap - e[i].flow);
                fg = 1;
                if(u == sink) {
                    flow += f;
                    augment(f);
                    f = INF;
                    u = source;
                }
                break;
            }
        }
        if(fg) continue;
        if(--num[d[u]] == 0) break;
        int M = tot - 1;
        for(int i = h[u]; ~i; i = e[i].nxt) {
            if(e[i].cap > e[i].flow && M > d[e[i].to]) {
                M = d[e[i].to];
                cur[u] = i;
            }
        }
        num[d[u] = M + 1]++;
        if(u != source) u = pre[u];
    }
    return flow;
}
int main() {
    scanf("%d%d%d%d", &n, &m, &source, &sink);
    tot = sink + 1;
    memset(h, -1, sizeof(h));
    for(int i = 1, a, b, c;i <= m; i++)
    {
        scanf("%d%d%d", &a, &b, &c);
        add_edge(a, b, c);
    }
    printf("%d\n", isap());
    return 0;
}

by λᴉʍ @ 2017-10-06 07:15:36

前排围观ISAP巨佬并%


by KKarshilov @ 2017-10-06 07:28:12

@XZZ__233 哥。。。怎么又是你啊


by λᴉʍ @ 2017-10-06 07:33:13

@归去来兮 中排围观ISAP巨佬并%怎么不下数据


by KKarshilov @ 2017-10-06 07:35:42

@XZZ__233 。。。多谢大佬指点


by heheheheehhejie @ 2017-10-13 21:41:39

后排围观两个滑稽大佬并%%%%%


by George_Plover @ 2017-10-27 20:25:38

大后排围观三个大佬并%%%%%%%


by 新城 @ 2017-10-29 19:23:04

****###%%%%%%%%%GGYGG%%%%%%%%%###****


|