hdu 1532 最大网络流 Drainage Ditches
来源:互联网 发布:art template.js文档 编辑:程序博客网 时间:2024/05/20 18:47
Problem Description
Every time it rains on Farmer John's fields, a pond forms over Bessie's favorite clover patch. This means that the clover is covered by water for awhile and takes quite a long time to regrow. Thus, Farmer John has built a set of drainage ditches so that Bessie's clover patch is never covered in water. Instead, the water is drained to a nearby stream. Being an ace engineer, Farmer John has also installed regulators at the beginning of each ditch, so he can control at what rate water flows into that ditch.
Farmer John knows not only how many gallons of water each ditch can transport per minute but also the exact layout of the ditches, which feed out of the pond and into each other and stream in a potentially complex network.
Given all this information, determine the maximum rate at which water can be transported out of the pond and into the stream. For any given ditch, water flows in only one direction, but there might be a way that water can flow in a circle.
Farmer John knows not only how many gallons of water each ditch can transport per minute but also the exact layout of the ditches, which feed out of the pond and into each other and stream in a potentially complex network.
Given all this information, determine the maximum rate at which water can be transported out of the pond and into the stream. For any given ditch, water flows in only one direction, but there might be a way that water can flow in a circle.
Input
The input includes several cases. For each case, the first line contains two space-separated integers, N (0 <= N <= 200) and M (2 <= M <= 200). N is the number of ditches that Farmer John has dug. M is the number of intersections points for those ditches. Intersection 1 is the pond. Intersection point M is the stream. Each of the following N lines contains three integers, Si, Ei, and Ci. Si and Ei (1 <= Si, Ei <= M) designate the intersections between which this ditch flows. Water will flow through this ditch from Si to Ei. Ci (0 <= Ci <= 10,000,000) is the maximum rate at which water will flow through the ditch.
Output
For each case, output a single integer, the maximum rate at which water may emptied from the pond.
Sample Input
5 41 2 401 4 202 4 202 3 303 4 10
Sample Output
50
Source
USACO 93
#include<stdio.h>
#include<string.h>
#include<deque>
using namespace std;
#define maxn 550
#define INF 99999999
int G[maxn][maxn];
int prev[maxn];
bool vis[maxn];
void KM(int n)
{
int sum=0,i,u,temp;
while(1)
{
deque<int>Q;
memset(vis,false,sizeof(vis));
memset(prev,0,sizeof(prev));
prev[1]=0;
vis[1]=true;
Q.push_back(1);
while(!Q.empty())
{
u=Q.front();
Q.pop_front();
for(i=1;i<=n;i++)
{
if(!vis[i]&&G[u][i])
{
vis[i]=true;
prev[i]=u;
if(i==n)
{
Q.clear();
break;
}
else
Q.push_back(i);
}
}
}
if(!vis[n])
break;
temp=INF;
i=n;
while(prev[i])
{
if(temp>G[prev[i]][i])
temp=G[prev[i]][i];
i=prev[i];
}
i=n;
while(prev[i])
{
G[prev[i]][i]-=temp;
G[i][prev[i]]+=temp;
i=prev[i];
}
sum+=temp;
}
printf("%d\n",sum);
}
int main()
{
int n,m;
int i;
int x,y,z;
while(scanf("%d%d",&m,&n)!=EOF)
{
memset(G,0,sizeof(G));
for(i=1;i<=m;i++)
{
scanf("%d%d%d",&x,&y,&z);
G[x][y]+=z;
}
KM(n);
}
return 0;
}
#include<string.h>
#include<deque>
using namespace std;
#define maxn 550
#define INF 99999999
int G[maxn][maxn];
int prev[maxn];
bool vis[maxn];
void KM(int n)
{
int sum=0,i,u,temp;
while(1)
{
deque<int>Q;
memset(vis,false,sizeof(vis));
memset(prev,0,sizeof(prev));
prev[1]=0;
vis[1]=true;
Q.push_back(1);
while(!Q.empty())
{
u=Q.front();
Q.pop_front();
for(i=1;i<=n;i++)
{
if(!vis[i]&&G[u][i])
{
vis[i]=true;
prev[i]=u;
if(i==n)
{
Q.clear();
break;
}
else
Q.push_back(i);
}
}
}
if(!vis[n])
break;
temp=INF;
i=n;
while(prev[i])
{
if(temp>G[prev[i]][i])
temp=G[prev[i]][i];
i=prev[i];
}
i=n;
while(prev[i])
{
G[prev[i]][i]-=temp;
G[i][prev[i]]+=temp;
i=prev[i];
}
sum+=temp;
}
printf("%d\n",sum);
}
int main()
{
int n,m;
int i;
int x,y,z;
while(scanf("%d%d",&m,&n)!=EOF)
{
memset(G,0,sizeof(G));
for(i=1;i<=m;i++)
{
scanf("%d%d%d",&x,&y,&z);
G[x][y]+=z;
}
KM(n);
}
return 0;
}
0 0
- HDU 1532 Drainage Ditches (最大网络流)
- HDU 1532 Drainage Ditches (最大网络流)
- hdu 1532 Drainage Ditches(最大网络流)
- hdu 1532 最大网络流 Drainage Ditches
- HDU 1532 Drainage Ditches 最大网络流
- hdu 1532 Drainage Ditches 基础最大流
- hdu 1532 Drainage Ditches 最大流
- hdu 1532 Drainage Ditches (最大流)
- [hdu 1532] Drainage Ditches(最大流dinic)
- hdu 1532 Drainage Ditches(最大流)
- hdu 1532 Drainage Ditches 最大流 水题
- HDU 1532 Drainage Ditches 最大流
- HDU 1532 Drainage Ditches(最大流)
- Drainage Ditches - HDU 1532 最大流
- HDU 1532--Drainage Ditches【最大流】
- hdu 1532 Drainage Ditches(最大流)
- HDU 1532 Drainage Ditches(最大流)
- hdu 1532 Drainage Ditches【最大流】
- 应用开发及托管服务
- Android连续点击两次Back键退出程序 分类: Android
- MFC选择文件夹对话框
- GAIA地形引擎源代码阅读 <一>
- Java线程(八):锁对象Lock-同步问题更完美的处理方式
- hdu 1532 最大网络流 Drainage Ditches
- JavaLearning:对象序列化
- Java线程(九):Condition-线程通信更高效的方式
- db2
- 查询mysql数据库字段及注释
- 循环多少次? hdoj 1799
- Java线程专栏文章汇总
- C#模拟MSN窗体抖动
- MongoDB can't call something 问题的解决办法