链接:https://vjudge.net/problem/HDU-1532
题意:
n条边,m个节点。
求最大流。
多组输入。
思路:
增广路算法。
代码:
#include <iostream> #include <memory.h> #include <vector> #include <map> #include <algorithm> #include <cstdio> #include <math.h> #include <queue> #include <string> using namespace std; typedef long long LL; const int MAXN = 200 + 10; const int INF = 1e9 + 10; struct Edge { int _from, _to, _cap, _flow; Edge(int f, int t, int c, int l):_from(f), _to(t), _cap(c), _flow(l) {}; }; vector<int> G[MAXN]; vector<Edge> edges; int a[MAXN], p[MAXN]; int n, m; int Get_Flow() { LL flow = 0; while (1) { memset(a, 0, sizeof(a)); queue<int> que; que.push(1); a[1] = INF; while (!que.empty()) { int x = que.front(); que.pop(); for (int i = 0;i < G[x].size();i++) { Edge & e = edges[G[x][i]]; if (!a[e._to] && e._cap > e._flow) { p[e._to] = G[x][i]; a[e._to] = min(a[x], e._cap - e._flow); que.push(e._to); } } if (a[m]) break; } if (a[m] == 0) break; for (int u = m;u != 1;u = edges[p[u]]._from) { edges[p[u]]._flow += a[m]; edges[p[u] ^ 1]._flow -= a[m]; } flow += a[m]; } return flow; } void Init() { edges.clear(); for (int i = 1;i <= m;i++) G[i].clear(); } int main() { int l, r, c; while(~scanf("%d%d", &n, &m)) { Init(); for (int i = 1; i <= n; i++) { scanf("%d%d%d", &l, &r, &c); edges.emplace_back(l, r, c, 0); edges.emplace_back(r, l, 0, 0); int sum = edges.size(); G[l].push_back(sum - 2); G[r].push_back(sum - 1); } printf("%d ", Get_Flow()); } return 0; }