USACO 4.4 Pollutant Control
2012-01-24 18:39
309 查看
非常不错的一个最大流的题目,对边的处理很巧妙。这个链接的解题报告比较好:http://hi.baidu.com/mengyun1993/blog/item/c30d193c9a85932870cf6cda.html总体上分3步,这题不看解题报告真不好做,我自己想了半个多小时,只想到了如何判断一条边是否在最小割中,也就是解题报告中说的“退流定理”~再赞一下USACO,题目确实很不错,很多问题很值得花时间好好研究一下。
Test 1: TEST OK [0.000 secs, 3224 KB] Test 2: TEST OK [0.000 secs, 3224 KB] Test 3: TEST OK [0.000 secs, 3092 KB] Test 4: TEST OK [0.000 secs, 3224 KB] Test 5: TEST OK [0.000 secs, 3224 KB] Test 6: TEST OK [0.000 secs, 3224 KB] Test 7: TEST OK [0.000 secs, 3224 KB] Test 8: TEST OK [0.000 secs, 3224 KB] Test 9: TEST OK [0.000 secs, 3224 KB] Test 10: TEST OK [0.022 secs, 3224 KB] Test 11: TEST OK [0.000 secs, 3224 KB] Test 12: TEST OK [0.000 secs, 3224 KB]
/* ID: zlqest11 LANG: C++ TASK: milk6 */ #include <iostream> #include <cstdio> #include <cstring> #include <vector> #include <algorithm> using namespace std; typedef long long llg; const int N = 35; const int M = 1010; const llg INF = 1LL<<60; struct node{ int u, v, w; }edge[M]; int n, m; llg miniCut, c , f , tf ; //data structure for dinic int lab , queue ; vector <int> ans; bool bfs(int s, int t){ int u, head, tail; memset(lab, -1, sizeof(lab)); head = tail = lab[s] = 0; queue[0] = s; while(head <= tail){ u = queue[head++]; for(int i = 1; i <= n; i++){ if(c[u][i]>f[u][i] && lab[i]==-1){ lab[i] = lab[u] + 1; queue[++tail] = i; } } if(lab[t] != -1) return true; } return false; } llg dinic_dfs(int u, llg delta){ llg sum, tmp; if(u == n) return delta; sum = 0; for(int i = 1; i <= n; i++){ if(lab[i]==lab[u]+1 && c[u][i]>f[u][i]){ tmp = dinic_dfs(i, min(delta, c[u][i]-f[u][i])); delta -= tmp; sum += tmp; f[u][i] += tmp; f[i][u] -= tmp; } } return sum; } llg maxFlow(){ llg ans = 0; while(bfs(1, n)){ ans += dinic_dfs(1, INF); } return ans; } void makeTmp(){ for(int i = 1; i <= n; i++) for(int j = 1; j <= n; j++) tf[i][j] = f[i][j]; } void reTmp(){ for(int i = 1; i <= n; i++) for(int j = 1; j <= n; j++) f[i][j] = tf[i][j]; } int main() { int a, b, w; freopen("milk6.in", "r", stdin); freopen("milk6.out", "w", stdout); //get data and init graph scanf("%d%d", &n, &m); memset(c, 0, sizeof(c)); for(int i = 0; i < m; i++){ scanf("%d%d%d", &a, &b, &w); edge[i].u = a; edge[i].v = b; edge[i].w = (llg)w*(m+1)+1; c[a][b] += edge[i].w; } memset(f, 0, sizeof(f)); miniCut = maxFlow(); ans.clear(); llg xCut = miniCut, yCut; for(int i = 0; i < m; i++){ a = edge[i].u; b = edge[i].v; w = edge[i].w; if(c[a][b] == f[a][b]){ //tmp for flow makeTmp(); c[a][b] -= w; memset(f, 0, sizeof(f)); yCut = maxFlow(); if(xCut-yCut == w){ ans.push_back(i+1); xCut = yCut; } else{ c[a][b] += w; reTmp(); } } } printf("%d %d\n", (int)(miniCut/(m+1)), (int)(miniCut%(m+1))); for(int i = 0; i < ans.size(); i++) printf("%d\n", ans[i]); return 0; }
相关文章推荐
- 求割边最少的最小割(P1344 [USACO4.4]追查坏牛奶Pollutant Control)
- USACO 4.4 Pollutant Control (网络流求最小割割集)
- 洛谷P1344 [USACO4.4]追查坏牛奶Pollutant Control(网络流, 最大流最小割)
- USACO 4.4 Pollutant Control
- Pollutant Control_usaco 4.4_网络流
- 洛谷 P1344 [USACO4.4]追查坏牛奶Pollutant Control
- P1344 [USACO4.4] 追查坏牛奶Pollutant Control
- [USACO4.4.2 Pollutant Control]
- usaco Pollutant Control
- USACO Pollutant Control 解题报告
- USACO Section 4.4 Pollutant Control
- usaco training 4.4.2 Pollutant Control 追查坏牛奶 题解
- usaco 4.4.2 Pollutant Control
- usaco Pollutant Control
- USACO 4.4.2 Pollutant Control追查坏牛奶 题解与分析
- USACO Section 4.4 Pollutant Control - 找更优的搜索枚举方案~
- USACO 4.4 分析
- USACO 4.4 Frame Up (拓扑排序)
- 08-27~29 HDU1010 USACO4.3~4.4 搜索剪枝,模拟,拓扑,网络流
- usaco 4.4 Shuttle Puzzle 找规律