UVA 820 Internet Bandwidth
Internet Bandwidth |
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.
![](http://uva.onlinejudge.org/external/8/p820a.jpg)
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 | Output for the Sample Input |
---|---|
4 |
Network 1 |
ACM World Finals 2000, Problem E
//uva 820
//网络流模板题
//注意题目要求的格式
#include <iostream> #include <stdio.h> #include <string.h> #include <algorithm> #include <cmath> #include <queue> #define N 103 using namespace std; int flow[N][N],cap[N][N]; int f[N]; int s,t,n; int BFS() { int a[N]; int u,v; memset(flow,0,sizeof(flow)); int fl=0; for(;;) { memset(a,0,sizeof(a)); queue<int>Q; a[s]=1003; Q.push(s); while(!Q.empty()) { u=Q.front();Q.pop(); for(v=1;v<=n;v++) if(!a[v]&&cap[u][v]-flow[u][v]>0) { f[v]=u; a[v]=a[u]>cap[u][v]-flow[u][v]?cap[u][v]-flow[u][v]:a[u]; Q.push(v); } } if(a[t]==0) break; for(u=t;u!=s;u=f[u]) { flow[f[u]][u]+=a[t]; flow[u][f[u]]-=a[t]; } fl+=a[t]; } return fl; } int main() { int c,u,v,cost; int tt=1; while(scanf("%d",&n),n) { scanf("%d%d%d",&s,&t,&c); memset(cap,0,sizeof(cap)); while(c--) { scanf("%d%d%d",&u,&v,&cost); cap[u][v]+=cost; cap[v][u]=cap[u][v]; } printf("Network %d\n",tt++); printf("The bandwidth is %d.\n\n",BFS()); } return 0; }