萌新求助,LOJAC,提交TLE。。。
查看原帖
萌新求助,LOJAC,提交TLE。。。
173660
zhoukangyang楼主2020/5/31 21:27
#include<bits/stdc++.h>
#define inf 0x3f3f3f3f
#define N 11010
#define M 120010
using namespace std;
int n, m, s, t, sum, cc[N], use[N], yflow[N], flag[N], cntcc[N];
char xB[1 << 15], *xS = xB, *xT = xB;
#define getchar() (xS == xT && (xT = (xS = xB) + fread(xB, 1, 1 << 15, stdin), xS == xT) ? 0 : *xS++)
inline int read() {
    int x = 0, f = 1;
    char ch = getchar();
    while (ch < '0' || ch > '9') {
        if(ch == '-')
            f = -1;
        ch = getchar();
    }
    while (ch >= '0' && ch <= '9') {
        x = x * 10 + ch - '0';
        ch = getchar();
    }
    return x * f;
}
struct cmp {
    bool operator()(int a, int b) const { return cc[a] < cc[b]; }
};
priority_queue<int, vector<int>, cmp> q;
struct node {
    int to, next, val;
} e[M << 1];
int head[N], last[N];
void add(int x, int y, int val, int id) {
    if(head[x] == 0) head[x] = id;
    else e[last[x]].next = id;
    last[x] = id, e[id].val = val, e[id].to = y;
}
void bfs() {
    memset(cc, 0x3f, sizeof(cc));
    use[1] = t, cc[t] = 0;
    int u = 0, v = 1;
    while (u < v) {
        ++u;
        int fst = use[u];
        for(int i = head[fst]; i != 0; i = e[i].next) {
            if(cc[e[i].to] != inf) continue;
            if(e[i ^ 1].val == 0) continue;
            ++v, cc[e[i].to] = cc[fst] + 1, use[v] = e[i].to;
        }
    }
    use[s] = n;
}
void HLPP() {
    bfs();
    if(cc[s] == inf) return;
    cc[s] = n;
    for(int i = 1; i <= n; i++)
        if(cc[i] != inf)
            cntcc[cc[i]]++;
    for(int i = head[s]; i != 0; i = e[i].next) {
        yflow[e[i].to] = e[i].val, e[i ^ 1].val += e[i].val, e[i].val = 0;
        if(e[i].to != s && e[i].to != t && flag[e[i].to] == 0)
            flag[e[i].to] = 1, q.push(e[i].to);
    }
    while(!q.empty()) {
        int now = q.top();
        q.pop();
        flag[now] = 0;
        if(cc[now] == n + 1) continue;
        for(int i = head[now]; i != 0; i = e[i].next)
            if(cc[now] == cc[e[i].to] + 1) {
                int flow = min(yflow[now], e[i].val);
                if(flow == 0) continue;
                e[i].val -= flow, e[i ^ 1].val += flow;
                yflow[now] -= flow, yflow[e[i].to] += flow;
                if(e[i].to != s && e[i].to != t && flag[e[i].to] == 0)
                    flag[e[i].to] = 1, q.push(e[i].to);
                if(!yflow[now]) break;
            }
        if(yflow[now]) {
            --cntcc[cc[now]];
            if(cntcc[cc[now]] == 0) {
                for(int i = 1; i <= n; i++)
                    if(cc[i] > cc[now] && i != s && i != t && cc[now] <= now)
                        cc[now] = n + 1, yflow[i] = 0;
                continue;
            }
            int tmp = m + 1;
            for(int i = head[now]; i != 0; i = e[i].next)
                if(e[i].val)
                    tmp = min(cc[e[i].to] + 1, tmp);
            cc[now] = tmp;
            ++cntcc[cc[now]];
            flag[now] = 1;
            q.push(now);
        }
    }
}
signed main() {
    int x, y, z;
    n = read(), m = read(), s = read(), t = read();
    for(int i = 1; i <= m; i++) {
        x = read(), y = read(), z = read();
        add(x, y, z, i * 2);
        add(y, x, 0, i * 2 + 1);
    }
    HLPP();
    printf("%d\n", yflow[t]);
    return 0;
}

https://www.luogu.com.cn/record/34059527 https://loj.ac/submission/820057 马峰已经极限了

2020/5/31 21:27
加载中...