【Uva 821】Page Hopping 翻译+题解

来源:互联网 发布:淘宝虚拟货品如何发布 编辑:程序博客网 时间:2024/05/16 08:42
It was recently reported that, on the average, only 19 clicks are necessary to move from any page on
the World Wide Web to any other page. That is, if the pages on the web are viewed as nodes in a
graph, then the average path length between arbitrary pairs of nodes in the graph is 19.


据报道,在平均水平上,只有19次点击是必要的,从万维网的任何一页到任何其他页面。也就是说,如果在网络上的网页被看作是在一个图中的节点,然后在图中的任意对节点之间的平均路径长度为19。

given a graph in which all nodes can be

reached from any starting point, your job is to
nd the average shortest path length between ar-
bitrary pairs of nodes. For example, consider the
following graph. Note that links are shown as di-
rected edges, since a link from page a to page b
does not imply a link from page b to page a.

鉴于在所有节点都可以从任何起点到达一个图,你的工作是和平均最短路径长度是任意节点对之间。例如,考虑下面的图。注意,链接显示为双导的边缘,因为一个链接从一个页面到页面B并不意味着链接从网页B网页A.

The length of the shortest path from node 1
to nodes 2, 3, and 4 is 1,1, and 2 respectively. From node 2 to nodes 1, 3 and 4, the shortest paths
have lengths of 3, 2, and 1. From node 3 to nodes 1, 2, and 4, the shortest paths have lengths of 1, 2,
and 3. Finally, from node 4 to nodes 1, 2, and 3 the shortest paths have lengths of 2, 3, and 1. The
sum of these path lengths is 1 + 1 + 2 + 3 + 2 + 1 + 1 + 2 + 3 + 2 + 3 + 1 = 22. Since there are
12 possible pairs of nodes to consider, we obtain an average path length of 22/12, or 1.833 (accurate
to three fractional digits).

以2节点,3节点1的最短路径的长度,和4和2分别是1。从节点2到节点1,3和4,最短路径有长度为3,2和1。从节点3到节点1,2和4,最短路径有长度为1,2和3。最后,从节点4到节点1,2和3的最短路径有长度为2,3和1。这些路径长度的总和为1,1,,3,2,1,1,2,,3,2,3,1,22 = 2。由于有12个可能的对节点考虑,我们获得的平均路径长度为22 / 12,或1.833(精确到三个小数位数)。

Input
The input data will contain multiple test cases. Each test case will consist of an arbitrary number of
pairs of integers,
a
and
b
, each representing a link from a page numbered
a
to a page numbered
b
. Page
numbers will always be in the range 1 to 100. The input for each test case will be terminated with a
pair of zeroes, which are not to be treated as page numbers. An additional pair of zeroes will follow
the last test case, effectively representing a test case with no links, which is not to be processed. The
graph will not include self-referential links (that is, there will be no direct link from a node to itself),
and at least one path will exist from each node in the graph to every other node in the graph.
输入输入数据将包含多个测试用例。每个测试用例都将由一对整数,A和B组成,每一个对一个链接从一个页面编号A到一个编号B的页面。页号将永远在1到100的范围内。每个测试用例的输入将与一对零终止,这是不被视为页号。零的一对额外的将最后一个测试案例,有效地代表一个没有链接的测试用例,这是不能处理。该图将不包括自引用链接(也就是说,将没有从一个节点到自己的直接链接),和至少一个路径将存在从图中的每一个节点到图中的每一个其他节点。1,3和4,最短路径有长度为3,2和1

Output
For each test case, determine the average shortest path length between every pair of nodes, accurate to
three fractional digits. Display this length and the test case identi er (theyre numbered sequentially
starting with 1) in a form similar to that shown in the sample output below.
输出对于每个测试用例,确定每对节点之间的平均最短路径长度,精确到三小数位数。显示这个长度和测试用例识别二(他们按顺序编号从1开始)在类似的形式,下面的例子所示。

SampInput
1 2 2 4 1 3 3 1 4 3 0 0
1 2 1 4 4 2 2 7 7 1 0 0
0 0
SampleOutput
Case 1: average length between pages = 1.833 clicks
Case 2: average length between pages = 1.750 clicks
题目的pdf: http://7xjob4.com1.z0.glb.clouddn.com/25f0922c098ea0793571065253143c11


#include<cstdio>#include<algorithm>#include<cstring>#include<iostream>#include<cmath>using namespace std;int idx,cnt;int dp[110][110];int ans;#define INF 110int main(){int a,b;while(1){for(int i=1;i<=100;i++){for(int j=1;j<=100;j++){if(i==j)dp[i][j] = 0;else dp[i][j] = INF;}}scanf("%d%d",&a,&b);if(!a&&!b)break;dp[a][b]=1;++idx;ans = 0,cnt=0;for(;;){scanf("%d%d",&a,&b);if(!a&&!b)break;dp[a][b]=1;}for(int k=1;k<=100;k++)for(int i=1;i<=100;i++){for(int j=1;j<=100;j++){if(i==j)continue;dp[i][j] = min(dp[i][j],dp[i][k]+dp[k][j]);}}for(int i=1;i<=100;i++){for(int j=1;j<=100;j++){if(i==j)continue;if(dp[i][j]<INF&&dp[i][j]){ans+=dp[i][j];++cnt;}}}printf("Case %d: average length between pages = %.3f clicks\n",idx,ans*1.0/cnt);//#define debug#ifdef debugfor(int i=1;i<=10;i++){for(int j=1;j<=10;j++){cout<<dp[i][j]<<" ";}cout<<endl;}#endif}return 0;}

神TM dev-c++, %.3lf会出诡异错-0.000 %.3f正常,%.3lf交上去正常。所以说是本机的锅咯?






0 0
原创粉丝点击