POJ 1273 Drainage Ditches | 最大流模板
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<queue>
#define N 250
#define M 250
#define INF 100000000
using namespace std;
int head[N],cur[N],lev[N],ecnt=1,S,T,n,m;
queue <int> q;
struct adj
{
int nxt,v,w;
}e[2*M];
void add(int u,int v,int w)
{
e[++ecnt].v=v,e[ecnt].w=w,e[ecnt].nxt=head[u],head[u]=ecnt;
e[++ecnt].v=u,e[ecnt].w=0,e[ecnt].nxt=head[v],head[v]=ecnt;
}
inline int bfs()
{
int u,v;
for (int i=S;i<=T;i++)
lev[i]=-1,cur[i]=head[i];
q.push(S),lev[S]=0;
while (!q.empty())
{
u=q.front();
for (int i=head[u];i;i=e[i].nxt)
{
if (e[i].w>0 && lev[v=e[i].v]==-1)
lev[v]=lev[u]+1,q.push(v);
}
q.pop();
}
return lev[T]!=-1;
}
inline int Dinic(const int &u,const int &flow)
{
if (u==T) return flow;
int res=0,v,delta;
for (int &i=cur[u];i;i=e[i].nxt)
{
if (e[i].w>0 && lev[u]<lev[v=e[i].v])
{
delta=Dinic(v,min(e[i].w,flow-res));
if (delta)
{
e[i].w-=delta;e[i^1].w+=delta;
res+=delta;if (res==flow) break;
}
}
}
if (res!=flow) lev[u]=-1;
return res;
}
inline int Maxflow()
{
int ans=0;
while (bfs()) ans+=Dinic(S,INF);
return ans;
}
inline void init()
{
memset(head,0,sizeof(head));
ecnt=1;
}
int main()
{
while (scanf("%d%d",&n,&m)!=EOF)
{
init();
for (int i=1,x,y,z;i<=n;i++)
{
scanf("%d%d%d",&x,&y,&z);
add(x,y,z);
}
S=1,T=m;
printf("%d\n",Maxflow());
}
return 0;
}
POJ 1273 Drainage Ditches | 最大流模板的更多相关文章
- POJ 1273 - Drainage Ditches - [最大流模板题] - [EK算法模板][Dinic算法模板 - 邻接表型]
题目链接:http://poj.org/problem?id=1273 Time Limit: 1000MS Memory Limit: 10000K Description Every time i ...
- 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(最大流 Edmonds-Karp )
题目链接:poj1273 Drainage Ditches 呜呜,今天自学网络流,看了EK算法,学的晕晕的,留个简单模板题来作纪念... #include<cstdio> #include ...
- POJ 1273 Drainage Ditches (网络流Dinic模板)
Description Every time it rains on Farmer John's fields, a pond forms over Bessie's favorite clover ...
- POJ 1273 Drainage Ditches(最大流Dinic 模板)
#include<cstdio> #include<cstring> #include<algorithm> using namespace std; int n, ...
- POJ 1273 Drainage Ditches 最大流
这道题用dinic会超时 用E_K就没问题 注意输入数据有重边.POJ1273 dinic的复杂度为O(N*N*M)E_K的复杂度为O(N*M*M)对于这道题,复杂度是相同的. 然而dinic主要依靠 ...
- poj 1273 Drainage Ditches(最大流)
http://poj.org/problem?id=1273 Drainage Ditches Time Limit: 1000MS Memory Limit: 10000K Total Subm ...
- POJ 1273 Drainage Ditches (网络最大流)
http://poj.org/problem? id=1273 Drainage Ditches Time Limit: 1000MS Memory Limit: 10000K Total Sub ...
- POJ 1273 Drainage Ditches(网络流dinic算法模板)
POJ 1273给出M条边,N个点,求源点1到汇点N的最大流量. 本文主要就是附上dinic的模板,供以后参考. #include <iostream> #include <stdi ...
随机推荐
- C++ 类型转换(conv.)
隐式类型转换 总结自:隐式类型转换&算数运算符 定义:隐式类型转换是指使用了与表达式规定或当前语境不相符的类型时所进行的类型转换,但是要注意,可能会存在转换出现歧义,从而无法通过编译;一切带有 ...
- Hibernate基础学习2
Hibernate基础学习2 测试hibernate的一级缓存,事务以及查询语句 1)Hibernate的一些相关概念 hibernate的一级缓存 1)缓存是为了提高该框架对数据库的查询速度 2)一 ...
- PHP实现的一个时间帮助类
背景 工作的过程中经常会遇到各种时间类的操作,因此封装了一个帮助工具类,提高代码的复用率 主要功能 根据相差的天数获取连续的时间段 /** * 根据相差的天数获取所有连续的时间段 * @param $ ...
- JAVAOOP集合框架
集合框架三大内容:对外的接口.接口的实现和对集合运算的算法 集合有三大类接口:List.Set.Map 共同点:都是集合接口,都可以用来存储很多对象 不同:Collection接口存储一组不唯一(允许 ...
- Shell学习——变量
1.在Bash中,每一个变量的值都是字符串 2.查看某个进程的环境变量 cat /proc/$PID/environ | tr '\0' '\n' 3.变量赋值 3.1.var=value,注意var ...
- html页面 加载完成后再刷新 一次
主要用于第一次加载页面有部分加载bug,再刷新一次即可正常运行. 简单粗暴直接上代码,不带参数,0影响 <Script>function refresh(){ url = location ...
- python——numpy_1图像基本操作
1.图像的数组表示: from PIL import Image from pylab import * from numpy import * im = array(Image.open('E:\P ...
- POJ:2976-Dropping tests(二分平均值)
Dropping tests Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 15508 Accepted: 5418 Descr ...
- http与www服务精解
TCP/IP 协议介绍 在介绍 HTTP 协议之前,先简单说一下TCP/IP协议的相关内容.TCP/IP协议是分层的,从底层至应用层分别为:物理层.链路层.网络层.传输层和应用层,如下图所示: 从应用 ...
- sparkStreaming统计各平台最近一分钟实时注册收入 时间段,平台,金额,订单数
样例数据: __clientip=10.10.9.153&paymentstatus=0&__opip=&memberid=89385239&iamount=1& ...