【模板】dinic(网络流)
2016-03-13 15:11
525 查看
#include<iostream>
#include<cstdio>
#include<cstring>
#define MAXN 1000
#define INF 0x7fffffff
using namespace std;
int n,m;
int next[MAXN*2],fst[MAXN*2],to[MAXN*2];
int tail=1;
int q[MAXN],h[MAXN],c[MAXN*2];
int ans=0;
inline void add(int u,int v,int t)
{next[++tail]=fst[u],fst[u]=tail,to[tail]=v,c[tail]=t;}
bool bfs()
{
memset(h,-1,sizeof(h));
int t=0,head=1,now;
q[t]=1,h[1]=0; /////////////////////////////
while(t!=head)
{
now=q[t++],t%=MAXN;
for(int i=fst[now];i;i=next[i])
{
if(c[i]>0&&h[to[i]]==-1)
{
h[to[i]]=h[now]+1;
q[head++]=to[i],head%=MAXN;
}
}
}
if(h[m]==-1)return false; /////////////////////////////////////////m
return true;
}
int dfs(int x,int f)
{
if(x==m)return f;
int t1,use=0;
for(int i=fst[x];i;i=next[i])
{
if(c[i]>0&&h[to[i]]==h[x]+1)
{
t1=dfs(to[i],min(c[i],f-use));
use+=t1,c[i]-=t1,c[i^1]+=t1;
if(use==f)return f;
}
}
return use;
}
void dinic(){while(bfs()){ans+=dfs(1,INF);}}
int main()
{
scanf("%d%d",&n,&m);
int t1,t2,t3;
for(int i=1;i<=n;i++) ///////////////////////////n
{
scanf("%d%d%d",&t1,&t2,&t3);
add(t1,t2,t3),add(t2,t1,0);
}
dinic();
printf("%d\n",ans);
//while(1);
return 0;
}
#include<cstdio>
#include<cstring>
#define MAXN 1000
#define INF 0x7fffffff
using namespace std;
int n,m;
int next[MAXN*2],fst[MAXN*2],to[MAXN*2];
int tail=1;
int q[MAXN],h[MAXN],c[MAXN*2];
int ans=0;
inline void add(int u,int v,int t)
{next[++tail]=fst[u],fst[u]=tail,to[tail]=v,c[tail]=t;}
bool bfs()
{
memset(h,-1,sizeof(h));
int t=0,head=1,now;
q[t]=1,h[1]=0; /////////////////////////////
while(t!=head)
{
now=q[t++],t%=MAXN;
for(int i=fst[now];i;i=next[i])
{
if(c[i]>0&&h[to[i]]==-1)
{
h[to[i]]=h[now]+1;
q[head++]=to[i],head%=MAXN;
}
}
}
if(h[m]==-1)return false; /////////////////////////////////////////m
return true;
}
int dfs(int x,int f)
{
if(x==m)return f;
int t1,use=0;
for(int i=fst[x];i;i=next[i])
{
if(c[i]>0&&h[to[i]]==h[x]+1)
{
t1=dfs(to[i],min(c[i],f-use));
use+=t1,c[i]-=t1,c[i^1]+=t1;
if(use==f)return f;
}
}
return use;
}
void dinic(){while(bfs()){ans+=dfs(1,INF);}}
int main()
{
scanf("%d%d",&n,&m);
int t1,t2,t3;
for(int i=1;i<=n;i++) ///////////////////////////n
{
scanf("%d%d%d",&t1,&t2,&t3);
add(t1,t2,t3),add(t2,t1,0);
}
dinic();
printf("%d\n",ans);
//while(1);
return 0;
}
相关文章推荐
- 网络地址划分标准
- Deep learning:三十六(关于构建深度卷积SAE网络的一点困惑)
- Linux笔记(30)——查看网络环境命令
- python之网络编程学习小结
- 【学习笔记】计算机网络概述
- socket模拟http请求
- Linux笔记(29)——虚拟机的网络配置
- TCP协议详解
- Android安全教程(3)---Fiddler简易使用教程之抓取https包
- 深度学习(三十二)半监督阶梯网络学习笔记-NIPS 2015
- 跨平台C++服务器程序开发 (4)tcp socket状态图(server端)
- Python网络编程
- 教你写Http框架(二)——三个例子带你深入理解AsyncTask
- TCP/IP、Http的区别
- Vmware虚拟机下三种网络模式配置
- BZOJ_P1475 方格取数(网络流)
- TCP和UDP的区别
- TCP/IP之四书五经
- Eclipse调错——Type The type javax.servlet.http.HttpServletRequest cannot be res...
- rtp over tcp