1153 - Internet Bandwidth
Time Limit: 2 second(s) Memory Limit: 32 MB

On the Internet, machines (nodes) are richly interconnected, and many paths may exist between a given pair of nodes. The total message-carrying capacity (bandwidth) between two given nodes is the maximal amount of data per unit time that can be transmitted from one node to the other. Using a technique called packet switching; this data can be transmitted along several paths at the same time.

For example, the following figure shows a network with four nodes (shown as circles), with a total of five connections among them. Every connection is labeled with a bandwidth that represents its data-carrying capacity per unit time.

In our example, the bandwidth between node 1 and node 4 is 25, which might be thought of as the sum of the bandwidths 10 along the path 1-2-4, 10 along the path 1-3-4, and 5 along the path 1-2-3-4. No other combination of paths between nodes 1 and 4 provides a larger bandwidth.

You must write a program that computes the bandwidth between two given nodes in a network, given the individual bandwidths of all the connections in the network. In this problem, assume that the bandwidth of a connection is always the same in both directions (which is not necessarily true in the real world).

Input

Input starts with an integer T (≤ 30), denoting the number of test cases.

Every description starts with a line containing an integer n (2 ≤ n ≤ 100), which is the number of nodes in the network. The nodes are numbered from 1 to n. The next line contains three numbers st, and c. The numbers s and t are the source and destination nodes, and the number c (c ≤ 5000, s ≠ t) is the total number of connections in the network. Following this are c lines describing the connections. Each of these lines contains three integers: the first two are the numbers of the connected nodes, and the third number is the bandwidth of the connection. The bandwidth is a non-negative number not greater than 1000.

There might be more than one connection between a pair of nodes, but a node cannot be connected to itself. All connections are bi-directional, i.e. data can be transmitted in both directions along a connection, but the sum of the amount of data transmitted in both directions must be less than the bandwidth.

Output

For each case of input, print the case number and the total bandwidth between the source node s and the destination node t.

Sample Input

Output for Sample Input

2

4

1 4 5

1 2 20

1 3 10

2 3 5

2 4 10

3 4 20

4

1 4 2

1 4 20

1 4 20

Case 1: 25

Case 2: 40

注意:此题是无向图的网络流,在处理反向边的时候,不能把反向边的容量设置为0 而要设置的和正向边一样

  1. #include<stdio.h>
  2. #include<string.h>
  3. #include<queue>
  4. #include<math.h>
  5. #include<algorithm>
  6. #define MAX 10010
  7. #define INF 0x7ffff
  8. #define MAXM 100100
  9. using namespace std;
  10. struct node
  11. {
  12. int from,to,cap,flow,next;
  13. }edge[MAXM];
  14. int dis[MAX],vis[MAX];
  15. int cur[MAX];
  16. int ans,head[MAX];
  17. void init()
  18. {
  19. ans=0;
  20. memset(head,-1,sizeof(head));
  21. }
  22. void add(int u,int v,int w)
  23. {
  24. edge[ans]={u,v,w,0,head[u]};
  25. head[u]=ans++;
  26. edge[ans]={v,u,w,0,head[v]};//这是有向图 无向图 edge[ans]={v,u,0,0,head[v]};
  27. head[v]=ans++;
  28. }
  29. int start,hui,sum;
  30. void getmap()
  31. {
  32. scanf("%d%d%d",&start,&hui,&sum);
  33. for(int i=1;i<=sum;i++)
  34. {
  35. int a,b,c;
  36. scanf("%d%d%d",&a,&b,&c);
  37. add(a,b,c);
  38. }
  39. }
  40. int bfs(int beg,int end)
  41. {
  42. queue<int>q;
  43. memset(vis,0,sizeof(vis));
  44. memset(dis,-1,sizeof(dis));
  45. while(!q.empty()) q.pop();
  46. vis[beg]=1;
  47. dis[beg]=0;
  48. q.push(beg);
  49. while(!q.empty())
  50. {
  51. int u=q.front();
  52. q.pop();
  53. for(int i=head[u];i!=-1;i=edge[i].next)
  54. {
  55. node E=edge[i];
  56. if(!vis[E.to]&&E.cap>E.flow)
  57. {
  58. dis[E.to]=dis[u]+1;
  59. vis[E.to]=1;
  60. if(E.to==end) return 1;
  61. q.push(E.to);
  62. }
  63. }
  64. }
  65.  
  66. return 0;
  67. }
  68. int dfs(int x,int a,int end)
  69. {
  70. if(x==end||a==0)
  71. return a;
  72. int flow=0,f;
  73. for(int& i=cur[x];i!=-1;i=edge[i].next)
  74. {
  75. node& E=edge[i];
  76. if(dis[E.to]==dis[x]+1&&(f=dfs(E.to,min(a,E.cap-E.flow),end))>0)
  77. {
  78. E.flow+=f;
  79. edge[i^1].flow-=f;
  80. flow+=f;
  81. a-=f;
  82. if(a==0) break;
  83. }
  84. }
  85. return flow;
  86. }
  87. int maxflow(int beg,int end)
  88. {
  89. int flow=0;
  90. while(bfs(beg,end))
  91. {
  92. memcpy(cur,head,sizeof(head));
  93. flow+=dfs(beg,INF,end);
  94. }
  95. return flow;
  96. }
  97. int main()
  98. {
  99. int t,k,n,m;
  100. scanf("%d",&t);
  101. k=1;
  102. while(t--)
  103. {
  104. scanf("%d",&n);
  105. init();
  106. getmap();
  107. printf("Case %d: %d\n",k++,maxflow(start,hui));
  108. }
  109. return 0;
  110. }

  

light oj 1153 - Internet Bandwidth【网络流无向图】的更多相关文章

  1. Light OJ 1114 Easily Readable 字典树

    题目来源:Light OJ 1114 Easily Readable 题意:求一个句子有多少种组成方案 仅仅要满足每一个单词的首尾字符一样 中间顺序能够变化 思路:每一个单词除了首尾 中间的字符排序 ...

  2. Light OJ 1429 Assassin`s Creed (II) BFS+缩点+最小路径覆盖

    题目来源:Light OJ 1429 Assassin`s Creed (II) 题意:最少几个人走全然图 能够反复走 有向图 思路:假设是DAG图而且每一个点不能反复走 那么就是裸的最小路径覆盖 如 ...

  3. Light OJ 1406 Assassin`s Creed 减少国家DP+支撑点甚至通缩+最小路径覆盖

    标题来源:problem=1406">Light OJ 1406 Assassin`s Creed 意甲冠军:向图 派出最少的人经过全部的城市 而且每一个人不能走别人走过的地方 思路: ...

  4. Light OJ 1316 A Wedding Party 最短路+状态压缩DP

    题目来源:Light OJ 1316 1316 - A Wedding Party 题意:和HDU 4284 差点儿相同 有一些商店 从起点到终点在走过尽量多商店的情况下求最短路 思路:首先预处理每两 ...

  5. light oj 1007 Mathematically Hard (欧拉函数)

    题目地址:light oj 1007 第一发欧拉函数. 欧拉函数重要性质: 设a为N的质因数.若(N % a == 0 && (N / a) % a == 0) 则有E(N)=E(N ...

  6. Light OJ 1406 Assassin`s Creed 状态压缩DP+强连通缩点+最小路径覆盖

    题目来源:Light OJ 1406 Assassin`s Creed 题意:有向图 派出最少的人经过全部的城市 而且每一个人不能走别人走过的地方 思路:最少的的人能够走全然图 明显是最小路径覆盖问题 ...

  7. Light OJ 1288 Subsets Forming Perfect Squares 高斯消元求矩阵的秩

    题目来源:Light OJ 1288 Subsets Forming Perfect Squares 题意:给你n个数 选出一些数 他们的乘积是全然平方数 求有多少种方案 思路:每一个数分解因子 每隔 ...

  8. Jan's light oj 01--二分搜索篇

    碰到的一般题型:1.准确值二分查找,或者三分查找(类似二次函数的模型). 2.与计算几何相结合答案精度要求比较高的二分查找,有时与圆有关系时需要用到反三角函数利用 角度解题. 3.不好直接求解的一类计 ...

  9. Light OJ 1272 Maximum Subset Sum 高斯消元 最大XOR值

    版权声明:本文为博主原创文章.未经博主同意不得转载. https://blog.csdn.net/u011686226/article/details/32337735 题目来源:problem=12 ...

随机推荐

  1. DataGridView自动行号

    最近又用了一下DataGridView控件,需要显示行号,我们知道在.net中DataGridView控件默认是不显示行号(数据的记录行数)的,后来通过查资料发现可以在DataGridView控件的R ...

  2. 问题分享:ActiveX component can't create object: "MSComDlg.CommonDialog"

    问题描述: 修改一个前辈的代码,在我自己电脑上面运行的很好,但是放到要用户电脑(win7 x64)上面却跑不了,报个如题的错误. 查了下是COMDLG32.OCX的问题,用到控件的地方是: Dim o ...

  3. 静态页面参数传递&回调函数写法&快速排序的实现方法

    相信很多人都有一种陋习,就是收藏的文章,几乎从来都没有回过头来仔细看过.这次借着这次活动的机会,在<无懈可击的web设计>一书的学习过程中,穿插着讲自己曾经收藏过的,现在觉得还有价值的文章 ...

  4. BZOJ 3992 [SDOI 2015] 序列统计 解题报告

    这个题最暴力的搞法就是这样的: 设 $Dp[i][j]$ 为前 $i$ 个数乘积为 $j$ 的方案数. 转移的话就不多说了哈... 当前复杂度 $O(nm^2)$ 注意到,$M$ 是个质数,就说明 $ ...

  5. .net和MVC中的json值和List<T>和DataTable的一些转换

    1.List<T>集合转换为Json值 List<ReportModel> dtList = new List<ReportModel>(); JsonResult ...

  6. PYTHON多进程并发WEB服务器(利用LINUX的FORK)

    这个又牛X 一点点.. 这还不涉及IO,如果调用GEVENT之类作异步IO或非阻塞IO,那就大框架都有啦.. ############################################# ...

  7. Jmeter 日志设置---如何设置java协议中被测jar的日志?

    先转载一下Jmeter的日志设置: Jmeter运行出现问题可以通过调整jmeter的日志级别定位问题,但运行测试时建议关闭jmeter日志,jmeter打印日志耗费系统性能. Jmeter日志默认存 ...

  8. [状压dp]经典TSP

    0出发 每个顶点经过一次 回到0 最小花费. O($n^2 \times 2^n$) 记忆化搜索: // s: 已经访问过的节点状态 v: 出发位置 int dfs(int s, int v) { ) ...

  9. 如何才能学到Qt的精髓——信号槽之间的无关性,提供了绝佳的对象间通讯方式,QT的GUI全是自己的一套,并且完全开源,提供了一个绝好机会窥视gui具体实现

    姚冬,中老年程序员 叶韵.KY Xu.赵奋强 等人赞同 被邀请了很久了,一直在思考,今天终于下决心开始写回答. 这个问题的确是够大的,Qt的代码规模在整个开源世界里也是名列前茅的,这么大的项目其中的精 ...

  10. Django单元测试(一)

    Django测试框架非常简单,首选方法是使用python标准库中的unittest模块. Writing tests Django的单元测试使用python的unittest模块,这个模块使用基于类的 ...