UVA - 820 Internet Bandwidth(最大流模板题)
题目:
思路:
直接套最大流的模板就OK了,注意一下输出的格式。
代码:
#include <bits/stdc++.h> #define inf 0x3f3f3f3f #define MAX 1000000000 #define mod 1000000007 #define FRE() freopen("in.txt","r",stdin) #define FRO() freopen("out.txt","w",stdout) using namespace std; typedef long long ll; typedef pair<int,ll> pii; const int maxn = 105; int n; struct Edge {int from,to,cap,flow;Edge(int from,int to,int cap,int flow):from(from),to(to),cap(cap),flow(flow) {} };struct Dinic {int n,m,s,t;vector<Edge> edges;vector<int> G[maxn];bool vis[maxn];int d[maxn];int cur[maxn];void init(){for(int i=0; i<maxn; i++){G[i].clear();}edges.clear();}void AddEdge(int from,int to,int cap) {edges.push_back(Edge(from,to,cap,0));edges.push_back(Edge(to,from,0,0));m = edges.size();G[from].push_back(m-2);G[to].push_back(m-1);}bool BFS(){memset(vis,0,sizeof(vis));queue<int>que;que.push(s);d[s] = 0;vis[s] = 1;while(!que.empty()){int x = que.front(); que.pop();for(int i=0; i<G[x].size(); i++){Edge& e = edges[G[x][i]];if(!vis[e.to] && e.cap>e.flow){vis[e.to] = 1;d[e.to] = d[x]+1;que.push(e.to);}}}return vis[t];}int DFS(int x,int a){if(x == t || a==0) return a;int flow = 0,f;for(int i=cur[x]; i<G[x].size(); i++){Edge&e = edges[G[x][i]];if(d[x]+1 == d[e.to] && (f=DFS(e.to,min(a,e.cap-e.flow)))>0){e.flow += f;edges[G[x][i]^1].flow -= f;flow += f;a -= f;if(a==0) break;}}return flow;}int MaxFlow(int s,int t){this->s = s; this->t = t;int flow = 0;while(BFS()){memset(cur,0,sizeof(cur));flow += DFS(s,inf);}return flow;} }dic;int main(){int kase = 0;while(scanf("%d",&n) && n){dic.init();int ss,tt,cc,s,t,c;scanf("%d%d%d",&ss,&tt,&cc);for(int i=0; i<cc; i++){scanf("%d%d%d",&s,&t,&c);dic.AddEdge(s,t,c);dic.AddEdge(t,s,c);}int ans = dic.MaxFlow(ss,tt);printf("Network %d\n",++kase);printf("The bandwidth is %d.\n\n",ans);}return 0; }
转载于:https://www.cnblogs.com/sykline/p/10399825.html
UVA - 820 Internet Bandwidth(最大流模板题)相关推荐
- UVA, 820 Internet Bandwidth
最大流,注意是无向图,水题. #include <iostream> #include<vector> #include<cstring> using namesp ...
- uva 820 Internet Bandwidth
就是最大流问题.用的EK. #include<iostream> #include<cstdio> #include<algorithm> #include< ...
- hdu3549(又是最大流模板题)
题意: 裸的最大流模板,比hdu1532多了个case输出... 思路: 还是用hdu1532那个模板... 代码: #include<cstdio> #include<cstrin ...
- [hdu3549]Flow Problem(最大流模板题)
解题关键:使用的挑战程序设计竞赛上的模板,第一道网络流题目,效率比较低,且用不习惯的vector来建图. 看到网上其他人说此题有重边,需要注意下,此问题只在邻接矩阵建图时会出问题,邻接表不会存在的,也 ...
- hdu 3549 Flow Problem(最大流模板题)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3549 Problem Description Network flow is a well-known ...
- 最大流 最小费用最大流模板
模板从 这里 搬运,链接博客还有很多网络流题集题解参考. 最大流模板 ( 可处理重边 ) const int maxn = 1e6 + 10; const int INF = 0x3f3f3f3 ...
- 图论-有向图的连通性模板题(hdu1296)(hdu1827)
1.强连通分量: 强连通分量可以理解为边数最少的情况下是一个环. 这里写了一个模板题用的是tarjan算法,当然还有其他算法. tarjan算法的关键其实还是对于num数组和low数组的使用 然后可以 ...
- P1339 热浪 最短路径模板题
这么naive的题面一看就是最短路模板题~~~ ok.首先是floyd算法,tts,记得把k放在最外面就行了. 1 #include <cstdio> 2 #include <cst ...
- HDU2255 奔小康赚大钱(km模板题)
Description 传说在遥远的地方有一个非常富裕的村落,有一天,村长决定进行制度改革:重新分配房子. 这可是一件大事,关系到人民的住房问题啊.村里共有n间房间,刚好有n家老百姓, ...
最新文章
- 数字转换为字符的L受哪个参数影响
- Redhat 5 配置DHCP服务器
- 美国一大型数据中心将24h由光伏+储能供电
- java限频_单个用户及Ip请求频率限制思路(附java实现)
- ip访问php $_files空,PHP中表单没有问题但$_FILES为空怎么办?
- Git Bash推送GitHub不成功---密钥设置
- ubuntu下nagios监控远程主机
- 台达伺服驱动器说明书_干货:伺服调机实例讲解
- java实现线性回归(简单明了,适合理解)
- jq ajax调用php函数,jQuery
- 途观l怎么使用_值得考虑的SUV车型,昂科威、探岳、途观L,你会如何选?
- 【没理解】为何JAVA虚函数(虚方法)会造成父类可以访问子类的假象?
- MS SQL SERVER中删除存在的临时表
- Jaspersoft 与springboot结合生成一维码,二维码
- js监听scrolltop_js中scrollTop()方法和scroll()方法用法示例
- 带你走进CoDeSys
- 数据准备脚本:Python Pandas OR esProc SPL?
- Web3:我们创造更好互联网的机会
- kafka启动之后秒退的原因
- Rancher安装部署
热门文章
- LightOJ 1278 - Sum of Consecutive Integers 分解奇因子 + 思维
- mysql备份时过滤掉某些库 以及 去掉Warning: Using a password on the command line interface can be insecure.提示信息...
- Global.asax中的C#服务
- TCP四种定时器--学习笔记
- Mr.J---重拾Ajax(三)-- jsonjQuery实现Ajax
- 透视前端工程化之 Webpack 基本介绍【文末有彩蛋~】
- 研究大华3G设备接入自主视频开发平台
- 进程的内存空间相互隔离
- Python3中的bytes和str类型
- input file HTML控件控制