您的位置:首页 > 理论基础 > 计算机网络

HDU 1532 Drainage Ditches 网络流

2012-11-24 17:38 309 查看
http://acm.hdu.edu.cn/showproblem.php?pid=1532

题意:

  从1开始到终点n,最多能通过多少的流量,就比如说1到2的流量是20,2到3的流量是10,那么到达终点3的流量为10,而

1到2还能经过10的流量,2到3不能再通过。

坑爹:

  每次找到一条增广轨的时候要补一个反向的边,不然你走这一条路就认为你一定走了,因为有可能这条路不是最佳的路径。

解法:

  利用BFS搜索点,找到终点就返回1,没有则返回0,再设置一个maxflow函数,只要BFS的返回值为1,就将这条增广轨上的

最大可以通过的流量(min)算出来(利用change_map函数),然后 max_flow += min。

#include<iostream>
#include<queue>
using namespace std;
const int maxn = 200 + 10;
const int INF = 0x3fffffff;

int map[maxn][maxn];
int front[maxn];
int used[maxn];
int max_flow;
int m;
int n;

void init()
{
max_flow = 0;
memset(map,0,sizeof(map));
}

int BFS()
{
memset(used,0,sizeof(used));
queue<int> Q;
while(!Q.empty())
{
Q.pop();
}
Q.push(1);
front[1] = 1;
used[1] = 1;
while(!Q.empty())
{
int i;
int temp = Q.front();
Q.pop();
for(i=2; i<=n; i++)
{
if(map[temp][i] != 0 && used[i] == 0)
{
Q.push(i);
used[i] = 1;
front[i] = temp;
if(i == n)
{
return 1;
}
}
}
}
return 0;
}

int change_map()
{
int min = INF;
int temp = n;
while(front[temp] != temp)
{
if(min > map[front[temp]][temp])
{
min = map[front[temp]][temp];
}
temp = front[temp];
}
temp = n;
while(front[temp] != temp)
{
map[front[temp]][temp] -= min;
map[temp][front[temp]] += min;//必须补反边
temp = front[temp];
}
return min;
}

void maxflow()
{
while(BFS())
{
max_flow += change_map();
}
}

int main()
{
while(cin>>m>>n)
{
init();
int i;
int a;
int b;
int c;
for(i=0; i<m; i++)
{
cin>>a>>b>>c;
map[a][b] += c;
}
maxflow();
cout<<max_flow<<endl;
}
return 0;
}


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