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 ...
随机推荐
- Apache Shiro权限框架在SpringMVC+Hibernate中的应用
在做网站开发中,用户权限必须要考虑的,权限这个东西很重要,它规定了用户在使用中能进行哪 些操作,和不能进行哪些操作:我们完全可以使用过滤器来进行权限的操作,但是有了权限框架之后,使用起来会非常的方便, ...
- linux vmstat 系统结果说明
可以用vmstat 显示系统负载等信息. 例如 vmstat 5 5,表示在T(5)秒时间内进行N(5)次采样. procs:r-->;在运行队列中等待的进程数b-->;在等待io的进程数 ...
- CentOS 7设置网络开机自动连接
用root登陆系统 修改/etc/sysconfig/network-scripts/ifcfg-enpxxxxxx(xxx)文件,其内容原本如下 TYPE=Ethernet BOOTPROTO=dh ...
- LUA GC 简单测试
function table.count(t) if type(t) ~= "table" then assert(false) return end for k, _ in pa ...
- 分享基于EF+MVC+Bootstrap的通用后台管理系统及架构(转)
http://www.cnblogs.com/guozili/p/3496265.html 基于EF+MVC+Bootstrap构建通用后台管理系统,集成轻量级的缓存模块.日志模块.上传缩略图模块.通 ...
- Objective-c复制对象的概念
- Android连接网络打印机,jSocket连接网络打印机
老大写的一个打印工具类,记录一下. package com.Ieasy.Tool; import android.annotation.SuppressLint; import java.io.IOE ...
- 修改Matlab 2012b默认工作路径
MATLAB的路径有多种,这里只讲一下启动时设置成MATLAB的用户的默认工作路径. 本人不想去改MATLAB的原来系统文件,而是尽量利用startup.m.这个文件默认在'/home/r/文档/MA ...
- [CareerCup] 12.5 Test a Pen 测试一支笔
12.5 How would you testa pen? 这道题让我们测试一支笔,我们需要问面试官许多问题来理解"who,what,where,when,how and why" ...
- 解答WPF中ComboBox SelectedItem Binding不上的Bug
正在做一个打印机列表,从中选择一个打印机(System.Printing) <ComboBox Width="150" ItemsSource="{Binding ...