Uva820 Internet Bandwidth(网络流、双向EdmondsKarp)
来源:互联网 发布:新西兰 购物 知乎 编辑:程序博客网 时间:2024/06/06 11:01
Uva820 Internet Bandwidth(网络流、双向EdmondsKarp)
链接:
https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=761
题目
Time Limit:3000MS Memory Limit:0KB
Description
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
The input file contains descriptions of several networks. Every description starts with a line containing a single 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 s, t, and c. The numbers s and t are the source and destination nodes, and the number c 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.
A line containing the number ‘0’ follows the last network description, and terminates the input.
Output
For each network description, first print the number of the network. Then print the total bandwidth between the source node s and the destination node t, following the format of the sample output. Print a blank line after each test case.
Sample Input
4
1 4 5
1 2 20
1 3 10
2 3 5
2 4 10
3 4 20
0
Sample Output
Network 1
The bandwidth is 25.
分析
网络流的最大流问题,用EdmondsKarp算法,注意题目为双向。故存图和处理时注意双向即可。
源码
#include<cstdio>#include<cstring>#include<iostream>#include<queue>#include<vector>#include<algorithm>#include<string>#include<sstream>#include<cmath>#include<set>#include<map>#include<vector>#include<stack>#include<utility>#include<sstream>#define mem0(x) memset(x,0,sizeof x)#define mem1(x) memset(x,-1,sizeof x)#define dbug cout<<"here"<<endl;//#define LOCALusing namespace std;typedef long long ll;typedef unsigned long long ull;const int INF = 0x3f3f3f3f;const int MAXN = 1e3+10;const int MOD = 1000000007;struct Edge{ int from, to, cap, flow; Edge(int u, int v, int c, int f) : from(u), to(v), cap(c), flow(f) {}};struct EdmondsKarp{ int n, m; vector<Edge> edges; // vector<int> G[MAXN]; //邻接表,G[i][j]表示结点i的第j条边在e数组中的编号 int a[MAXN]; //当起点到i的可改进量 int p[MAXN]; //最短路树上p的入弧编号 void init(int n){ for(int i = 0; i <= n; ++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, cap, 0)); m = edges.size(); G[from].push_back(m-2); G[to].push_back(m-1); } int MaxFlow(int s, int t){ int flow = 0; for( ; ; ){ mem0(a); queue<int> Q; while(!Q.empty()) Q.pop(); Q.push(s); a[s] = INF; while(!Q.empty()){ int x = Q.front(); Q.pop(); for(int i = 0; i < G[x].size(); ++i){ Edge& e = edges[G[x][i]]; if(!a[e.to] && e.cap>e.flow){ p[e.to] = G[x][i]; a[e.to] = min(a[x], e.cap-e.flow); Q.push(e.to); } } if(a[t]) break; } if(!a[t]) return flow; for(int u = t; u != s; u = edges[p[u]].from){ edges[p[u]].flow += a[t]; edges[p[u]^1].flow -= a[t]; } flow += a[t]; } }};EdmondsKarp graph;int main(){ #ifdef LOCAL freopen("C:\\Users\\asus-z\\Desktop\\input.txt","r",stdin); freopen("C:\\Users\\asus-z\\Desktop\\output.txt","w",stdout); #endif int N,T; int S, E; int x,y,c; int kase = 0; while(scanf("%d", &N)!=EOF && N){ scanf("%d%d%d", &S, &E, &T); graph.init(N); for(int i = 0; i < T; ++i){ scanf("%d%d%d", &x, &y, &c); graph.AddEdge(x, y, c); } printf("Network %d\n", ++kase); printf("The bandwidth is %d.\n\n", graph.MaxFlow(S, E)); } return 0;}
- Uva820 Internet Bandwidth(网络流、双向EdmondsKarp)
- [最大流]UVa820 - Internet Bandwidth
- [uva820] Internet Bandwidth
- 解题报告 之 UVA820 Internet Bandwidth
- 【最大流问题 dinic算法】Internet Bandwidth, ACM/ICPC World Finals 2000, UVa820
- 网络流模板--edmondsKarp
- 网络最大流-EdmondsKarp
- Internet Bandwidth
- 【最大流】LightOJ-1153 Internet Bandwidth
- LightOJ 1153 - Internet Bandwidth【最大流】
- UVA 820 Internet Bandwidth(最大流)
- UVA 820 Internet Bandwidth(最大流)
- uva 820 Internet Bandwidth 最大流
- 网络流 EdmondsKarp和 Dinic算法
- HDU1532 Drainage Ditches(网络流EdmondsKarp)
- HDU1532 Drainage Ditches(网络流、EdmondsKarp)
- uva820(最大流)
- LightOJ 1153 Internet Bandwidth
- 深入分析AQS的Condition
- php静态属性和静态方法
- struts2自定义拦截器并配置拦截器使其生效
- 抽象类
- Unity UGUI学习(一)
- Uva820 Internet Bandwidth(网络流、双向EdmondsKarp)
- 39.NULL和临时变量和顺序结构
- 高质量代码的命名规则
- Android edittext 输入框 下一项 android:imeOptions="actionNext"
- Android 47个小知识
- markdown数学公式基本语法
- poj1011 | NKOJ 1423 木棍
- Java集合详解-ArrayList
- 辨异 —— 近义词(词组)