poj Drainage Ditches(最大流入门)
| Time Limit: 1000MS | Memory Limit: 10000K | |
| Total Submissions: 85250 | Accepted: 33164 |
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
#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
#include<vector>
#define MAX 205
#define INF 0x3f3f3f3f
#define pb push_back
using namespace std;
struct edge{
int to,cap,rev;
edge(){}
edge(int to,int cap,int rev):to(to),cap(cap),rev(rev){}
};
vector<edge>G[MAX];
bool used[MAX];
void add_edge(int from,int to,int cap)
{
G[from].pb(edge(to,cap,G[to].size()));
G[to].pb(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];
//cout<<e.to<<endl;
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 ;
}
int max_flow(int s,int t)
{
int flow=;
for(;;)
{
memset(used,,sizeof(used));
int f=dfs(s,t,INF);
if(f==)
return flow;
flow+=f;
}
}
int main()
{
int n,m;
while(cin>>n>>m){
for(int i=;i<n;i++)
G[i].clear();
for(int i=;i<n;i++)
{
int u,v,cap;
cin>>u>>v>>cap;
add_edge(u,v,cap);
}
cout<<max_flow(,m)<<endl;
}
}
Ford_Fulkerson
#include<string.h>
#include<iostream>
#include<algorithm>
#include<vector>
#include<queue>
using namespace std;
const int maxn = ;
const int INF = 0x3f3f3f3f; struct Edge
{
int from,to,cap,flow;
Edge(){}
Edge(int from,int to,int cap,int flow):from(from),to(to),cap(cap),flow(flow){}
}; struct Dinic
{
int n,m,s,t; //结点数,边数(包括反向弧),源点与汇点编号
vector<Edge> edges; //边表 edges[e]和edges[e^1]互为反向弧
vector<int> G[maxn]; //邻接表,G[i][j]表示结点i的第j条边在e数组中的序号
bool vis[maxn]; //BFS使用,标记一个节点是否被遍历过
int d[maxn]; //d[i]表从起点s到i点的距离(层次)
int cur[maxn]; //cur[i]表当前正访问i节点的第cur[i]条弧 void init(int n,int s,int t)
{
this->n=n,this->s=s,this->t=t;
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]=true;
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]=true;
d[e.to] = d[x]+;
Q.push(e.to);
}
}
}
return vis[t];
} //a表示从s到x目前为止所有弧的最小残量
//flow表示从x到t的最小残量
int DFS(int x,int a)
{
if(x==t || a==)return a;
int flow=,f;//flow用来记录从x到t的最小残量
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;
}
}
if(!flow) d[x] = -;///炸点优化
return flow;
} int Maxflow()
{
int flow=;
while(BFS())
{
memset(cur,,sizeof(cur));
flow += DFS(s,INF);
}
return flow;
}
}Di;
int main()
{
int n,m; while(cin>>n>>m){
Di.init(n,,m);
for(int i=;i<n;i++)
{
int v,u,rap;
cin>>u>>v>>rap;
Di.AddEdge(u,v,rap);
}
cout<<Di.Maxflow()<<endl;
}
}
Dinic
poj Drainage Ditches(最大流入门)的更多相关文章
- 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(最大流)
http://poj.org/problem?id=1273 Drainage Ditches Time Limit: 1000MS Memory Limit: 10000K Total Subm ...
- POJ 1273 - Drainage Ditches - [最大流模板题] - [EK算法模板][Dinic算法模板 - 邻接表型]
题目链接:http://poj.org/problem?id=1273 Time Limit: 1000MS Memory Limit: 10000K Description Every time i ...
- (网络流 模板 Edmonds-Karp)Drainage Ditches --POJ --1273
链接: http://poj.org/problem?id=1273 Drainage Ditches Time Limit: 1000MS Memory Limit: 10000K Total ...
- POJ 1273 Drainage Ditches (网络最大流)
http://poj.org/problem? id=1273 Drainage Ditches Time Limit: 1000MS Memory Limit: 10000K Total Sub ...
- POJ 1273 Drainage Ditches题解——S.B.S.
Drainage Ditches Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 67823 Accepted: 2620 ...
- POJ 1273 Drainage Ditches
Drainage Ditches Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 67387 Accepted: 2603 ...
- POJ 1273 Drainage Ditches -dinic
dinic版本 感觉dinic算法好帅,比Edmonds-Karp算法不知高到哪里去了 Description Every time it rains on Farmer John's fields, ...
- Drainage Ditches 分类: POJ 图论 2015-07-29 15:01 7人阅读 评论(0) 收藏
Drainage Ditches Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 62016 Accepted: 23808 De ...
随机推荐
- 【问题解决方案】Markdown正文中慎用星号否则容易变斜体
参考链接: [学习总结]Markdown 使用的正确姿势:第九部分-斜体and加粗 原理: 注意: Markdown中,若在正文中使用星号,如乘号或者指针的星号时,需要特别注意 当一句话中包含两个或者 ...
- vuex配置
import Vue from 'vue' import App from './App.vue' import router from './router' import store from '. ...
- Python Web开发:使用Django框架创建HolleWorld项目
开发环境搭建 Python环境安装 下载地址:https://www.python.org/downloads// Django安装 打开Windows CMD输入pip install django ...
- MySQL01---简介及安装
目录 MySQL简介及安装 DBA工作内容 DBA的职业素养 MySQL简介及安装 01 什么是数据? 02 什么是数据库管理系统 03 数据库管理系统种类 关系型数据库(RDMS)与非关系型数据库( ...
- java ArrayList的基本使用
package java06; /* 数组的长度是不可以发生改变的 Arraylist 集合的长度可以发生改变 对于ArrayList来说,有一个尖括号<E>代表泛型 泛型:就是装在结合中 ...
- spring cloud学习笔记五 网关服务zuul
网关服务是指,客户端发送的请求不用直接访问特定的微服务接口,而且是经过网关服务的接口进行交互,网关服务再去到特定的微服务中进行调用. 网关服务的路由功能和Nginx的反向代理一样,所有的服务都先会 ...
- spark 计算结果写入mysql 案例及常见问题解决
package com.jxd import org.apache.spark.SparkContextimport org.apache.spark.SparkConfimport java.sql ...
- MySQL添加主键、索引
查看索引 SHOW INDEX FROM 数据库表名 比如:SHOW INDEX FROM order_info; 添加索引 alter table 数据库add index 索引名称(数据库字段 ...
- 【leetcode】44. Wildcard Matching
题目如下: 解题思路:本题和[leetcode]97. Interleaving String非常相似,同样可以采用动态规划的方法.记dp[i][j] = 1或者0 表示pattern[0:i]是否匹 ...
- 百度小程序-图片画廊-使用previewImage方法实现
.swan <!-- 轮播图 S--> <view class="swiper-box"> <swiper style='height:{{swipe ...