解题报告 之 UVA820 Internet Bandwidth

解题报告 之 UVA820 Internet Bandwidth

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 Output for the Sample Input
4
1 4 5
1 2 20
1 3 10
2 3 5
2 4 10
3 4 20
0
Network 1
The bandwidth is 25.

ACM World Finals 2000, Problem E

题目大意:有一个计算机网络,输入节点数n,输入网络流源点和汇点src,des,再输入双向边数m。给出m条边的负载,求最大流。

分析:裸的最大流,连背景都不包装了,于是乎直接敲了模板跑最大流即可。注意唯一的坑点在于输出要多一行空行,没看到就一直WA把。于是乎解题报告至此结束啦。

上代码:

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<queue>
#include<cstring>
using namespace std;

const int MAXM = 160000;
const int MAXN = 400;
const int INF = 0x3f3f3f3f;

struct Edge
{
	int to, cap, next;
};

Edge edge[MAXM];
int level[MAXN];
int head[MAXN];
int src, des, cnt;

void addedge( int from, int to, int cap )
{
	edge[cnt].to = to;
	edge[cnt].cap = cap;
	edge[cnt].next = head[from];
	head[from] = cnt++;

	swap( from, to );

	edge[cnt].to = to;
	edge[cnt].cap = cap;
	edge[cnt].next = head[from];
	head[from] = cnt++;
}

int bfs()
{
	memset( level, -1, sizeof level );
	queue<int>q;
	while (!q.empty())
		q.pop();

	level[src] = 0;
	q.push( src );

	while (!q.empty())
	{
		int u = q.front();
		q.pop();

		for (int i = head[u]; i != -1; i = edge[i].next)
		{
			int v = edge[i].to;
			if (edge[i].cap > 0 && level[v] == -1)
			{
				level[v] = level[u] + 1;
				q.push( v );
			}
		}
	}
	return level[des] != -1;
}

int dfs( int u, int f )
{
	if (u == des) return f;
	int tem;
	for (int i = head[u]; i != -1; i = edge[i].next)
	{
		int v = edge[i].to;
		if (edge[i].cap>0&&level[v] == level[u] + 1)
		{
			tem = dfs( v, min( f, edge[i].cap ) );
			if (tem > 0)
			{
				edge[i].cap -= tem;
				edge[i^1].cap += tem;
				return tem;
			}
		}
	}
	level[u] = -1;
	return 0;
}

int Dinic()
{
	int ans = 0, tem;

	while (bfs())
	{
		while ((tem = dfs( src, INF )) > 0)
		{
			ans += tem;
		}
	}
	return ans;
}

int main()
{
	int n, m;
	int kase = 1;
	while (cin >> n&&n)
	{
		memset( head, -1, sizeof head );
		cnt = 0;
		cin >> src >> des >> m;
		for (int i = 1; i <= m; i++)
		{
			int a, b, c;
			cin >> a >> b >> c;
			addedge( a, b, c );
		}

		printf( "Network %d\nThe bandwidth is %d.\n\n",kase++, Dinic() );
	}
	return 0;

}

啦啦啦啦啦啦啦,好有诚意的一道题。。

时间: 2024-08-26 09:14:39

解题报告 之 UVA820 Internet Bandwidth的相关文章

UVa820 Internet Bandwidth (最大流)

链接:http://bak3.vjudge.net/problem/UVA-820 分析:最大流模板题. 1 #include <cstdio> 2 #include <queue> 3 #include <cstring> 4 #include <vector> 5 using namespace std; 6 7 const int maxn = 100 + 5; 8 const int INF = 0x3f3f3f3f; 9 10 struct Edg

UVA820 Internet Bandwidth

很裸的模版题,就是敲起来稍微麻烦一点. #include<bits/stdc++.h> using namespace std; struct Edge { int v,cap; }; const int maxn = 101; vector<Edge> E; vector<int> G[maxn]; #define PB push_back void AddEdge(int u,int v,int c) { G[u].PB(E.size()); E.PB({v,c})

UVA-820 Internet Bandwidth (最大流)

题目大意:单源单汇无项网络求最大流. 题目分析:入门级别的题.但是ISAP在这儿好像不大好使?... 代码如下: # include<iostream> # include<cstdio> # include<cstring> # include<vector> # include<queue> # include<algorithm> using namespace std; const int INF=1<<30; c

解题报告 之 POJ3057 Evacuation

解题报告 之 POJ3057 Evacuation Description Fires can be disastrous, especially when a fire breaks out in a room that is completely filled with people. Rooms usually have a couple of exits and emergency exits, but with everyone rushing out at the same time

hdu 1541 Stars 解题报告

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1541 题目意思:有 N 颗星星,每颗星星都有各自的等级.给出每颗星星的坐标(x, y),它的等级由所有比它低层(或者同层)的或者在它左手边的星星数决定.计算出每个等级(0 ~ n-1)的星星各有多少颗. 我只能说,题目换了一下就不会变通了,泪~~~~ 星星的分布是不是很像树状数组呢~~~没错,就是树状数组题来滴! 按照题目输入,当前星星与后面的星星没有关系.所以只要把 x 之前的横坐标加起来就可以了

【百度之星2014~初赛(第二轮)解题报告】Chess

声明 笔者最近意外的发现 笔者的个人网站http://tiankonguse.com/ 的很多文章被其它网站转载,但是转载时未声明文章来源或参考自 http://tiankonguse.com/ 网站,因此,笔者添加此条声明. 郑重声明:这篇记录<[百度之星2014~初赛(第二轮)解题报告]Chess>转载自 http://tiankonguse.com/ 的这条记录:http://tiankonguse.com/record/record.php?id=667 前言 最近要毕业了,有半年没做

2016 第七届蓝桥杯 c/c++ B组省赛真题及解题报告

2016 第七届蓝桥杯 c/c++ B组省赛真题及解题报告 勘误1:第6题第4个 if最后一个条件粗心写错了,答案应为1580. 条件应为abs(a[3]-a[7])!=1,宝宝心理苦啊.!感谢zzh童鞋的提醒. 勘误2:第7题在推断连通的时候条件写错了,后两个if条件中是应该是<=12 落了一个等于号.正确答案应为116. 1.煤球数目 有一堆煤球.堆成三角棱锥形.详细: 第一层放1个, 第二层3个(排列成三角形), 第三层6个(排列成三角形), 第四层10个(排列成三角形). -. 假设一共

light oj 1153 - Internet Bandwidth【网络流无向图】

1153 - Internet Bandwidth   PDF (English) Statistics Forum Time Limit: 2 second(s) Memory Limit: 32 MB On the Internet, machines (nodes) are richly interconnected, and many paths may exist between a given pair of nodes. The total message-carrying cap

[noip2011]铺地毯(carpet)解题报告

最近在写noip2011的题,备战noip,先给自己加个油! 下面是noip2011的试题和自己的解题报告,希望对大家有帮助,题目1如下 1.铺地毯(carpet.cpp/c/pas) [问题描述]为了准备一个独特的颁奖典礼,组织者在会场的一片矩形区域(可看做是平面直角坐标系的第一象限)铺上一些矩形地毯.一共有n 张地毯,编号从1 到n.现在将这些地毯按照编号从小到大的顺序平行于坐标轴先后铺设,后铺的地毯覆盖在前面已经铺好的地毯之上.地毯铺设完成后,组织者想知道覆盖地面某个点的最上面的那张地毯的