diff --git a/Luogu/P3376/P3376.cpp b/Luogu/P3376/P3376.cpp index a80ce228..3291408c 100644 --- a/Luogu/P3376/P3376.cpp +++ b/Luogu/P3376/P3376.cpp @@ -1,5 +1,5 @@ #include -#include +#include #include using std::cin; @@ -8,44 +8,45 @@ const char endl = '\n'; const int N = 205, M = 5005; +const int INF = 0x3f3f3f3f; int n, m, s, t, flow; long long ans; - -// Graph -int idx, head[N], edge[M << 1], ver[M << 1], next[M << 1]; +int idx, head[N], ver[M << 1], edge[M << 1], next[M << 1]; +int d[N], cur[N]; void add(int u, int v, int w) { next[idx] = head[u]; - edge[idx] = w; ver[idx] = v; + edge[idx] = w; head[u] = idx++; } -// Dinic -int d[N], cur[N]; - bool bfs() { - memset(d, 0x00, sizeof(d)); + std::fill_n(d, N, 0); + std::queue q; - d[s] = 1; q.push(s); + d[s] = 1; cur[s] = head[s]; while (!q.empty()) { int u = q.front(); q.pop(); + for (int i = head[u]; ~i; i = next[i]) { int v = ver[i], w = edge[i]; + if (w && !d[v]) { d[v] = d[u] + 1; cur[v] = head[v]; if (v == t) return true; - q.push(v); + q.emplace(v); } } } + return false; } @@ -58,30 +59,41 @@ int dinic(int u, int limit) { int v = ver[i], w = edge[i]; + if (w && d[v] == d[u] + 1) { - int k = dinic(v, std::min(edge[i], limit - flow)); + int k = dinic(v, std::min(w, limit - flow)); + if (!k) d[v] = 0; + edge[i] -= k; edge[i ^ 1] += k; flow += k; } } + return flow; } int main() { std::ios::sync_with_stdio(false); - memset(head, 0xff, sizeof(head)); + cin.tie(nullptr); + + std::fill_n(head, N, -1); + cin >> n >> m >> s >> t; - for (int i = 1; i <= m; i++) { - int u, v, w; + + for (int i = 1, u, v, w; i <= m; i++) { cin >> u >> v >> w; + add(u, v, w); add(v, u, 0); } + while (bfs()) { - while (flow = dinic(s, 0x3f3f3f3f)) ans += flow; + while (flow = dinic(s, INF)) ans += flow; } + cout << ans << endl; + return 0; } diff --git a/Luogu/P3376/data/P3376_7.in b/Luogu/P3376/data/P3376_7.in new file mode 100644 index 00000000..fde9e15a --- /dev/null +++ b/Luogu/P3376/data/P3376_7.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:9c2edbc19f4a198ad327a229949eaed355cd613cd5fa743cc3caec02f2bfa528 +size 24510 diff --git a/Luogu/P3376/data/P3376_7.out b/Luogu/P3376/data/P3376_7.out new file mode 100644 index 00000000..b24f6e50 --- /dev/null +++ b/Luogu/P3376/data/P3376_7.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:711705b8d16758ada9c9de622d69b10052bd7258acb69fa33c6bed20855083f4 +size 12