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 ...
随机推荐
- 解锁Dagger2使用姿势(二) 之带你理解@Scope
关于Dagger2使用的基础如果你还不了解,可以参考我的上一篇文章解锁Dagger2使用姿势(一),这有助于你理解本篇文章. OK,我们在上篇文章介绍另外Dagger2使用过程中四个基本的注解,分别是 ...
- 从源码角度深入理解LayoutInflater
关于LayoutInflater,在开发中经常会遇到,特别是在使用ListView的时候,这个几乎是必不可少.今天我们就一起来探讨LayoutInflater的工作原理. 一般情况下,有两种方式获得一 ...
- 《app研发录》第一章 重构,夜未眠笔记
1.1 重新规划android的项目结构 重新规划android的目录结构分两步: 1.建立AndroidLib类库,将与业务无关的逻辑转移到AndroidLib. acitivity存放的是 ...
- 需要设置jdk的三处位置:
需要设置jdk的三处位置:1.tomcat需要一个JDK : Windows--->Preferences--->MyEclipse--->Servers--->Tomcat- ...
- 解决DataTable中的DataColumn类型默认为int类型时, 导致不能修改其列值为其他类型的解决办法
问题起因: 扔给数据库一条select * from [表名] , 得到一个DataTable, 发现有一列status状态的DataColumn的类型是int,然后我想换成字典表里的文字描述,然后就 ...
- SQL Server 2008导入、导出数据库
SQL Server 2008数据库的导入.导出和Mysql的导出还有一定的区别,刚开始的时候完全摸不到方向,利用Microsoft SQL Server Management Studio进行导入. ...
- ios 可变参数(va_list,va_start,va_end)
例如:UIAlertView的init方法中的otherButtonTitles:(NSString *)otherButtonTitles, ...等多个可变参数. ios实现传递不定长的多个参数的 ...
- PHP学习笔记(五)
关于Response header 的一些小知识: Host :address url, host 是浏览器给服务器提供的address标识.由于http协议是无状态的,服务器需要根据host的这个 ...
- ASP.NET数据绑定控件
数据绑定控件简介 数据绑定分为:数据源 和 数据绑定控件 两部分,数据绑定控件通过数据源来获得数据,通过数据源来隔离数据提供者和数据使用者,数据源有:SqlDataSource,AccessDataS ...
- 认识linux权限
首先,我们来了解下linux系统的用户和用户组 场景:公司里有两个项目组:小组A和小组B:A.B.C是小组A的成员,甲.乙是小组B的成员.为了保密起见,小组内的进度.文档.程序都有小组内公开.比如小组 ...