网络流之最大流的基础题;

可以使用dinic算法和EK算法:

分别对着模板敲了一遍:

dinic:

 #include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
#define maxn 205
#define inf 0xfffffff
using namespace std; int map[maxn][maxn],level[maxn];
int n,m; int bfs(int s)
{
memset(level,,sizeof level);
queue<int>q;
q.push(s);
level[s]=;
while(!q.empty())
{
int now=q.front();
q.pop();
for(int i=; i<=m; i++)
if(!level[i]&&map[now][i]>)
{
level[i]=level[now]+;
q.push(i);
}
}
return level[m]!=;
} int dfs(int s,int cp)
{
int tmp=cp,t;
if(s==m) return cp;
for(int i=; i<=m&&tmp; i++)
if(level[i]==level[s]+&&map[s][i]>)
{
t=dfs(i,min(tmp,map[s][i]));
map[s][i]-=t;
map[i][s]+=t;
tmp-=t;
}
return cp-tmp;
} int main()
{
int a,b,f;
while(scanf("%d%d",&n,&m)!=EOF)
{
memset(map,,sizeof map);
for(int i=; i<n; i++)
{
scanf("%d%d%d",&a,&b,&f);
map[a][b]+=f;
}
int ans=,flow=;
while(bfs())
{
while(flow=dfs(,inf))
ans+=flow;
}
printf("%d\n",ans);
}
return ;
}

EK:

 #include<cstdio>
#include<cstring>
#include<algorithm>
#define maxn 205
#include<queue>
using namespace std; int map[maxn][maxn],p[maxn],n,m;
bool flag[maxn];
bool bfs()
{
queue<int>q;
memset(flag,,sizeof flag);
memset(p,-,sizeof p);
q.push();
flag[]=;
while(!q.empty())
{
int now=q.front();
q.pop();
if(now==m) return ;
for(int i=; i<=m; i++)
if(map[now][i]>&&!flag[i])
{
flag[i]=;
p[i]=now;
q.push(i);
}
}
return ;
} void ek()
{
int u,flow=,t;
while(bfs())
{
t=;
u=m;
while(p[u]!=-)
{
t=min(t,map[p[u]][u]);
u=p[u];
}
flow+=t;
u=m;
while(p[u]!=-)
{
map[p[u]][u]-=t;
map[u][p[u]]+=t;
u=p[u];
}
}
printf("%d\n",flow);
} int main()
{
int a,b,f;
while(scanf("%d%d",&n,&m)!=EOF)
{
memset(map,,sizeof map);
while(n--)
{
scanf("%d%d%d",&a,&b,&f);
map[a][b]+=f;
}
ek();
}
return ;
}

poj 1273的更多相关文章

  1. UVA 820 --- POJ 1273 最大流

    找了好久这两个的区别...UVA820 WA了 好多次.不过以后就做模板了,可以求任意两点之间的最大流. UVA 是无向图,因此可能有重边,POJ 1273是有向图,而且是单源点求最大流,因此改模板的 ...

  2. POJ 1273 Drainage Ditches(网络流dinic算法模板)

    POJ 1273给出M条边,N个点,求源点1到汇点N的最大流量. 本文主要就是附上dinic的模板,供以后参考. #include <iostream> #include <stdi ...

  3. POJ 1273 网络流(最大流)模板

    http://poj.org/problem?id=1273 这道题很值得反思,弄了一下午,交上去先是一直编译错误,而在本地运行没有问题, 原因可能是oj的编译器版本老旧不支持这样的写法 G[from ...

  4. poj 1273 最大流

    题目链接:http://poj.org/problem?id=1273 a.EK算法:(Edmond-Karp): 用BFS不断找增广路径,当找不到增广路径时当前流量即为最大流. b.dinic算法: ...

  5. poj 1273 Drainage Ditches 最大流入门题

    题目链接:http://poj.org/problem?id=1273 Every time it rains on Farmer John's fields, a pond forms over B ...

  6. poj 1273 Drainage Ditches(最大流)

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

  7. POJ 1273 - Drainage Ditches - [最大流模板题] - [EK算法模板][Dinic算法模板 - 邻接表型]

    题目链接:http://poj.org/problem?id=1273 Time Limit: 1000MS Memory Limit: 10000K Description Every time i ...

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

    链接: http://poj.org/problem?id=1273 代码: //Dinic #include<stdio.h> #include<string.h> #inc ...

  9. (网络流 模板 Edmonds-Karp)Drainage Ditches --POJ --1273

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

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

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

随机推荐

  1. Android 自定义View修炼-【2014年最后的分享啦】Android实现自定义刮刮卡效果View

    一.简介: 今天是2014年最后一天啦,首先在这里,我祝福大家在新的2015年都一个个的新健康,新收入,新顺利,新如意!!! 上一偏,我介绍了用Xfermode实现自定义圆角和椭圆图片view的博文& ...

  2. Java基础知识强化之IO流笔记60:打印流 之 改进复制文本文件的案例

    1. 使用打印流改进复制文本文件的案例 2. 代码示例: package cn.itcast_03; import java.io.BufferedReader; import java.io.Buf ...

  3. 3.linux安装vsftpd服务

    1.首先查看本地是否安装了vsftpd rpm -qa |grep vsftpd 2.安装vsftpd: yum install vsftpd 3.查询当前ftp状态 chkconfig --list ...

  4. C#里面比较时间大小三种方法

    1.比较时间大小的实验 string st1="12:13";string st2="14:14";DateTime dt1=Convert.ToDateTim ...

  5. android测试分析1

    Android测试框架,开发环境中集成的一部分,提供一个架构和强有力的工具 可以帮助测试你的应用从单元到框架的每个方面. 测试框架有这些主要特征: 1.Android测试组件基于Junit.你可以使用 ...

  6. ora01033 oracle正在初始化或关闭

    toad连数据库报错: ORA-01033: ORACLE initialization or shutdown in progress 解决方法: 1)开始-运行-cmd 2)命令行中输入SQLPL ...

  7. 时空分割的画面--用xcode命令行回忆turbo c

    大学时期曾经玩过turbo c的同学,可以用xcode命令行写写c程序,回味一下吧:) 1. 首先在终端输入,touch main.c 新建文件 2. 编辑main.c内容,写一段简单代码 #incl ...

  8. JS模版引擎[20行代码实现模版引擎读后感]

    曾经阅读过<只有20行JAVASCRIPT代码, 手把手教你写一个页面模版引擎>这篇文章, 对其中实现模版的想法实在膜拜, 于是有了这篇读后感, 谈谈自己对模版引擎的理解, 以及用自己的语 ...

  9. 九度OJ 1437 To Fill or Not to Fill -- 贪心算法

    题目地址:http://ac.jobdu.com/problem.php?pid=1437 题目描述: With highways available, driving a car from Hang ...

  10. centos 6.5 安装jenkins

    Installation sudo wget -O /etc/yum.repos.d/jenkins.repo http://pkg.jenkins-ci.org/redhat/jenkins.rep ...