poj 1273 Drainage Ditches (网络流 最大流)
网络流模板题。
==========================================================================================================
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<cmath>
#include<queue>
using namespace std;
typedef long long LL;
const LL INF = 1e9+;
const LL maxn = ;
const LL MOD = 1e9+;
int n, m, Head[maxn], k;
int Pre[maxn];
bool vis[maxn];
struct Edge
{
int from, to, w;
int next;
}edge[maxn*]; void Init()
{
memset(Head, -, sizeof(Head));
memset(Pre, -, sizeof(Pre));
}
void AddEdge(int s,int e,int w)
{
edge[k].from = s;
edge[k].to = e;
edge[k].w = w;
edge[k].next = Head[s];
Head[s] = k ++;
}
bool BFS(int s,int e)///从源点到汇点找到一条路径
{
memset(vis, false, sizeof(vis));
queue<int> Q;
Q.push(s);
Pre[s] = -;
vis[s] = true;
while( Q.size() )
{
int v = Q.front();
Q.pop();
if(v == e) return true; for(int i=Head[v]; i != -; i = edge[i].next)
{
int to = edge[i].to;
if( !vis[to] && edge[i].w )
{
vis[to] = true;
Pre[to] = i;
Q.push(to);
}
}
}
return false;
} int Karp(int s,int e)
{
int ans = ;
while( BFS(s, e) )///如果能找到路径就一直找
{
int MinFlow = INF, Cur = Pre[e]; while(Cur != -)
{
MinFlow = min(MinFlow, edge[Cur].w);
Cur = Pre[edge[Cur].from];
}
ans += MinFlow;
Cur = Pre[e]; while(Cur != -)
{
edge[Cur].w -= MinFlow;
edge[Cur^].w += MinFlow;
Cur = Pre[edge[Cur].from];
}
}
return ans;
} int main()
{
while(cin >> m >> n)
{
int s, e, w;
Init();
for(int i=; i<m; i++)
{
scanf("%d %d %d", &s, &e, &w);
AddEdge(s, e, w);
AddEdge(e, s, );///增加的反向边
} printf("%d\n", Karp(, n) );
}
return ;
}
==========================================================================================================
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<cmath>
#include<queue>
using namespace std;
typedef long long LL;
const LL INF = 1e9+;
const LL maxn = ;
const LL MOD = 1e9+;
int n, m, Head[maxn], k;
int Depth[maxn];
bool vis[maxn];
struct node
{
int s, e, flow;
int next;
}edge[maxn*]; void AddEdge(int s,int e,int flow)
{
edge[k].s = s;
edge[k].e = e;
edge[k].flow = flow;
edge[k].next = Head[s];
Head[s] = k ++;
} bool BfsDepth(int Star,int End)
{
memset(Depth, , sizeof(Depth) );
queue<int> Q;
Depth[Star] = ;
Q.push(Star); while( Q.size() )
{
int s = Q.front();
Q.pop();
if(s == End) return true; for(int i=Head[s]; i != -; i=edge[i].next)
{
int e = edge[i].e;
if(!Depth[e] && edge[i].flow)
{
Q.push(e);
Depth[e] = Depth[s] + ;
}
}
}
return false;
} int DFS(int s,int MaxFlow)///从s点发出的最大流量是MaxFlow
{
if(s == n) return MaxFlow;
int sFlow = ;///sFlow 从 for(int i=Head[s]; i != -; i = edge[i].next)
{
int e = edge[i].e, flow = edge[i].flow; if(Depth[s]+ == Depth[e] && flow)///到达下一层
{
flow = min(MaxFlow-sFlow, flow);
flow = DFS(e, flow);
edge[i].flow -= flow;
edge[i^].flow += flow;
sFlow += flow;
if(sFlow == MaxFlow)
break;
}
}
if(sFlow == )
Depth[s] = ;
return sFlow;
} int Dinic(int s,int e)
{
int ans = ;
while(BfsDepth(s,e) == true)
{
ans += DFS(s, INF);
}
return ans;
} int main()
{
while(scanf("%d %d",&m, &n) != EOF)
{
int s, e, w;
memset(Head, -, sizeof(Head));
k = ;
for(int i=; i<m; i++)
{
scanf("%d %d %d", &s, &e, &w);
AddEdge(s, e, w);
AddEdge(e, s, );///添加反向边
}
printf("%d\n", Dinic(, n) );
}
return ;
}
poj 1273 Drainage Ditches (网络流 最大流)的更多相关文章
- poj 1273 Drainage Ditches 网络流最大流基础
Drainage Ditches Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 59176 Accepted: 2272 ...
- 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【最大流入门】
Drainage Ditches Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 63924 Accepted: 2467 ...
- poj 1273 Drainage Ditches(最大流,E-K算法)
一.Description Every time it rains on Farmer John's fields, a pond forms over Bessie's favorite clove ...
- 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 网络流 FF
Drainage Ditches Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 74480 Accepted: 2895 ...
- POJ 1273 Drainage Ditches【最大流】
题意:给出起点是一个池塘,M条沟渠,给出这M条沟渠的最大流量,再给出终点是一条河流,问从起点通过沟渠最多能够排多少水到河流里面去 看的紫书的最大流,还不是很理解,照着敲了一遍 #include< ...
- POJ 1273 Drainage Ditches【最大流模版】
题意:现在有m个池塘(从1到m开始编号,1为源点,m为汇点),及n条有向水渠,给出这n条水渠所连接的点和所能流过的最大流量,求从源点到汇点能流过的最大流量 Dinic #include<iost ...
随机推荐
- iOS UIKit:viewController之Segues (4)
@import url(http://i.cnblogs.com/Load.ashx?type=style&file=SyntaxHighlighter.css);@import url(/c ...
- css3 calc()
概述 CSS函数calc()可以用在任何一个需要<length>的地方.有了calc(),你可以通过计算来决定一个对象的大小和形状. 你还可以在一个calc()内部嵌套另一个calc(). ...
- Android开发手记(26) Java多线程的实现
随着多核CPU的发展,多线程编程显得越来越重要,本文将对Java中的多线程编程进行一些简单的探讨. 1.继承Thread类 Java中,线程运行的基本单位是Thread,所以,我们可以通过继承Thre ...
- Android 巧妙实现图片和文字上下布局或者左右布局
最近去了一家新公司,然后开始做新的项目,看其代码发现了一个很巧妙的方法来实现图片在上面文字在下面的布局方式.只需要一个控件——RadioButton. 布局文件很简单,用来展示RadioBUtton的 ...
- OpenWrt的主Makefile工作过程
OpenWrt是一个典型的嵌入式Linux工程,了解OpenWrt的Makefile的工作过程对提高嵌入式Linux工程的开发能力有极其重要意义. OpenWrt的主Makefile文件只有100行, ...
- Hadoop的读写类调用关系_图示
- Convention插件的使用(会涉及content目录,jsp必须放入这个下面才能映射成功基于注解的配置)
http://blog.csdn.net/zclandzzq/article/details/7107816
- MyEclipse起步Tomcat报错“A configuration error occurred during…” MyEclipse起步Tomcat报错“A configuration error occurred during…”
- 【转】iOS-Core-Animation-Advanced-Techniques(四)
原文:http://www.cocoachina.com/ios/20150105/10812.html 隐式动画和显式动画 隐式动画 按照我的意思去做,而不是我说的. -- 埃德娜,辛普森 我们在第 ...
- Spring 实例化bean的方式
实例化bean的方式有三种: 1.用构造器来实例化 2.使用静态工厂方法实例化 3.使用实例工厂方法实例化 当采用构造器来创建bean实例时,Spring对class并没有特殊的要求, 我们通常使用的 ...