您的位置:首页 > 其它

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;
}

                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: