题意:给出所有计算机之间的路径和路径容量后,求出两个给定结点之间的流通总容量。(假设路径是双向的,且两方向流动的容量相同)

分析:裸最大流。标号从1开始,初始化的时候注意。

  1. #pragma comment(linker, "/STACK:102400000, 102400000")
  2. #include<cstdio>
  3. #include<cstring>
  4. #include<cstdlib>
  5. #include<cctype>
  6. #include<cmath>
  7. #include<iostream>
  8. #include<sstream>
  9. #include<iterator>
  10. #include<algorithm>
  11. #include<string>
  12. #include<vector>
  13. #include<set>
  14. #include<map>
  15. #include<stack>
  16. #include<deque>
  17. #include<queue>
  18. #include<list>
  19. #define Min(a, b) ((a < b) ? a : b)
  20. #define Max(a, b) ((a < b) ? b : a)
  21. const double eps = 1e-8;
  22. inline int dcmp(double a, double b){
  23. if(fabs(a - b) < eps) return 0;
  24. return a > b ? 1 : -1;
  25. }
  26. typedef long long LL;
  27. typedef unsigned long long ULL;
  28. const int INT_INF = 0x3f3f3f3f;
  29. const int INT_M_INF = 0x7f7f7f7f;
  30. const LL LL_INF = 0x3f3f3f3f3f3f3f3f;
  31. const LL LL_M_INF = 0x7f7f7f7f7f7f7f7f;
  32. const int dr[] = {0, 0, -1, 1, -1, -1, 1, 1};
  33. const int dc[] = {-1, 1, 0, 0, -1, 1, -1, 1};
  34. const int MOD = 1e9 + 7;
  35. const double pi = acos(-1.0);
  36. const int MAXN = 100 + 10;
  37. const int MAXT = 10000 + 10;
  38. using namespace std;
  39. struct Edge{
  40. int from, to, cap, flow;
  41. Edge(int fr, int t, int c, int fl):from(fr), to(t), cap(c), flow(fl){}
  42. };
  43. struct Dinic{
  44. int n, m, s, t;
  45. vector<Edge> edges;
  46. vector<int> G[MAXN];
  47. bool vis[MAXN];
  48. int d[MAXN];
  49. int cur[MAXN];
  50. void init(int n){
  51. edges.clear();
  52. for(int i = 1; i <= n; ++i) G[i].clear();//标号从1开始
  53. }
  54. void AddEdge(int from, int to, int cap){
  55. edges.push_back(Edge(from, to, cap, 0));
  56. edges.push_back(Edge(to, from, 0, 0));
  57. m = edges.size();
  58. G[from].push_back(m - 2);
  59. G[to].push_back(m - 1);
  60. }
  61. bool BFS(){
  62. memset(vis, 0, sizeof vis);
  63. queue<int> Q;
  64. Q.push(s);
  65. d[s] = 0;
  66. vis[s] = 1;
  67. while(!Q.empty()){
  68. int x = Q.front();
  69. Q.pop();
  70. for(int i = 0; i < G[x].size(); ++i){
  71. Edge& e = edges[G[x][i]];
  72. if(!vis[e.to] && e.cap > e.flow){
  73. vis[e.to] = 1;
  74. d[e.to] = d[x] + 1;
  75. Q.push(e.to);
  76. }
  77. }
  78. }
  79. return vis[t];
  80. }
  81. int DFS(int x, int a){
  82. if(x == t || a == 0) return a;
  83. int flow = 0, f;
  84. for(int& i = cur[x]; i < G[x].size(); ++i){
  85. Edge& e = edges[G[x][i]];
  86. if(d[x] + 1 == d[e.to] && (f = DFS(e.to, Min(a, e.cap - e.flow))) > 0){
  87. e.flow += f;
  88. edges[G[x][i] ^ 1].flow -= f;
  89. flow += f;
  90. a -= f;
  91. if(a == 0) break;
  92. }
  93. }
  94. return flow;
  95. }
  96. int Maxflow(int s, int t){
  97. this -> s = s;
  98. this -> t = t;
  99. int flow = 0;
  100. while(BFS()){
  101. memset(cur, 0, sizeof cur);
  102. flow += DFS(s, INT_INF);
  103. }
  104. return flow;
  105. }
  106. }di;
  107. int main(){
  108. int n;
  109. int kase = 0;
  110. while(scanf("%d", &n) == 1){
  111. if(!n) return 0;
  112. di.init(n);
  113. int s, t, c;
  114. scanf("%d%d%d", &s, &t, &c);
  115. for(int i = 0; i < c; ++i){
  116. int x, y, v;
  117. scanf("%d%d%d", &x, &y, &v);
  118. di.AddEdge(x, y, v);//路径双向
  119. di.AddEdge(y, x, v);
  120. }
  121. printf("Network %d\nThe bandwidth is %d.\n\n", ++kase, di.Maxflow(s, t));
  122. }
  123. return 0;
  124. }

  

UVA - 820 Internet Bandwidth (因特网带宽)(最大流)的更多相关文章

  1. UVA 820 Internet Bandwidth 因特网宽带(无向图,最大流,常规)

    题意:给一个无向图,每条边上都有容量的限制,要求求出给定起点和终点的最大流. 思路:每条无向边就得拆成2条,每条还得有反向边,所以共4条.源点汇点已经给出,所以不用建了.直接在图上跑最大流就可以了. ...

  2. UVA - 820 Internet Bandwidth(最大流模板题)

    题目: 思路: 直接套最大流的模板就OK了,注意一下输出的格式. 代码: #include <bits/stdc++.h> #define inf 0x3f3f3f3f #define M ...

  3. UVa 820 Internet Bandwidth (裸板网络流)

    题意:有一个计算机网络,输入节点数n,输入网络流源点和汇点src,des,再输入双向边数m.给出m条边的负载,求最大流. 析:直接上网络流的最大流. 代码如下: #pragma comment(lin ...

  4. UVA 820 Internet Bandwidth

    题意: 给出双向图,求给出两点的流通总流量. 分析: 网络流中的增广路算法. 代码: #include <iostream>#include <cstring>#include ...

  5. UVA 820 --- POJ 1273 最大流

    找了好久这两个的区别...UVA820 WA了 好多次.不过以后就做模板了,可以求任意两点之间的最大流. UVA 是无向图,因此可能有重边,POJ 1273是有向图,而且是单源点求最大流,因此改模板的 ...

  6. UVA 10480 Sabotage (网络流,最大流,最小割)

    UVA 10480 Sabotage (网络流,最大流,最小割) Description The regime of a small but wealthy dictatorship has been ...

  7. light oj 1153 - Internet Bandwidth【网络流无向图】

    1153 - Internet Bandwidth   PDF (English) Statistics Forum Time Limit: 2 second(s) Memory Limit: 32 ...

  8. Acme Corporation UVA - 11613 拆点法+最大费用最大流(费用取相反数)+费用有正负

    /** 题目:Acme Corporation UVA - 11613 拆点法+最大费用最大流(费用取相反数)+费用有正负 链接:https://vjudge.net/problem/UVA-1161 ...

  9. UVa 820 因特网带宽(最大流)

    https://vjudge.net/problem/UVA-820 题意: 给出所有计算机之间的路径和路径容量后求出两个给定结点之间的流通总容量. 思路: 裸的最大流问题.注意有个比较坑的地方,最后 ...

随机推荐

  1. day14-Python运维开发基础(内置函数、pickle序列化模块、math数学模块)

    1. 内置函数 # ### 内置函数 # abs 绝对值函数 res = abs(-10) print(res) # round 四舍五入 (n.5 n为偶数则舍去 n.5 n为奇数,则进一!) 奇进 ...

  2. matplotlib画图的时候显示不出中文和负号的解决办法

    import matplotlib.pyplot as pltfrom pylab import * plt.rcParams['font.sans-serif'] = ['SimHei'] #显示中 ...

  3. Nginx 的优势

    Nginx 的优势 在 Java 开发中,Nginx 有着非常广泛的使用,随便举几点: 使用 Nginx 做静态资源服务器:Java 中的资源可以分为动态和静态,动态需要经过 Tomcat 解析之后, ...

  4. java字符集编码乱码问题

    博客分类: web javajspservlet  最近做网页这块时碰到了正文字符乱码问题.别看这小小的一个问题,对我来说却花费了好长一段时间.现在让我慢慢分析它吧(说实话.这些有部分是从网上找的,但 ...

  5. Vue3中的Proxy作用在哪里?

    目录 前言 Vue没有Proxy会怎么样? proxy开始 前言 在讲解Proxy之前,我们有些前置知识点是必要掌握的: Object相关静态函数 Reflect相关静态函数 简单说明知识盲点 名称 ...

  6. centos 6.5安装NodeJS

    centos 6.5安装NodeJS 下载 可以在本地下载node.js最新版,然后通过ftp工具上传到服务器,或者直接在服务器终端使用wget命令下载(我当时下载的是node-v7.5.0-linu ...

  7. Vue-cli3与springboot项目整合打包

    一.需求        使用前后端分离编写了个小程序,前端使用的是vue-cli3创建的项目,后端使用的是springboot创建的项目,部署的时候一起打包部署,本文对一些细节部分进行了说明.   二 ...

  8. 攻防世界web新手区(3)

    xff_referer:http://111.198.29.45:43071 打开网址,显示出这个页面: X-Forwarded-For:简称XFF头,它代表客户端,也就是HTTP的请求端真实的IP, ...

  9. SciPy 输入输出

    章节 SciPy 介绍 SciPy 安装 SciPy 基础功能 SciPy 特殊函数 SciPy k均值聚类 SciPy 常量 SciPy fftpack(傅里叶变换) SciPy 积分 SciPy ...

  10. 开发自己的 chart【转】

    Kubernetes 给我们提供了大量官方 chart,不过要部署微服务应用,还是需要开发自己的 chart,下面就来实践这个主题. 创建 chart 执行 helm create mychart 的 ...