diff --git a/AcWing/2171/2171.cpp b/AcWing/2171/2171.cpp new file mode 100644 index 00000000..4ba375c0 --- /dev/null +++ b/AcWing/2171/2171.cpp @@ -0,0 +1,77 @@ +#include +#include +#include +#include + +using std::cin; +using std::cout; +const char endl = '\n'; + +const int N = 1005, + M = 10005; + +int n, m, s, t, ans; + +// Graph +int idx, head[N], ver[M << 1], edge[M << 1], next[M << 1]; + +void add(int u, int v, int w) { + next[idx] = head[u]; + ver[idx] = v; + edge[idx] = w; + head[u] = idx++; +} + +// Edmonds-Karp +int d[N], pre[N]; +bool vis[N]; + +bool bfs() { + memset(vis, 0x00, sizeof(vis)); + std::queue q; + q.push(s); + vis[s] = true; + d[s] = std::numeric_limits::max(); + while (!q.empty()) { + int x = q.front(); + q.pop(); + for (int i = head[x]; ~i; i = next[i]) { + if (edge[i]) { + int y = ver[i]; + if (vis[y]) continue; + d[y] = std::min(d[x], edge[i]); + pre[y] = i; + q.push(y); + vis[y] = true; + if (y == t) return true; + } + } + } + return false; +} + +void update() { + int x = t; + while (x != s) { + int i = pre[x]; + edge[i] -= d[t]; + edge[i ^ 1] += d[t]; + x = ver[i ^ 1]; + } + ans += d[t]; +} + +int main() { + std::ios::sync_with_stdio(false); + memset(head, 0xff, sizeof(head)); + cin >> n >> m >> s >> t; + for (int i = 1; i <= m; i++) { + int u, v, w; + cin >> u >> v >> w; + add(u, v, w); + add(v, u, 0); + } + while (bfs()) update(); + cout << ans << endl; + return 0; +} diff --git a/AcWing/2171/data/5.ans b/AcWing/2171/data/5.ans new file mode 100644 index 00000000..d771faad --- /dev/null +++ b/AcWing/2171/data/5.ans @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:5c862790153d23a72dd410bb8b36819d51ac248cbaec4f6742efd8e75f20b175 +size 4 diff --git a/AcWing/2171/data/5.in b/AcWing/2171/data/5.in new file mode 100644 index 00000000..5bf6d84e --- /dev/null +++ b/AcWing/2171/data/5.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:816fed73e97fe0d5a1fb4aaae54307283611cc109f3b1ef3576d046551f8cdb0 +size 801