HDOJ 3549 Flow Problem(network flow)
来源:互联网 发布:ubuntu不支持exfat 编辑:程序博客网 时间:2024/06/04 19:55
this is a very good question for beginning to learn network flow , very easy way to use ff algorithm .
to solve the max flow problem ,we need to calculate the rest flow network,so we update the reverse edges when applying dfs to find the answer.
Need more work in graphic theory……
#include <bits/stdc++.h>#include <cstring>using namespace std;const int M=25;int INF=0x3f3f3f3f3f;struct edge{ int to,cap; unsigned int rev;};vector<edge> G[M];//add a edge with volume of cap to the map from s to tvoid add_edge(int from,int to,int cap){ G[from].push_back((edge) { to,cap,G[to].size() }); G[to].push_back((edge) { from,0,G[from].size()-1 });}//find extend path by dfsbool used[M];int dfs(int v,int t,int f){ if(v==t) return f; used[v] = true; for(unsigned int i=0; i<G[v].size(); i++) { edge &e=G[v][i]; if(!used[e.to] && e.cap>0) { int d=dfs(e.to,t,min(f,e.cap)); if(d>0) { e.cap-=d; G[e.to][e.rev].cap+=d; return d; } } } return 0;}int max_flow(int s,int t){ int flow=0; while(1) { memset(used,0,sizeof(used)); int f=dfs(s,t,INF); if(f==0) return flow; flow+=f; }}int main(){ int T; scanf("%d",&T); for(int t = 1; t<=T; t++) { int n,m; scanf("%d%d",&n,&m); for(int i=0;i<=n;i++) { G[i].clear(); } for(int i=0; i<m; i++) { int s,t,c; scanf("%d%d%d",&s,&t,&c); add_edge(s,t,c); } printf("Case %d: %d\n",t,max_flow(1,n)); } return 0;}
阅读全文
0 0
- HDOJ 3549 Flow Problem(network flow)
- hdoj 3549 Flow Problem
- hdoj 3549 Flow Problem 【最大流】
- HDOJ Flow Problem 3549(最大流)
- HDOJ-3549 Flow Problem(网络流)
- hdoj--3549--Flow Problem(最大流)
- HDOJ 3549 Flow Problem(最大流)
- HDOJ-3549 Flow Problem (最大流模板)
- HDU 3549 Flow Problem
- hdu 3549 Flow Problem
- hdu 3549 Flow Problem
- hdu 3549 Flow Problem
- HDU 3549 Flow Problem
- hdu(3549)Flow Problem
- HDU 3549 Flow Problem
- hdu 3549 Flow Problem
- HDU 3549 Flow Problem
- HDU 3549 Flow Problem
- hibernate注解主键生成策略
- 静态与非静态方法
- Groovy语言学习和Gradle项目搭建笔记 (上)
- 关于spring boot使用velocity作为视图层模板
- hdu 3783 ZOJ
- HDOJ 3549 Flow Problem(network flow)
- linux初学者-正则表达式
- Hibernate学习-用oracle sequence序列生成ID的配置示例
- HTML入门笔记15-HTML中插入外部链接
- 简单LinuxC程序之位运算的简单应用
- Could not get BatchedBridge, make sure your bundle is packaged correctly
- JQuery.Ajax()的data参数类型
- Lnuix系统结构及文件管理
- bzoj1051【HAOI2006】受欢迎的牛