Drainage Ditches

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.  

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 4
1 2 40
1 4 20
2 4 20
2 3 30
3 4 10

Sample Output

50
题意:求一个起点到终点的最大流。
题解:多次bfs找增广流直到没有增广流为止
#include<stdio.h>
#include<iostream>
#include<queue>
#include<string.h>
#define INF 10000000
using namespace std;
int p[205][205];
int path[205];//保存增广路径
int flow[205];
int n,m,s;
int min(int a,int b)
{
 return a<b?a:b;
}
queue<int> q;
int bfs(int s)
{
 int i;
 while(!q.empty()) q.pop();
 memset(path,-1,sizeof(path));
 path[s]=0;
 flow[s]=INF;
 q.push(s);
 while(!q.empty())
 {
       int t=q.front();
    q.pop();
    for(i=1;i<=m;i++)
    {
     if(i!=s&&path[i]==-1&&p[t][i])
     {
      flow[i]=min(flow[t],p[t][i]);
      path[i]=t;
      q.push(i);
     }
    }
 }
    if(path[m]==-1) return -1;
 return flow[m];
}
int finder()
{
  int maxi=0,cur,pre,step;
  while((step=bfs(s))!=-1)
  {
   maxi+=step;
      cur=m;
   while(cur!=s)
   {
    pre=path[cur];
    p[pre][cur]-=step;
    p[cur][pre]+=step;
    cur=pre;
   }
  }
  return maxi;
}
int main()
{
    int i;
 while(scanf("%d %d",&n,&m)!=EOF)
 {
  int a,b,c;
  memset(p,0,sizeof(p));
  for(i=1;i<=n;i++)
  {
   scanf("%d %d %d",&a,&b,&c);
   p[a][b]+=c;
  }
  s=1;
        printf("%d\n",finder());
 }
 return 0;
}

转载于:https://www.cnblogs.com/ffhuguang/p/3363924.html

这段代码实现了一个并查集数据结构。并查集是一种树型的数据结构,用于处理一些不相交集合(Disjoint Sets)的合并及查询问题。它支持两种操作: - 查找(Find):确定元素属于哪一个子集。它可以被用来确定两个元素是否属于同一子集。 - 合并(Union):将两个子集合并成同一个集合。 并查集可以用于解决很多实际问题,例如: - 判断无向图中是否有环 - 在图像处理中,判断连通区域 - 在游戏开发中,判断游戏中角色的关系 在这段代码中,类 UnionFindSet 的初始化函数 __init__ 接收两个参数:起始点 start 和结束点 n。接着定义了两个列表 pre 和 rank,用于存储每个节点的父节点和树的深度。其中,pre[i] 表示节点 i 的父节点,如果 pre[i] = i,则 i 为该集合的代表元素。 接下来的函数 init 用于初始化并查集,将每个节点的父节点设置为自身,深度为 1。 函数 find_pre 用于查找节点 x 的代表元素,同时实现了路径压缩的优化,即将查找路径上的所有节点都直接连接到代表元素上,减少查找时间。 函数 is_same 用于判断节点 x 和节点 y 是否在同一个集合中,即是否具有相同的代表元素。 函数 unite 用于合并两个集合,即将 x 所在的集合和 y 所在的集合合并为一个集合。首先查找 x 和 y 的代表元素,如果它们已经在同一个集合中,则直接返回 False。否则,将深度较小的集合连接到深度较大的集合上,并更新代表元素和深度。 最后,函数 is_one 用于判断整个并查集是否只有一个集合。它首先找到起始点的代表元素 temp,然后遍历起始点到结束点之间的所有节点,如果存在任意一个节点的代表元素不等于 temp,则说明存在多个集合,返回 False;否则,所有节点都在同一个集合中,返回 True。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值