码迷,mamicode.com
首页 > 其他好文 > 详细

[loj#101] 最大流 网络流模板

时间:2017-11-20 19:10:09      阅读:216      评论:0      收藏:0      [点我收藏+]

标签:_id   reset   lin   输出   bottom   splay   htm   sdi   row   

#101. 最大流

内存限制:512 MiB时间限制:5000 ms标准输入输出
题目类型:传统评测方式:文本比较
上传者: 匿名

题目描述

这是一道模板题。

给定 n nn 个点,m mm 条边,给定每条边的容量,求从点 s ss 到点 t tt 的最大流。

输入格式

第一行四个整数 n nn、m mm、s ss、t tt。
接下来的 m mm 行,每行三个整数 u uu、v vv、c cc,表示 u uu 到 v vv,流量为 c cc 的一条边。

输出格式

输出点 s ss 到点 t tt 的最大流。

样例

样例输入

7 14 1 7
1 2 5
1 3 6
1 4 5
2 3 2
2 5 3
3 2 2
3 4 3
3 5 3
3 6 7
4 6 5
5 6 1
6 5 1
5 7 8
6 7 7

样例输出

14

数据范围与提示

1≤n≤106,1≤m≤4×106,0≤c≤231?1 1 \leq n \leq 10 ^ 6, 1 \leq m \leq 4 \times 10 ^ 6, 0 \leq c \leq 2 ^ {31} - 11n10?6??,1m4×10?6??,0c2?31???1

 

技术分享图片
 1 #include<iostream>
 2 #include<cstring>
 3 #include<cstdio>
 4 #include<cstdlib>
 5 #include<cmath>
 6 #include<algorithm>
 7 #define maxm 4000006
 8 #define maxn 1000006
 9 using namespace std;
10 int read() {
11     int x=0,f=1;char ch=getchar();
12     while(!isdigit(ch)){ch=getchar();}
13     while(isdigit(ch)){x=x*10+ch-0;ch=getchar();}
14     return x;
15 }
16 struct data {
17     int from,to,next,w;
18 }e[maxm*2];
19 int head[maxn],cnt;
20 int cur[maxn];
21 void add(int u,int v,int w){e[cnt].from=u;e[cnt].next=head[u];e[cnt].to=v;e[cnt].w=w;head[u]=cnt++;}
22 int n,m,s,t;
23 int q[maxn];
24 bool vis[maxn];
25 int dis[maxn];
26 bool bfs() {
27     memset(dis,-97,sizeof(dis));
28     int h=0,tt=1;
29     q[h]=t;
30     vis[t]=1;
31     dis[t]=0;
32     while(h!=tt) {
33         int now=q[h];h++;vis[now]=0;if(h==maxn) h=0;
34         for(int i=head[now];i>=0;i=e[i].next) {
35             int to=e[i].to;
36             if(e[i^1].w&&dis[to]<-1000000) {
37                 dis[to]=dis[now]-1;
38                 if(!vis[to]){
39                     vis[to]=1;
40                     q[tt++]=to;if(tt==maxn) tt=0;
41                 }
42             }
43         }
44     }
45     return dis[s]>=-1000000;
46 }
47 int dfs(int now,int a) {
48     if(now==t||a==0) return a;
49     int flow=0,f;
50     for(int i=cur[now];i>=0;i=e[i].next) {
51         int to=e[i].to;
52         if(dis[to]==dis[now]+1&&e[i].w>0&&(f=dfs(to,min(a,e[i].w)))) {
53             e[i].w-=f;
54             e[i^1].w+=f;
55             flow+=f;
56             a-=f;
57             if(a==0) return flow;
58         }
59         cur[now]=i;
60     }
61     if(!flow) dis[now]=-1;
62     return flow;
63 }
64 int main() {
65     memset(head,-1,sizeof(head));
66     n=read(),m=read(),s=read(),t=read();
67     for(int i=1;i<=m;i++) {
68         int u=read(),v=read(),w=read();
69         add(u,v,w);add(v,u,0);
70     }
71     int ans=0;
72     while(bfs()){
73         for(int i=1;i<=n;i++) cur[i]=head[i];
74         ans+=dfs(s,2147483647);
75     }
76     printf("%d",ans);
77 }
View Code

 

[loj#101] 最大流 网络流模板

标签:_id   reset   lin   输出   bottom   splay   htm   sdi   row   

原文地址:http://www.cnblogs.com/wls001/p/7867503.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!