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 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 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 |
最大流入门,真的是最基础的了。。
给的边直接建图,跑一个最大流模板就好了,,
#include "cstring"
#include "iostream"
#include "cstdio"
#include "queue"
using namespace std;
typedef long long LL;
const int M=205;
const int maxn=2147483648;
const int INF = 0x3f3f3f3f;
int s,t,n,m;
int map[M][M],flow[M][M];
int p[M],a[M];
int EK(int s,int t)
{
queue<int>q;
int sum=0;
memset(flow,0,sizeof(flow));
while(1)
{
memset(a,0,sizeof(a));
a[s]=INF;
q.push(s);
while(!q.empty())
{
int u=q.front();
q.pop();
for(int i=1; i<=m; i++)
{
if(!a[i]&&flow[u][i]<map[u][i])
{
p[i]=u;
q.push(i);
a[i]=a[u]<map[u][i]-flow[u][i]?a[u]:map[u][i]-flow[u][i];
}
}
}
if(!a[t])
break;
for(int i=t; i!=s; i=p[i])
{
flow[p[i]][i]+=a[t];
flow[i][p[i]]-=a[t];
}
sum+=a[t];
}
return sum;
}
int main()
{
int casee=1;
int T;
scanf("%d",&T);
while(T--)
{
scanf("%d",&m);
scanf("%d%d%d",&s,&t,&n);
memset(map,0,sizeof(map));
int a,b,c;
for(int i=0; i<n; i++)
{
scanf("%d%d%d",&a,&b,&c);
map[a][b]+=c;
map[b][a]=map[a][b];
}
int maxx=EK(s,t);
cout<<"Case "<<casee++<<": "<<EK(s,t)<<endl;
}
return 0;
}