Light OJ 1153-internet Bandwidth "Network flow without graph"

Source: Internet
Author: User

1153-internet Bandwidth
PDF (中文版) Statistics Forum
Time Limit:2 second (s) Memory limit:32 MB

On the Internet, Machines (nodes) is richly interconnected, and many paths may exist between a given pair of nodes. The total message-carrying capacity (bandwidth) between, given nodes is the maximal amount of data per unit time that C An IS transmitted from one node to the other. Using a technique called packet switching; This data can is 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 amo ng 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 are, which might be thought of as the sum of the bandwidths a Long the path 1-2-4, 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 and given nodes in a network, given the individual bandwidths The connections in the network. In this problem, assume that the bandwidth of a connection are always the same in both directions (which are 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 O F nodes in the network. The nodes is 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 (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 and the numbers of the connected, and the nodes number is The bandwidth of the connection. The bandwidth is a non-negative number not greater than  .

There might is more than one connection between a pair of nodes, but a node cannot is connected to itself. All connections is 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 is 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 Destinati On 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

Note: This problem is a graph-free network stream, when dealing with reverse edges, you cannot set the capacity of the reverse edge to 0 and the same as the forward edge

#include <stdio.h> #include <string.h> #include <queue> #include <math.h> #include < algorithm> #define MAX 10010#define INF 0x7ffff#define maxm 100100using namespace std;struct node{int From,to,cap,flow , Next;} Edge[maxm];int dis[max],vis[max];int cur[max];int ans,head[max];void init () {ans=0;memset (head);} void Add (int u,int v,int W) {edge[ans]={u,v,w,0,head[u]};head[u]=ans++;edge[ans]={v,u,w,0,head[v]};//This is a map-free graph edge[ ans]={v,u,0,0,head[v]};head[v]=ans++;} int start,hui,sum;void Getmap () {scanf ("%d%d%d", &start,&hui,&sum); for (int i=1;i<=sum;i++) {int a,b,c; scanf ("%d%d%d", &a,&b,&c); add (a,b,c);}} int BFS (int beg,int end) {queue<int>q;memset (vis,0,sizeof (Vis)); memset (dis,-1,sizeof (DIS)); while (!q.empty ()) Q.pop (); Vis[beg]=1;dis[beg]=0;q.push (Beg), while (!q.empty ()) {int U=q.front (), Q.pop (); for (int i=head[u];i!=-1;i= Edge[i].next) {node e=edge[i];if (!vis[e.to]&&e.cap>e.flow) {dis[e.to]=dis[u]+1;vis[e.to]=1;if (E.to== END) RETUrn 1;q.push (e.to);}}} return 0;} int dfs (int x,int a,int end) {if (x==end| | a==0) return A;int flow=0,f;for (int& i=cur[x];i!=-1;i=edge[i].next) {node& e=edge[i];if (dis[E.to]==dis[x]+1 && (F=dfs (E.to,min (A,e.cap-e.flow), end)) >0) {e.flow+=f;edge[i^1].flow-=f;flow+=f;a-=f;if (a==0) break;}} return flow;} int maxflow (int beg,int end) {int Flow=0;while (BFS (beg,end)) {memcpy (cur,head,sizeof (head)); Flow+=dfs (beg,inf,end);} return flow;} int main () {int t,k,n,m;scanf ("%d", &t), K=1;while (t--) {scanf ("%d", &n), Init (), Getmap ();p rintf ("Case%d:%d\n" , K++,maxflow (Start,hui));} return 0;}

  

Light OJ 1153-internet Bandwidth "Network flow without graph"

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.