HDU 5889 [minimum cut + shortest path]

The meaning of the question: Given an undirected graph with n points and m sides, the edge weights are all 1. The enemy is ready to take the shortest path at point n and attack his own position at point 1. Now we need to set up some roadblocks on some edges to give Figure out the cost of setting up roadblocks on each side, requiring the least cost to set up roadblocks so that the enemy will inevitably encounter roadblocks.

This code uses the current arc optimization, although I don't really understand it. . .
But if you don't use it, it will time out!

#include<stdio.h>
#include<string.h>
#include<queue>
using namespace std;
struct node {
    int from;
    int to;
    int w;
    int next;
}e[160000];
int cur[1500];
int head[1500];
int divv[1500];
int cont, ss, tt;
void add(int from, int to, int w) {
    e[cont].from = from;
    e[cont].w = w;
    e[cont].to = to;
    e[cont].next = head[from];
    head[from] = cont++;
}
int n, m;
int makedivv() {
    memset(divv, 0, sizeof(divv));
    divv[ss] = 1;
    queue<int >s;
    s.push(ss);
    while (!s.empty()) {
        int u = s.front();
        if (u == tt)return 1;
        s.pop();
        for (int i = head[u]; i != -1; i = e[i].next) {
            int w = e[i].w;
            int v = e[i].to;
            if (divv[v] == 0 && w) {
                divv[v] = divv[u] + 1;
                s.push(v);
            }
        }
    }
    return 0;
}
int Dfs(int u, int maxflow, int tt) {
    if (u == tt)return maxflow;
    int ret = 0;
    //当前弧优化
    for (int &i = cur[u]; i != -1; i = e[i].next) {
        int v = e[i].to;
        int w = e[i].w;
        if (divv[v] == divv[u] + 1 && w) {
            int f = Dfs(v, min(maxflow - ret, w), tt);
            e[i].w -= f;
            e[i ^ 1].w += f;
            ret += f;
            if (ret == maxflow)return ret;
        }
    }
    return ret;
}
void Dinic() {
    long long int ans = 0;
    while (makedivv() == 1) {
        memcpy(cur, head, sizeof(head));
        ans += Dfs(ss, 0x3f3f3f3f, tt);
    }
    printf("%lld\n", ans % 100000);
}
int main() {
    int t;
    scanf("%d", &t);
    while (t--) {
        scanf("%d%d", &n, &m);
        scanf("%d%d", &ss, &tt);
        cont = 0;
        memset(head, -1, sizeof(head));
        for (int i = 0; i < m; i++) {
            int x, y, z;
            scanf("%d%d%d", &x, &y, &z);
            add(x, y, z); add(y, x, 0);
        }
        Dinic();
    }
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325063497&siteId=291194637
Recommended