您的位置:首页 > 理论基础 > 计算机网络

[HihoCoder1369]网络流一·Ford-Fulkerson算法

2017-07-28 14:39 513 查看

思路:

最大流模板。

#include<queue>
#include<cstdio>
#include<cctype>
#include<vector>
#include<cstring>
inline int getint() {
char ch;
while(!isdigit(ch=getchar()));
int x=ch^'0';
while(isdigit(ch=getchar())) x=(((x<<2)+x)<<1)+(ch^'0');
return x;
}
const int N=501,M=20000,inf=0x7fffffff;
struct Edge {
int from,to,remain;
};
Edge e[M<<1];
std::vector<int> g
;
int sz=0;
inline void add_edge(const int u,const int v,const int w) {
e[sz]=(Edge){u,v,w};
g[u].push_back(sz);
sz++;
}
int s,t;
int a
,p
;
inline int Augment() {
memset(a,0,sizeof a);
a[s]=inf;
std::queue<int> q;
q.push(s);
while(!q.empty()&&!a[t]) {
int x=q.front();
q.pop();
for(unsigned i=0;i<g[x].size();i++) {
Edge &y=e[g[x][i]];
if(!a[y.to]&&y.remain) {
p[y.to]=g[x][i];
a[y.to]=std::min(a[x],y.remain);
q.push(y.to);
}
}
}
return a[t];
}
inline int EdmondsKarp() {
int maxflow=0;
while(int flow=Augment()) {
for(int i=t;i!=s;i=e[p[i]].from) {
e[p[i]].remain-=flow;
e[p[i]^1].remain+=flow;
}
maxflow+=flow;
}
return maxflow;
}
int main() {
int n=getint(),m=getint();
s=1,t=n;
while(m--) {
int u=getint(),v=getint(),w=getint();
add_edge(u,v,w);
add_edge(v,u,0);
}
printf("%d\n",EdmondsKarp());
return 0;
}

 

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: