POJ-1273Drainage Ditches(网络流入门题,最大流)
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
Output
Sample Input
5 4
1 2 40
1 4 20
2 4 20
2 3 30
3 4 10
Sample Output
50 代码:
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<queue>
#include<stack>
#include<set>
#include<map>
#include<vector>
#include<cmath> typedef long long ll;
using namespace std;
const int inf = 0x3f3f3f3f;
const int maxn = ;
const int maxe = * maxn * maxn;
struct MaxFlow
{
struct Edge {
int v, w, nxt;
} edge[maxe];
int head[maxn], tot, level[maxn];
void init(){
memset(head,-,sizeof(head));
tot=;
}
void add(int u, int v, int w) {
edge[tot].v = v;
edge[tot].w = w;
edge[tot].nxt = head[u];
head[u] = tot++; edge[tot].v = u;
edge[tot].w = ;
edge[tot].nxt = head[v];
head[v] = tot++;
}
bool bfs(int s, int t) {
memset(level, -, sizeof(level));
queue<int>q;
q.push(s);
level[s] = ;
while(!q.empty()) {
int u = q.front(); q.pop();
for(int i = head[u]; ~i; i = edge[i].nxt) {
if(edge[i].w > && level[edge[i].v] < ) {
level[edge[i].v] = level[u] + ;
q.push(edge[i].v);
}
}
}
return level[t] > ;
}
int dfs(int u, int t, int f) {
if(u == t) return f;
for(int i = head[u]; ~i; i = edge[i].nxt) {
int v = edge[i].v;
if(edge[i].w > && level[v] > level[u]) {
int d = dfs(v, t, min(f, edge[i].w));
if(d > ) {
edge[i].w -= d;
edge[i ^ ].w += d;
return d;
}
}
}
level[u] = -;
return ;
}
int solve(int s, int t) {
int flow = , f;
while(bfs(s, t)) {
while(f = dfs(s, t, inf)) flow += f;
}
return flow;
}
}F; int main(){
int n,m;
while(~scanf("%d%d",&m,&n))
{
F.init();
for(int i=;i<m;i++){
int u,v,c;
scanf("%d%d%d",&u,&v,&c);
F.add(u,v,c);
}
printf("%d\n",F.solve(,n));
}
return ;
}
POJ-1273Drainage Ditches(网络流入门题,最大流)的更多相关文章
- Tile Cut~网络流入门题
Description When Frodo, Sam, Merry, and Pippin are at the Green Dragon Inn drinking ale, they like t ...
- POJ 1273:Drainage Ditches 网络流模板题
Drainage Ditches Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 63339 Accepted: 2443 ...
- POJ 3207 【2-SAT入门题 + 强连通分量】
这道题是我对于2-SAT问题的入门题:http://poj.org/problem?id=3207 一篇非常非常非常好的博客,很详细,认真看一遍差不多可以了解个大概:https://blog.csdn ...
- POJ 2342 树形DP入门题
有一个大学的庆典晚会,想邀请一些在大学任职的人来參加,每一个人有自己的搞笑值,可是如今遇到一个问题就是假设两个人之间有直接的上下级关系,那么他们中仅仅能有一个来參加,求请来一部分人之后,搞笑值的最大是 ...
- 网络流入门—用于最大流的Dinic算法
"网络流博大精深"-sideman语 一个基本的网络流问题 最早知道网络流的内容便是最大流问题,最大流问题很好理解: 解释一定要通俗! 如右图所示,有一个管道系统,节点{1,2,3 ...
- USACO 4.2 Drainage Ditches(网络流模板题)
Drainage DitchesHal Burch Every time it rains on Farmer John's fields, a pond forms over Bessie's fa ...
- HDU 1532 Drainage Ditches(网络流模板题)
题目大意:就是由于下大雨的时候约翰的农场就会被雨水给淹没,无奈下约翰不得不修建水沟,而且是网络水沟,并且聪明的约翰还控制了水的流速, 本题就是让你求出最大流速,无疑要运用到求最大流了.题中m为水沟数, ...
- POJ 3984(DFS入门题 +stack储存路径)
POJ 3984 Description 定义一个二维数组: int maze[5][5] = { 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, ...
- poj1273 网络流入门题 dinic算法解决,可作模板使用
Drainage Ditches Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 62078 Accepted: 2384 ...
随机推荐
- Improving RGB-D SLAM in dynamic environments: A motion removal approach
一.贡献 (1)提出一种针对RGB-D的新的运动分割算法 (2)运动分割采用矢量量化深度图像 (3)数据集测试,并建立RGB-D SLAM系统 二.Related work [1]R.K. Namde ...
- 灰帽黑客 基本的Linux漏洞攻击
有两个重要的寄存器负责处理堆栈:基址指针(EBP)和栈指针(ESP),EBP指向当前进程的当前栈帧的底部,ESP则总是指向栈顶 当调用函数的时候,会导致程序流跳转.在汇编代码调用函数时,将发生以下三件 ...
- CI4框架应用一 - 环境搭建
CI框架 (codeigniter)算是一个古老的框架了,由于在工作中一直在使用这个框架,还是比较有感情的.我对CI的感觉就是,简单易用,学习曲线平滑,对于新手友好. 目前CI框架已经更新到CI4了, ...
- SpringBoot整合Shiro自定义Redis存储
Shiro Shiro 主要分为 安全认证 和 接口授权 两个部分,其中的核心组件为 Subject. SecurityManager. Realms,公共部分 Shiro 都已经为我们封装好了,我们 ...
- Spring学习之——手写Spring源码V2.0(实现IOC、D、MVC、AOP)
前言 在上一篇<Spring学习之——手写Spring源码(V1.0)>中,我实现了一个Mini版本的Spring框架,在这几天,博主又看了不少关于Spring源码解析的视频,受益匪浅,也 ...
- C++基础面试题及答案
C++ C++ 和C的主要区别 C语言是面向过程编程,C++是面向对象编程,C++ 完全兼容C C++有哪些特性,简述对他们的理解 封装.继承.多态 封装 将的事物抽象成一个个集合(也就是所说的类), ...
- 搭建 springboot selenium 网页文件转图片环境
1. 环境准备 需要有 chrome 浏览器 + chrome driver + selenium 客户端 离线 chrome 下载地址 # 64位 linux 系统 https://dl.googl ...
- subprocess.CalledProcessError: Command '['ninja', '-v']' returned non-zero exit status 1.
将anaconda环境下的 lib/python3.5/dist-packages/torch/utils/cpp_extension.py文件 将['ninja','-v']改成['ninja', ...
- C#LeetCode刷题之#11-盛最多水的容器(Container With Most Water)
问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3615 访问. 给定 n 个非负整数 a1,a2,...,an,每 ...
- C#LeetCode刷题之#58-最后一个单词的长度(Length of Last Word)
问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3927 访问. 给定一个仅包含大小写字母和空格 ' ' 的字符串, ...