UVA 820 Internet Bandwidth(最大流)

思路:UVA上的真是阅读题....读懂题目之后其实就是一个最大流裸题...


#include<bits/stdc++.h>
using namespace std;
#define INF 1e9
const int maxn = 1000;
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 Dinic
{
	int n,m,s,t;
	vector<Edge>edges;
	vector<int> G[maxn];
	bool vis[maxn];
	int d[maxn];
	int cur[maxn];
	void AddEdge(int from,int to,int cap)
	{
		edges.push_back(Edge(from,to,cap,0));
		edges.push_back(Edge(to,from,0,0));
		m = edges.size();
		G[from].push_back(m-2);
		G[to].push_back(m-1);
	}
	void init(int n)
	{
		for (int i = 0;i<=n;i++)
			G[i].clear();
		edges.clear();
	}
	bool BFS()
	{
		memset(vis,0,sizeof(vis));
		queue<int>q;
		q.push(s);
		d[s]=0;
		vis[s]=1;
		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 (!vis[e.to] && e.cap>e.flow){
					vis[e.to]=1;
					d[e.to]=d[x]+1;
					q.push(e.to);
				}
			}
		}
		return vis[t];
	}

	int DFS(int x,int a){
		if (x==t || a==0)
			return a;
		int flow = 0,f;
		for (int &i=cur[x];i<G[x].size();i++){
			Edge&e = edges[G[x][i]];
			if (d[x]+1==d[e.to] && (f=DFS(e.to,min(a,e.cap-e.flow)))>0){
				e.flow+=f;
				edges[G[x][i]^1].flow-=f;
				flow+=f;
				a-=f;
				if (a==0)
					break;
			}
		}
		return flow;
	}

	int Maxflow(int s,int t)
	{
		this->s=s;
		this->t=t;
		int flow = 0;
		while (BFS())
		{
			memset(cur,0,sizeof(cur));
			flow+=DFS(s,INF);
		}
		return flow;
	}
}di;
int main()
{
	int cas = 1;
	int n;
	while (scanf("%d",&n)!=EOF && n)
	{
		int s,t,m;
		scanf("%d%d%d",&s,&t,&m);
		di.init(n+1);
		for (int i = 0;i<m;i++)
		{
            int u,v,c;
			scanf("%d%d%d",&u,&v,&c);
			di.AddEdge(u,v,c);
			di.AddEdge(v,u,c);
		}
		
		printf("Network %d\nThe bandwidth is %d.\n\n",cas++,di.Maxflow(s,t));
	}
}


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.


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值