USACO 4.2 Drainage Ditches(网络流模板题)
Drainage Ditches
Hal Burch
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. Note however, that there can be more than one ditch between two intersections.
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.
PROGRAM NAME: ditch
INPUT FORMAT
Line 1: | 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. |
Line 2..N+1: | Each of 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. |
SAMPLE INPUT (file ditch.in)
5 4
1 2 40
1 4 20
2 4 20
2 3 30
3 4 10
OUTPUT FORMAT
One line with a single integer, the maximum rate at which water may emptied from the pond.
SAMPLE OUTPUT (file ditch.out)
50
————————————————————————————————————————————————
它有个俗名叫草地排水
/*
ID:ivorysi
PROG:ditch
LANG:C++
*/
#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
#include <set>
#include <vector>
#define inf 0x7fffffff
#define ivorysi
#define siji(i,x,y) for(int i=x;i<=y;++i)
#define gongzi(j,x,y) for(int j=x;j>=y;--j)
#define xiaosiji(i,x,y) for(int i=x;i<y;++i)
#define sigongzi(j,x,y) for(int j=x;j>y;--j)
using namespace std;
struct node {
int to,next,val;
}edge[];
int head[],sum=;
void add(int u,int v,int c) {
edge[++sum].to=v;
edge[sum].next=head[u];
edge[sum].val=c;
head[u]=sum;
}
void AddTwoWays(int u,int v,int c) {
add(u,v,c);
add(v,u,);//反向边
}
int n,m,dis[],gap[],ans;
int sap(int u,int aug){//O(玄学)
if(u==m) return aug;
int dmin=m-,flow=,v,t; for(int i=head[u];i;i=edge[i].next) {
v=edge[i].to;
if(edge[i].val>) {//只有还能流动的地方才能分层
if(dis[u]==dis[v]+) {
t=sap(v,min(aug-flow,edge[i].val));//限制流量不超过该点流量和边的限制
edge[i].val-=t;
edge[i^].val+=t;
flow+=t;
if(aug==flow) return flow;//流尽
if(dis[]>=m) return flow;//搜索已在某处达到结束条件
}
dmin=min(dmin,dis[v]);
}
}
if(!flow) {
--gap[dis[u]];
if(gap[dis[u]]==) dis[]=m;//出现断层说明无法再流
dis[u]=dmin+;//分层
++gap[dis[u]];
}
return flow;
}
int main(int argc, char const *argv[])
{
#ifdef ivorysi
freopen("ditch.in","r",stdin);
freopen("ditch.out","w",stdout);
#else
freopen("f1.in","r",stdin);
#endif
scanf("%d%d",&n,&m);
int u,v,c;
siji(i,,n) {
scanf("%d%d%d",&u,&v,&c);
AddTwoWays(u,v,c);
}
while(dis[]<m) {ans+=sap(,inf);}
printf("%d\n",ans);
return ;
}
USACO 4.2 Drainage Ditches(网络流模板题)的更多相关文章
- POJ 1273:Drainage Ditches 网络流模板题
Drainage Ditches Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 63339 Accepted: 2443 ...
- HDU 1532 Drainage Ditches(网络流模板题)
题目大意:就是由于下大雨的时候约翰的农场就会被雨水给淹没,无奈下约翰不得不修建水沟,而且是网络水沟,并且聪明的约翰还控制了水的流速, 本题就是让你求出最大流速,无疑要运用到求最大流了.题中m为水沟数, ...
- Drainage Ditches~网络流模板
Description Every time it rains on Farmer John's fields, a pond forms over Bessie's favorite clover ...
- poj 1273 Drainage Ditches (网络流 最大流)
网络流模板题. ============================================================================================ ...
- POJ 1273 Drainage Ditches (网络流Dinic模板)
Description Every time it rains on Farmer John's fields, a pond forms over Bessie's favorite clover ...
- NYOJ 323 Drainage Ditches 网络流 FF 练手
Drainage Ditches 时间限制:1000 ms | 内存限制:65535 KB 难度:4 描述 Every time it rains on Farmer John's fields, ...
- Drainage Ditches--hdu1532(网络流 模板)
http://acm.hdu.edu.cn/showproblem.php?pid=1532 Drainage Ditches Time Limit: 2000/1000 MS (Java/Other ...
- [POJ1273][USACO4.2]Drainage Ditches (网络流最大流)
题意 网络流最大流模板 思路 EK也不会超时 所以说是一个数据比较水的模板题 但是POJ有点坑,多组数据,而且题目没给 哭得我AC率直掉 代码 用的朴素Dinic #include<cstdio ...
- HDU1532 Drainage Ditches 网络流EK算法
Drainage Ditches Problem Description Every time it rains on Farmer John's fields, a pond forms over ...
随机推荐
- 读书笔记—CLR via C#字符串及文本
前言 这本书这几年零零散散读过两三遍了,作为经典书籍,应该重复读反复读,既然我现在开始写博了,我也准备把以前觉得经典的好书重读细读一遍,并且将笔记整理到博客中,好记性不如烂笔头,同时也在写的过程中也可 ...
- sql常用语句汇总
--创建数据库 USE yuju CREATE database YuJu on primary ( name='YuJu', filename='B:\ceshi数据库\YuJu.mdf', max ...
- iOS基础 - KVC and KVO
一.KVC(key value coding) 我们一般是通过调用set方法或属性的点语法来直接更改对象的状态,即对象的属性值,比如[stu setAge:10]; stu.age = 9; KVC ...
- Java多线程学习笔记——从Java JVM对多线程数据同步的一些理解
我们知道在多线程编程中,我们很大的一部分内容是为了解决线程间的资源同步问题和线程间共同协作解决问题.线程间的同步,通俗我们理解为僧多粥少,在粥有限情况下,我们怎么去防止大家有秩序的喝到粥,不至于 ...
- c# 发邮件功能
using System;using System.Collections.Generic;using System.Data;using System.Data.SqlClient;using Sy ...
- C#的匿名委托 和 Java的匿名局部内部类
.NET:C#的匿名委托 和 Java的匿名局部内部类 目录 背景实验备注 背景返回目录 这几天重温Java,发现Java在嵌套类型这里提供的特性比较多,结合自身对C#中匿名委托的理解,我大胆的做了一 ...
- MongoDB学习之--安全和认证
MongoDB学习之--安全和认证 本文主要介绍两部分内容,Mongodb的安全检查配置以及安全认证操作: 虽然确保系统安全是系统管理员的重要工作,但是作为程序员了解其机制也是大有好处的,毕竟不是每个 ...
- 基于 WebSocket 构建跨浏览器的实时应用
Socket.IO – 基于 WebSocket 构建跨浏览器的实时应用 Socket.IO 是一个功能非常强大的框架,能够帮助你构建基于 WebSocket 的跨浏览器的实时应用.支持主流浏览器,多 ...
- RTB撕开黑盒子 Part 0:Pacing: is everyone doing it wrong?
曾尝试为我们的RTB客户解决过Pacing问题,Pacing问题要解决的问题是:如果一个客户给你一笔预算,让你去运营一个广告推广计划,在一定的时间内投放广告,将这笔预算在指内的时间内,比较均匀地将预算 ...
- Java 多线程系列
要编写线程安全的代码,其核心在于要对状态访问操作进行管理,特别是对共享的(Shared)和可变的(Mutable)状态的访问. Java中的主要同步机制是关键字synchronized,它提供了一种独 ...