标签:stack iostream intersect scan pre ted miss usaco memory
#include<iostream> #include<cstdio> #include<cmath> #include<cstring> #include<sstream> #include<algorithm> #include<queue> #include<deque> #include<iomanip> #include<vector> #include<cmath> #include<map> #include<stack> #include<set> #include<fstream> #include<memory> #include<list> #include<string> using namespace std; typedef long long LL; typedef unsigned long long ULL; #define MAXN 1100 #define L 31 #define INF 1000000009 #define eps 0.00000001 /* 最大流问题 */ int g[MAXN][MAXN], path[MAXN], flow[MAXN], start, End, n, m; int bfs() { queue<int> q; q.push(start); memset(path, -1, sizeof(path)); path[start] = 0; flow[start] = INF; while (!q.empty()) { int tmp = q.front(); q.pop(); if (tmp == End) break; for (int i = 1; i <= n; i++) { if (i != start&&g[tmp][i] && path[i] == -1) { flow[i] = min(g[tmp][i], flow[tmp]); path[i] = tmp; q.push(i); } } } if (path[End] == -1) return -1; return flow[End]; } int EK() { int max_flow = 0, now, step; while ((step = bfs())!= -1) { max_flow += step; now = End; while (now != start) { int pre = path[now]; g[pre][now] -= step; g[now][pre] += step; now = pre; } } return max_flow; } int main() { while (scanf("%d%d", &m, &n) != EOF) { memset(g, 0, sizeof(g)); int f, t, d; for (int i = 0; i < m; i++) { scanf("%d%d%d", &f, &t, &d); g[f][t] += d; } start = 1, End = n; printf("%d\n", EK()); } }
标签:stack iostream intersect scan pre ted miss usaco memory
原文地址:http://www.cnblogs.com/joeylee97/p/6852440.html