Dinic算法,时间复杂度O(n^2 *m) 可通过
[C++] 纯文本查看 复制代码 #include<bits/stdc++.h>
using namespace std;
using i64 = long long;
using u64 = unsigned long long;
using u32 = unsigned;
template <typename T>
struct Flow_
{
const int n;
const T inf = numeric_limits<T>::max();
struct Edge
{
int to;
T w;
Edge(int to, T w) : to(to), w(w) {}
};
vector<Edge> ver;
vector<vector<int>> h;
vector<int> cur, d;
Flow_(int n) : n(n + 1), h(n + 1) {}
void add(int u, int v, T c)
{
h[u].push_back(ver.size());
ver.emplace_back(v, c);
h[v].push_back(ver.size());
ver.emplace_back(u, 0);
}
bool bfs(int s, int t)
{
d.assign(n, -1);
d[s] = 0;
queue<int> q;
q.push(s);
while (!q.empty())
{
auto x = q.front();
q.pop();
for (auto it : h[x])
{
auto [y, w] = ver[it];
if (w && d[y] == -1)
{
d[y] = d[x] + 1;
if (y == t)
return true;
q.push(y);
}
}
}
return false;
}
T dfs(int u, int t, T f)
{
if (u == t)
return f;
auto r = f;
for (int &i = cur[u]; i < h[u].size(); i++)
{
auto j = h[u][i];
auto &[v, c] = ver[j];
auto &[u, rc] = ver[j ^ 1];
if (c && d[v] == d[u] + 1)
{
auto a = dfs(v, t, std::min(r, c));
c -= a;
rc += a;
r -= a;
if (!r)
return f;
}
}
return f - r;
}
T work(int s, int t)
{
T ans = 0;
while (bfs(s, t))
{
cur.assign(n, 0);
ans += dfs(s, t, inf);
}
return ans;
}
};
using Flow = Flow_<i64>;
void solve(){
int n, m, s, t;
cin >> n >> m >> s >> t;
Flow flow(n);
for (int i = 0; i < m;i++){
int u, v, w;
cin >> u >> v >> w;
flow.add(u, v, w);
}
i64 ans = flow.work(s,t);
cout << ans << '\n';
}
signed main(){
ios::sync_with_stdio(false);
cin.tie(nullptr);
solve();
return 0;
} |