网络流模板题。

==========================================================================================================

#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 (网络流 最大流)的更多相关文章

  1. poj 1273 Drainage Ditches 网络流最大流基础

    Drainage Ditches Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 59176   Accepted: 2272 ...

  2. poj 1273 Drainage Ditches(最大流)

    http://poj.org/problem?id=1273 Drainage Ditches Time Limit: 1000MS   Memory Limit: 10000K Total Subm ...

  3. POJ 1273 Drainage Ditches (网络最大流)

    http://poj.org/problem? id=1273 Drainage Ditches Time Limit: 1000MS   Memory Limit: 10000K Total Sub ...

  4. poj 1273 Drainage Ditches【最大流入门】

    Drainage Ditches Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 63924   Accepted: 2467 ...

  5. poj 1273 Drainage Ditches(最大流,E-K算法)

    一.Description Every time it rains on Farmer John's fields, a pond forms over Bessie's favorite clove ...

  6. POJ 1273 Drainage Ditches (网络流Dinic模板)

    Description Every time it rains on Farmer John's fields, a pond forms over Bessie's favorite clover ...

  7. POJ 1273 Drainage Ditches 网络流 FF

    Drainage Ditches Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 74480   Accepted: 2895 ...

  8. POJ 1273 Drainage Ditches【最大流】

    题意:给出起点是一个池塘,M条沟渠,给出这M条沟渠的最大流量,再给出终点是一条河流,问从起点通过沟渠最多能够排多少水到河流里面去 看的紫书的最大流,还不是很理解,照着敲了一遍 #include< ...

  9. POJ 1273 Drainage Ditches【最大流模版】

    题意:现在有m个池塘(从1到m开始编号,1为源点,m为汇点),及n条有向水渠,给出这n条水渠所连接的点和所能流过的最大流量,求从源点到汇点能流过的最大流量 Dinic #include<iost ...

随机推荐

  1. Android制作粒子爆炸特效

    简介 最近在闲逛的时候,发现了一款粒子爆炸特效的控件,觉得比较有意思,效果也不错. 但是代码不好扩展,也就是说如果要提供不同的爆炸效果,需要修改的地方比较多.于是我对源代码进行了一些重构,将爆炸流程和 ...

  2. Struts2 ValueStack

    一.作用 可以作为一个数据中转站,用在前台和后台数据传递 二.生命周期 ValueStack的生命周期是随着request的创建而创建,随request的销毁而销毁. 三.结构 OgnlValueSt ...

  3. java的各个队列之间的联系和区别是什么

    java的各个并发队列之间的联系和区别 java.util.concurrent是在并发编程中很常用的实用工具类 ArrayBlockingQueue, DelayQueue, LinkedBlock ...

  4. asp.net对word文档进行修改 对于使用word文档做模板编辑比较适用

    最近做项目,需要多word文档进行编辑并导出一个新的word,在最初的word编辑中留下特定的字符串用来替换,然后在本地生成一个新的word文档,并且不修改服务器中的word文档,这样才能保证服务器中 ...

  5. C# DataTable去除重复,极其简便、简单

    其中sourceDT是获取到的一个DataTable类型的集合对象 去重复使用方式: 实例化一个DataView对象 假设为dv,直接dv.ToTable()即可,ToTable中可为(true,&q ...

  6. 那些年,我们一起学WCF--(8)Single实例行为

    Single实例行为,类似于单件设计模式,所有可以客户端共享一个服务实例,这个服务实例是一个全局变量,该实例第一次被调用的时候初始化,到服务器关闭的时候停止. 设置服务为Single实例行为,只要设置 ...

  7. 用Ueditor存入数据库带HTML标签的文本,从数据库取出来后,anjular用ng-bind-html处理带HTML标签的文本

    ng.module('index-filters', []) .filter('trustHtml', function ($sce) { return function (input) { retu ...

  8. weChat聊天发送图片带有小尖角的实现

    weChat聊天发送图片带有小尖角的实现 1.#import <UIKit/UIKit.h>2.3.@interface JKShapeImage : UIView4.5.@propert ...

  9. JavaScript学习笔记--ES6学习(五) 数值的扩展

    ES6 对于数值类型 (Number) 进行了一下扩展: 1.对于二进制和八进制提供了新的写法 ES6对于二进制和八进制的数值提供了新的写法,分别用0b (或者0B) 和0o (或者0o) 表示.例如 ...

  10. 【转】WF事件驱动

    转自:http://www.cnblogs.com/Mayvar/category/315963.html 这系统的教程有代码可以下载 WF事件驱动(5) 摘要: 之前,我通过4篇文章介绍了在WF4中 ...