POJ 1273 解题报告
2014-11-11 00:47
337 查看
还是按照http://blog.csdn.net/thestoryofsnow/article/details/40942009里面的刷题顺序,最简单的最大流问题。
这道题我在USACO上面已经做过了:http://blog.csdn.net/thestoryofsnow/article/details/38103513 Drainage Ditches问题
这里稍稍改动,主要是把同样两个点之间的pair的所有capacity加到一起了。用的还是我之前写的模板,dfs。
周末刷题的人好多,poj特别挤,程序一直提交不上去。周一就好多了。
这道题我在USACO上面已经做过了:http://blog.csdn.net/thestoryofsnow/article/details/38103513 Drainage Ditches问题
这里稍稍改动,主要是把同样两个点之间的pair的所有capacity加到一起了。用的还是我之前写的模板,dfs。
周末刷题的人好多,poj特别挤,程序一直提交不上去。周一就好多了。
Accepted | 436K | 79MS | C++ | 2427B |
/* ID: thestor1 LANG: C++ TASK: poj1273 */ #include <iostream> #include <cmath> #include <cstdio> #include <cstring> #include <climits> #include <cassert> #include <string> #include <vector> #include <set> #include <map> #include <queue> #include <stack> #include <algorithm> using namespace std; int find_path(vector<vector<int> > &capacity, vector<vector<int> > adjs, const int source, const int sink) { int M = capacity.size(); std::vector<bool> visited(M, false); // from[x] is the previous vertex visited in the shortest path from the source to x std::vector<int> from(M, -1); queue<int> Q; Q.push(source); visited[source] = true; int where = -1, next = -1, prev = -1; bool found = false; while (!Q.empty()) { where = Q.front(); Q.pop(); for (int i = 0; i < adjs[where].size(); ++i) { next = adjs[where][i]; if (!visited[next] && capacity[where][next] > 0) { Q.push(next); visited[next] = true; from[next] = where; if (next == sink) { found = true; break; } } } if (found) { break; } } // we compute the path capacity where = sink; int path_cap = -1; while (from[where] > -1) { prev = from[where]; if (path_cap > -1) { path_cap = min(path_cap, capacity[prev][where]); } else { path_cap = capacity[prev][where]; } where = prev; } // we update the residual network; if no path is found the while loop will not be entered where = sink; while (from[where] > -1) { prev = from[where]; capacity[prev][where] -= path_cap; capacity[where][prev] += path_cap; where = prev; } // if no path is found, path_cap is infinity if (path_cap == -1) { return 0; } return path_cap; } int main() { int N, M; while (cin >> N >> M) { vector<vector<int> > capacity(M, vector<int>(M, 0)); vector<vector<int> > adjs(M, vector<int>()); for (int i = 0; i < N; ++i) { int s, e, c; cin >> s >> e >> c; capacity[s - 1][e - 1] += c; adjs[s - 1].push_back(e - 1); adjs[e - 1].push_back(s - 1); } // max flow int maxflow = 0; while (true) { int path_cap = find_path(capacity, adjs, 0, M - 1); if (path_cap == 0) { break; } else { maxflow += path_cap; } } cout << maxflow << endl; } return 0; }
相关文章推荐
- poj1273解题报告(最大流 EK算法)
- poj1579解题报告
- POJ 1702 解题报告——Eva's Balance:用砝码为物体称重
- POJ - 2352 Stars解题报告(树状数组求二维区域和)
- poj2159解题报告
- POJ 2367 拓扑排序 解题报告
- poj 1325Machine Schedule解题报告-最小顶点覆盖等于最大匹配数
- poj2255 Tree Recovery 字典树 解题报告(Ulm Local 1997)
- POJ 2719 Faulty Odometer 解题报告
- POJ1250解题报告补
- poj 1050 解题报告
- POJ_1014 Dividing解题报告
- POJ3009Curling 2.0解题报告
- poj 2606 Rabbit hunt 解题报告
- POJ 3714-Raid解题报告
- 北大POJ解题报告-1004 Financial Management
- POJ1247解题报告
- 【原】 POJ 1088 滑雪 递归+memoization 解题报告
- 【原】 POJ 1157 LITTLE SHOP OF FLOWERS 动态规划 解题报告
- 【原】 POJ 2352 Stars 树状数组 解题报告