hdu 3549 Flow Problem (最大流)
裸最大流,做模板用
m条边,n个点,求最大流
#include <iostream> #include <cstdio> #include <cstring> #include <queue> #include <algorithm> using namespace std; const int N = 20; const int INF = 0x7fffffff; int flow[N]; int cap[N][N]; int pre[N]; queue<int>q; int m, n; int BFS(int src, int des) { while (!q.empty()) q.pop(); for (int i = 1; i <= n; ++i) pre[i] = -1; flow[src] = INF; pre[src] = 0; q.push(src); while (!q.empty()) { int index = q.front(); q.pop(); if (index == des) break; for (int i = 1; i <= n; ++i) { if (pre[i] == -1 && cap[index][i] > 0) { pre[i] = index; flow[i] = min(cap[index][i], flow[index]); q.push(i); } } } if (pre[des] == -1) return -1; return flow[des]; } int maxFlow(int src, int des) { int ans = 0; int in = 0; while ((in = BFS(src, des)) != -1) { int k = des; while (k != src) { int last = pre[k]; cap[last][k] -= in; cap[k][last] += in; k = last; } ans += in; } return ans; } int main() { int t; int i, k; scanf("%d", &t); for (k = 1; k <= t; ++k) { memset(cap, 0, sizeof cap); memset(flow, 0, sizeof flow); scanf("%d%d", &n, &m); for (i = 0; i < m; ++i) { int a, b, c; scanf("%d%d%d", &a, &b, &c); cap[a][b] += c; } printf("Case %d: %d\n", k, maxFlow(1, n)); } return 0; }