网络流模板题。

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

#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. iOS 数据持久化(3):Core Data

    @import url(http://i.cnblogs.com/Load.ashx?type=style&file=SyntaxHighlighter.css); @import url(/ ...

  2. VNC服务端自动化配置脚本

    在使用阿里云的linux云主机,看到官方提供的远程连接服务器bash脚本,记录下来.       功能:自动修改系统源和安装vncserver相关的软件包,centos.redhat系列都是安装gno ...

  3. Linux系统下Memcached的安装以及自启动

    一.准备工作: 1.下载libevent:http://monkey.org/~provos/libevent/ (由于memcached与客户端的通信是借助libevent来实现的,所以此动作必须在 ...

  4. angularjs 将带标签的内容解析后返回

    参考地址:http://okashii.lofter.com/post/1cba87e8_29e0fab 引入angular-sanitize.js文件 注入ngSanitize 页面数据绑定 ng- ...

  5. MVC ViewData和ViewBag

        视图数据可以通过ViewBag属性访问,它主要是为了从Controller到view进行传值用的,类似有所使用的ViewData[] 字典类.对于ViewBag是如此的强大,意味着你能动态的s ...

  6. C#获取类中所有方法

    var t = typeof(HomeController); //获取所有方法 System.Reflection.MethodInfo[] methods = t.GetMethods(); // ...

  7. CentOS7下安装SVN服务端

    CentOS7下安装SVN服务 1. yum命令即可方便的完成安装# sudo yum install subversion 测试安装是否成功:# svnserve --version 更改svn的默 ...

  8. CI 笔记3 (easyui 的layout布局,最小化layout原型)

    在做easyui的layout的布局时,最小化一个原型,分2步,一个是div的父标签,一个是body做父标签,全屏的. 步骤分别为: 在设置的5个区中,div的最后一个,必须是data-options ...

  9. 百度ios 开发面试题

    百度移动云可穿戴部门的面试经历,面试官都非常热情友好,一上来到弄的我挺不好意思的.下面记录一下自己的面试过程,因为我真的没啥面试经验,需要总结下. 1面 Objective C runtime lib ...

  10. iOS 里面如何使用第三方应用程序打开自己的文件,调用wps其他应用打开当前应用里面的的ppt doc xls

    我们的自己的应用里面经常涉及的要打开ppt doc,这样的功能,以前总以为iOS沙盒封闭化,不可能实现,后来终于解决了 使用 UIDocumentInteractionController 来解决这一 ...