poj1273 Drainage Ditches Dinic最大流
| Time Limit: 1000MS | Memory Limit: 10000K | |
| Total Submissions: 76000 | Accepted: 29530 |
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
Output
Sample Input
5 4
1 2 40
1 4 20
2 4 20
2 3 30
3 4 10
Sample Output
50
Source
/**
题目:poj1273 Drainage Ditches
链接:http://poj.org/problem?id=1273
题意:裸的最大流
思路:裸的最大流 */
#include<iostream>
#include<cstring>
#include<vector>
#include<cstdio>
#include<algorithm>
using namespace std;
const int INF = 0x3f3f3f3f;
typedef long long LL;
const int N = ;
struct edge{
int to, cap, rev;
};
vector<edge> G[N];
bool used[N];
void add_edge(int from,int to,int cap)
{
G[from].push_back((edge){to,cap,G[to].size()});
G[to].push_back((edge){from,,G[from].size()-}); }
int dfs(int v,int t,int f)
{
if(v==t) return f;
used[v] = true;
for(int i = ; i < G[v].size(); i++){
edge&e = G[v][i];
if(!used[e.to]&&e.cap>){
int d = dfs(e.to,t,min(f,e.cap));
if(d>){
e.cap -= d;
G[e.to][e.rev].cap += d;
return d;
}
}
}
return ;
}
LL max_flow(int s,int t)
{
LL flow = ;
for(;;){
memset(used, , sizeof used);
int f = dfs(s,t,INF);
if(f==) return flow;
flow+=f;
}
}
int main()
{
int n , m;
while(scanf("%d%d",&m,&n)==)
{
int u, v, cap;
for(int i = ; i <= n; i++) G[i].clear(); for(int i = ; i < m; i++){
scanf("%d%d%d",&u,&v,&cap);
add_edge(u,v,cap);
}
printf("%lld\n",max_flow(,n));
}
return ;
}
/**
题目:poj1273 Drainage Ditches
链接:http://poj.org/problem?id=1273
题意:
思路:Dinic算法解最大流 */
#include<iostream>
#include<cstring>
#include<vector>
#include<map>
#include<cstdio>
#include<algorithm>
#include<queue>
using namespace std;
const int INF = 0x3f3f3f3f;
typedef long long LL;
const int N = ;
struct Edge{
int from, to, cap, flow;
Edge(int u,int v,int c,int f):from(u),to(v),cap(c),flow(f){}
};
struct Dinic{
int n, m, s, t;
vector<Edge> edges;
vector<int> G[N];
bool vis[N];
int d[N];
int cur[N]; void init(int n)
{
this->n = n;
for(int i = ; i <= n; i++) G[i].clear();
edges.clear();
} void AddEdge(int from,int to,int cap)
{
edges.push_back(Edge(from,to,cap,));
edges.push_back(Edge(to,from,,));
m = edges.size();
G[from].push_back(m-);
G[to].push_back(m-);
} bool BFS(){
memset(vis, , sizeof vis);
queue<int> Q;
Q.push(s);
d[s] = ;
vis[s] = ;
while(!Q.empty()){
int x = Q.front(); Q.pop();
for(int i = ; i < G[x].size(); i++){
Edge &e = edges[G[x][i]];
if(!vis[e.to]&&e.cap>e.flow){
vis[e.to] = ;
d[e.to] = d[x]+;
Q.push(e.to);
}
}
}
return vis[t];
} int DFS(int x,int a){
if(x==t||a==) return a;
int flow = , f;
for(int &i = cur[x]; i < G[x].size(); i++){
Edge& e = edges[G[x][i]];
if(d[x]+==d[e.to]&&(f=DFS(e.to,min(a,e.cap-e.flow)))>){
e.flow += f;
edges[G[x][i]^].flow -= f;
flow += f;
a -= f;
if(a==) break;
}
}
return flow;
} int Maxflow(int s,int t){
this->s = s, this->t = t;
int flow = ;
while(BFS()){
memset(cur, , sizeof cur);
flow += DFS(s,INF);
}
return flow;
}
};
int main()
{
int n, m;
while(scanf("%d%d",&m,&n)==){
int from, to, cap;
Dinic dinic;
dinic.init(n);
for(int i = ; i < m; i++){
scanf("%d%d%d",&from,&to,&cap);
dinic.AddEdge(from,to,cap);
}
printf("%d\n",dinic.Maxflow(,n));
}
return ;
}
/**
题目:poj1273 Drainage Ditches
链接:http://poj.org/problem?id=1273
题意:裸的最大流
思路:EdmondsKarp最大流 */
#include<iostream>
#include<cstring>
#include<vector>
#include<cstdio>
#include<algorithm>
#include<queue>
using namespace std;
const int INF = 0x3f3f3f3f;
typedef long long LL;
const int N = ;
struct Edge{
int from, to, cap, flow;
Edge(int u,int v,int c,int f):from(u),to(v),cap(c),flow(f){}
};
struct EdmondsKarp
{
int n, m;
vector<Edge>edges;
vector<int>G[N];
int a[N];
int p[N]; void init(int n){
for(int i = ; i<= n; i++) G[i].clear();
edges.clear();
}
void AddEdge(int from,int to,int cap){
edges.push_back(Edge(from,to,cap,));
edges.push_back(Edge(to,from,,));
m = edges.size();
G[from].push_back(m-);
G[to].push_back(m-);
}
int Maxflow(int s,int t)
{
int flow = ;
for(;;){
memset(a, , sizeof a);
queue<int> Q;
Q.push(s);
a[s] = INF;
while(!Q.empty()){
int x = Q.front(); Q.pop();
for(int i = ; i < G[x].size(); i++){
Edge& e = edges[G[x][i]];
if(!a[e.to]&&e.cap>e.flow){
p[e.to] = G[x][i];
a[e.to] = min(a[x],e.cap-e.flow);
Q.push(e.to);
}
}
if(a[t]) break;
}
if(!a[t]) break;
for(int u = t; u != s; u = edges[p[u]].from){
edges[p[u]].flow += a[t];
edges[p[u]^].flow -= a[t];
}
flow += a[t];
}
return flow;
}
};
int main()
{
int n, m;
while(scanf("%d%d",&m,&n)==)
{
EdmondsKarp ek;
ek.init(n);
int from, to, cap;
for(int i = ; i < m; i++){
scanf("%d%d%d",&from,&to,&cap);
ek.AddEdge(from,to,cap);
}
printf("%d\n",ek.Maxflow(,n));
}
return ;
}
poj1273 Drainage Ditches Dinic最大流的更多相关文章
- 2018.07.06 POJ1273 Drainage Ditches(最大流)
Drainage Ditches Time Limit: 1000MS Memory Limit: 10000K Description Every time it rains on Farmer J ...
- Drainage Ditches(Dinic最大流)
http://poj.org/problem?id=1273 用Dinic求最大流的模板题,注意会有重边. 邻接矩阵建图 #include<stdio.h> #include<str ...
- poj1273 Drainage Ditches (最大流模板)
http://poj.org/problem?id=1273 Dinic算法 这是一道最大流的经典题 最大流尽量应该用边表,优于邻接矩阵(所以我写了邻接矩阵版的之后又写了个边表) 用了新学的Dinic ...
- POJ1273:Drainage Ditches(最大流入门 EK,dinic算法)
http://poj.org/problem?id=1273 Description Every time it rains on Farmer John's fields, a pond forms ...
- POJ1273&&Hdu1532 Drainage Ditches(最大流dinic) 2017-02-11 16:28 54人阅读 评论(0) 收藏
Drainage Ditches Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- POJ-1273 Drainage Ditches 最大流Dinic
Drainage Ditches Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 65146 Accepted: 25112 De ...
- HDU 1532||POJ1273:Drainage Ditches(最大流)
pid=1532">Drainage Ditches Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/327 ...
- poj-1273 Drainage Ditches(最大流基础题)
题目链接: Drainage Ditches Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 67475 Accepted ...
- poj1273 Drainage Ditches
Drainage Ditches Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 68414 Accepted: 2648 ...
随机推荐
- inline-block 前世今生(转)
曾几何时,display:inline-block 已经深入「大街小巷」,随处可见 「display:inline-block; *display:inline; *zoom:1; 」这样的代码.如今 ...
- 7zip File: How to Uncompress 7z files on Ubuntu, Debian, Fedora
转:http://www.thegeekstuff.com/2010/04/7z-7zip-7za-file-compression/ Question: How do I uncompress a ...
- npm install -S -D -g 有什么区别
npm install module_name -S 即 npm install module_name --save 写入dependencies npm install modu ...
- 怎么在windows7系统我的电脑中添加快捷方式
在我的电脑中添加一些快捷方式,这样不用每次在开始菜单中去找了 2 选择开始菜单运行 3 输入:Regedit命令 4 进入路径地址:HKEY_LOCAL_MACHINE\SOFTWARE\Micros ...
- vue2计算属性computed
详见vue2.0 API<计算属性> 需求: 模板内的表达式是非常便利的,但是它们实际上只用于简单的运算.在模板中放入太多的逻辑会让模板过重且难以维护.例如: <div id=&qu ...
- JMS-activeMq发布订阅模式(非持久订阅)
Publisher的代码: import javax.jms.Connection; import javax.jms.ConnectionFactory; import javax.jms.Deli ...
- Jfinal极速开发微信系列教程(三)--------------对JSP的支持以及部署Tomcat运行异常问题
本文章主要解决以下问题: 1.Jfianl对JSP的支持2.Jfianl Maven项目部署到Tomcat,启动项目异常问题解决 第一个问题重现截图解决方案:1.在configConstant中添加视 ...
- 学习ajax总结
之前公司的ajax学习分享,做一点总结,加深记忆 什么是ajax? 异步的的js和xml,用js异步形式操作xml,工作主要是数据交互 借阅用户操作时间,减少数据请求,可以无刷新请求数据 创建一个对象 ...
- Django——Django中的QuerySet API 与ORM(对象关系映射)
首先名词解释. ORM: 对象关系映射(英语:Object Relational Mapping,简称ORM,或O/RM,或O/R mapping),是一种程序技术,用于实现面向对象编程语言里不同类型 ...
- Linux——Ubuntu下Sublime Text 2的安装
Sublime Text 2是一款共享软件,收费但可以永久免费试用的编辑器,价格是59个美刀,相信开发者一定不了解中国人,也不面对中国市场,.言归正传,ST2编辑功能强大,好评如潮,在Windows/ ...