- 网络流(草地排水)

news/2025/5/24 2:28:13

网络流(Dinic(模板))

Drainage Ditches

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 16467    Accepted Submission(s): 7823


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. 

 

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

 

Source
USACO 93

 

Recommend
lwg

 

#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue> 
#define MAXN 300
#define INF 9999999
using namespace std;
int N,M,map[MAXN][MAXN],a,b,f,maxflow,pre[MAXN];
void flow(int start,int end){while(1){queue<int>q;/*while(!q.empty())q.pop();*/		int minflow=INF;q.push(1);memset(pre,0,sizeof(pre));while(!q.empty()){int u=q.front();q.pop();if(u==end)	break; for(int i=1;i<=M;i++)if(map[u][i]&&!pre[i]){pre[i]=u;q.push(i);}}if(pre[end]==0)	break;for(int i=end;i!=start;i=pre[i])minflow=min(minflow,map[pre[i]][i]);for(int i=end;i!=start;i=pre[i]){map[pre[i]][i]-=minflow;map[i][pre[i]]+=minflow;}maxflow+=minflow;}
}
int main(){cin>>N>>M;for(int i=1;i<=N;i++){cin>>a>>b>>f;map[a][b]+=f;}maxflow;	//最大流 flow(1,M);cout<<maxflow;
}

  

转载于:https://www.cnblogs.com/cangT-Tlan/p/6502908.html


https://dhexx.cn/news/show-3380714.html

相关文章

剑指offer面试题4拓展——已排序数组的合并

题目&#xff1a;有两个排序的数组A1和A2,内存在A1的末尾有足够的空间来容纳数组A2,请实现一种函数&#xff0c;把A2的所有数字插入到A1中并且所有的数字是排序的 //思路&#xff1a;设置两个索引indexofA1,indexofA2分别指向数组A1和A2的最后一个元素&#xff0c;即两个数组的…

Adobe是什么软件?

Adobe是一家跨国软件公司&#xff0c;总部位于美国加利福尼亚州。 Adobe公司开发和销售的软件广泛应用于图形设计、影像处理、多媒体内容创作、网页开发等领域。 Adobe公司的软件产品包括Photoshop、Illustrator、InDesign、Acrobat、Premiere Pro、After Effects等&#xff…

eclipse打jar包

今天给spark项目打jar包&#xff0c;报错&#xff1a;JAR creation failed. See details for additional information. Resource is out of sync with the file system: /WordCountSpark/.cache.解决办法&#xff1a;eclipse里clean一下项目即可&#xff0c;出现这个问题是因…

Android中关于主线程的理解

在Android中&#xff0c;四大组件运行在主线程中&#xff0c;在主线程中做耗时操作会导致程序出现卡顿甚至出现ANR异常&#xff0c;一个基本常识就是将耗时操作放到子线程中去处理&#xff0c;然后通过Handler回调到主线程。 但是有三点还需要注意&#xff1a; 1 因为四大组件…

bgd

我是北京工业大学耿丹学院信息系中软14-1班&#xff0c;陈宇。 我的专业是计算机科学与技术&#xff0c;这是我的第一次随笔。 我的兴趣爱好是上网&#xff0c;看电影&#xff0c;打网球。毕业之后大概就是从事IT方面的工作。 熟悉的编程语言就是在学校学习的c,java。在学校的老…

设计模式之三--单例模式《转载》

概念&#xff1a;  Java中单例模式是一种常见的设计模式&#xff0c;单例模式的写法有好几种&#xff0c;这里主要介绍三种&#xff1a;懒汉式单例、饿汉式单例、登记式单例。  单例模式有以下特点&#xff1a;  1、单例类只能有一个实例。  2、单例类必须自己创建自己…

smali代码相关

一.smali调试&#xff1a;调试Smali代码主要任务是解决注入代码后导致的运行时错误。具体的说&#xff0c;就是使注入后的Smali代码通过dalvik虚拟机的字节码校验。获取错误的方法相对简单&#xff0c;使用下面两条命令即可&#xff1a;adb logcat | grep dalvikvmadb logcat |…

iOS 蓝牙获取MAC地址

援引&#xff1a;http://www.jianshu.com/p/1d6a8fc8134f iOS要获取蓝牙设备的MAC地址有两种&#xff1a;一是硬件工程师开通的服务特征下有MAC的信息&#xff0c;我们就从通道中获取&#xff1b;二是硬件工程师在扫描中设备信息中放置MAC信息&#xff0c;我们从有RSSI的函数中…

FXS/FXO, BRI/PRI, IPPBX

FXO - Foreign Exchange Office 外部交换局。简单的理解它是 PBX 交换机上用来同公共电话网相连的接口。也就是是中央交换局交换机和数字电话交换系统之间的一个中继端连接。相对于中心局而言&#xff0c;它模拟一台PABX分机&#xff0c;可实现一部普通电话机与一部多路复用器的…

如何修改linux用户密码?

如果是以root身份登录,修改root密码.只要输入 passwd 就会出现: New password: Retype new password: 按提示输入密码确认即可. 如果想更改其他用户密码,只要输入passwd username即可. 如&#xff1a;hadoop用户&#xff08;passwd hadoop&#xff09;New password: Retyp…