POJ 1273 Drainage Ditches -dinic
dinic版本
感觉dinic算法好帅,比Edmonds-Karp算法不知高到哪里去了
Description
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
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
Sample Input
5 4
1 2 40
1 4 20
2 4 20
2 3 30
3 4 10
Sample Output
50
/*Dinic*/
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<queue>
using namespace std;
const int inf=0x7fffffff;
int n,m;//路径数 结点数
int fl[][],dis[];
int bfs(){
// memset(dis,-1,sizeof(dis));
for(int i=;i<=m+;i++)dis[i]=-;
dis[]=;//原点层数为0
queue<int> q;
q.push();
while(!q.empty()){
int k=q.front();q.pop();
for(int i=;i<=m;i++){
if(fl[k][i]> &&dis[i]<){
dis[i]=dis[k]+;
q.push(i);
}
}
}
// return (dis[m]>0);
if(dis[m]>)return ;
else return ;
}
int dfs(int q,int mx){
if(q==m)return mx;
int i,a;
for(i=;i<=m;i++){
if(fl[q][i]> && dis[i]==dis[q]+ && (a=dfs(i,min(fl[q][i],mx))) ){
fl[i][q]+=a;
fl[q][i]-=a;
return a;
}
}
return ;
}
int main(){ int i,j,u,v,d;
while(scanf("%d%d",&n,&m)!=EOF){
memset(fl,,sizeof(fl)); for(i=;i<=n;i++){
scanf("%d%d%d",&u,&v,&d);
fl[u][v]+=d;
}
int ans=;
int res;
while(bfs()){
while(res=dfs(,inf))ans+=res;
}
printf("%d\n",ans);
}
return ;
}
POJ 1273 Drainage Ditches -dinic的更多相关文章
- poj 1273 Drainage Ditches(最大流)
http://poj.org/problem?id=1273 Drainage Ditches Time Limit: 1000MS Memory Limit: 10000K Total Subm ...
- POJ 1273 Drainage Ditches (网络最大流)
http://poj.org/problem? id=1273 Drainage Ditches Time Limit: 1000MS Memory Limit: 10000K Total Sub ...
- POJ 1273 - Drainage Ditches - [最大流模板题] - [EK算法模板][Dinic算法模板 - 邻接表型]
题目链接:http://poj.org/problem?id=1273 Time Limit: 1000MS Memory Limit: 10000K Description Every time i ...
- POJ 1273 Drainage Ditches(网络流dinic算法模板)
POJ 1273给出M条边,N个点,求源点1到汇点N的最大流量. 本文主要就是附上dinic的模板,供以后参考. #include <iostream> #include <stdi ...
- POJ 1273 Drainage Ditches (网络流Dinic模板)
Description Every time it rains on Farmer John's fields, a pond forms over Bessie's favorite clover ...
- poj 1273 Drainage Ditches 最大流入门题
题目链接:http://poj.org/problem?id=1273 Every time it rains on Farmer John's fields, a pond forms over B ...
- POJ 1273 Drainage Ditches题解——S.B.S.
Drainage Ditches Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 67823 Accepted: 2620 ...
- 网络流最经典的入门题 各种网络流算法都能AC。 poj 1273 Drainage Ditches
Drainage Ditches 题目抽象:给你m条边u,v,c. n个定点,源点1,汇点n.求最大流. 最好的入门题,各种算法都可以拿来练习 (1): 一般增广路算法 ford() #in ...
- POJ 1273 Drainage Ditches(网络流,最大流)
Description Every time it rains on Farmer John's fields, a pond forms over Bessie's favorite clover ...
随机推荐
- 【mybatis】1、入门CURD基本操作(环境搭建)
#1.基本环境 环境 版本 jdk 1.7.0_10 ide eclipse-jee-luna-SR2-win32-x86_64 maven 3.3.3 mybatis 3.2.7 mysql 5.1 ...
- 看ImplicitBackwardEulerSparse关于static solve的代码
当选择static solve的时候,求解的流程如下: 1.获得内力 2.qresidual = 外力-内力,qdelta = qresidual, qdelta的非约束元素赋给bufferConst ...
- Netty关闭客户端
在启动客户端的时候,我们一般会 channelFuture.channel().closeFuture().sync(); 这是一段阻塞的代码,除非链路断了,否则是不会终止的,我们可以在handler ...
- C# 类型运算符重载在类继承中的调用测试
这是一篇晦涩难懂的片面的研究 一,简单的继承层次 class CA { } class CB : CA{ } class CC : CB{ } } void Test(CA oa){//CATest ...
- cannot change version web module 3.0
eclipse如何修改dynamic web module version 由于从SVN down下来的工程java及tomcat 版本比本地高,导致工程不能编译,报以下错误. 1.Java comp ...
- ios 定位 监听是否跨入某个指定的区域
/*****监听用户是否进入和走出 在某个区域*****/ 1 #import "ViewController.h" #import <CoreLocation/CoreLo ...
- LeetCode 笔记28 Maximum Gap
Given an unsorted array, find the maximum difference between the successive elements in its sorted f ...
- 学习笔记——Maven实战(三)多模块项目的POM重构
重复,还是重复 程序员应该有狗一般的嗅觉,要能嗅到重复这一最常见的坏味道,不管重复披着怎样的外衣,一旦发现,都应该毫不留情地彻底地将其干掉.不要因为POM不是产品代码而纵容重复在这里发酵,例如这样一段 ...
- ASP.NET MVC 5 入门教程 (1) 新建项目
文章来源: Slark.NET-博客园 http://www.cnblogs.com/slark/p/mvc-5-get-started-create-project.html 下一节:ASP.NET ...
- C#基础之IL
1.实例解析IL 作为C#程序员,IL的作用不言而喻,首先来看一个非常简单的程序和它的IL解释图,通过这个程序的IL指令来简单的了解常见的IL指令是什么意思. class Program { stat ...