poj 1273
网络流之最大流的基础题;
可以使用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的更多相关文章
- UVA 820 --- POJ 1273 最大流
找了好久这两个的区别...UVA820 WA了 好多次.不过以后就做模板了,可以求任意两点之间的最大流. UVA 是无向图,因此可能有重边,POJ 1273是有向图,而且是单源点求最大流,因此改模板的 ...
- POJ 1273 Drainage Ditches(网络流dinic算法模板)
POJ 1273给出M条边,N个点,求源点1到汇点N的最大流量. 本文主要就是附上dinic的模板,供以后参考. #include <iostream> #include <stdi ...
- POJ 1273 网络流(最大流)模板
http://poj.org/problem?id=1273 这道题很值得反思,弄了一下午,交上去先是一直编译错误,而在本地运行没有问题, 原因可能是oj的编译器版本老旧不支持这样的写法 G[from ...
- poj 1273 最大流
题目链接:http://poj.org/problem?id=1273 a.EK算法:(Edmond-Karp): 用BFS不断找增广路径,当找不到增广路径时当前流量即为最大流. b.dinic算法: ...
- 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 ...
- (网络流 模板 Dinic) Drainage Ditches --POJ --1273
链接: http://poj.org/problem?id=1273 代码: //Dinic #include<stdio.h> #include<string.h> #inc ...
- (网络流 模板 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 ...
随机推荐
- Mac安装Mysql无法登录
Mac安装的Mysql5.7.10 执行mysql -u root -p 就要求输入密码,但是新安装是没密码的.然后就会报错. ERROR 1045 (28000): Access denied fo ...
- 第七篇:web之前端之ajax
前端之ajax 前端之ajax 本节内容 ajax介绍 原生js实现ajax jquery实现ajax json 跨域请求 1. ajax介绍 AJAX(Asynchronous Javascri ...
- 几个MVC属性
1 用于显示提示字符串 [Required(ErrorMessage="请输入类型名称")] public string ArticleTypeName { get; set; ...
- java 乱码问题-Dfile.encoding=UTF-8
http://blog.csdn.net/telnetor/article/details/5555361 问题描述:程序涉及到国际化问题,httpclient抓回来的数据乱七八糟的乱码,在转了几次编 ...
- UIKit各类概述
1.UIAcceleration: 被叫做加速事件的一个UIAcceleration类的实例是用来代表即时的三维加速数据.为了接收重力加速度,要注册一个应用应用程序作为一个共享UIAccelerate ...
- oc ios 中文字符串 进行 sha1加密 错误?
我在网上找到了一个oc版加密的工具类,但是加密中文就出现大问题 const char *cstr = [self cStringUsingEncoding:encoding]; NSData *dat ...
- 转JSONObject put,accumulate,element的区别
public Object put (Object key, Object value) 将value映射到key下.如果此JSONObject对象之前存在一个value在这个key下,当前的 ...
- 暑假集训(2)第二弹 ----- The Suspects(POJ1611)
B - The Suspects Crawling in process... Crawling failed Time Limit:1000MS Memory Limit:20000KB ...
- mysql SELECT INTO OUTFILE ,can't create file (转)
原文 http://blog.sina.com.cn/s/blog_6a5e34ad0100zfbi.html (转) 命令行模式进入mysql #mysql -uroot -p12345 #sel ...
- ASP.NET MVC5 easyui 之 treegrid 初用记录
菜鸟初次使用,参考论坛中介绍的方法仍走了一些弯路,把自己遇到的问题记录下来. 1.必须定义根节点: 2.根节点一个或多个均可: 4.根节点的父节点属性不必定义,或者定义为0: 5.各级子节点的父节点属 ...