UPC 组队训练 Network Report(Floyd)

博客围绕网络最短路径统计问题展开,给定无向无权连通图,要找出所有点对间最短路径,并统计各长度最短路径数量。输入包含图的节点数、边数及边的连接信息,输出各长度最短路径的数量,解题思路采用Floyd算法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

问题 D: Network Report
时间限制: 1 Sec 内存限制: 128 MB
提交: 139 解决: 58
[提交] [状态] [讨论版] [命题人:admin]
题目描述

For time efficiency, data transmission is often done along the shortest path (with minimum number of hops) between network nodes. In the network graph (undirected and unweighted) below, the length of the shortest path from node 0 to node 2 is 2, and the length of the shortest path from node 0 to node 4 is 3. For a network graph with n nodes, we want to find all-pairs shortest paths, and provide a summary report on how many shortest paths are there for each length (from 1 to the maximal length). That is, this summary report should contain the total number of shortest paths of length 1, the total number of shortest paths of length 2, and so on and so forth.
Note that if multiple shortest paths from node i to node j (i ≠ j) exist, they should collectively count as one. For example, though there are two shortest paths from node 0 to node 4, only one path is counted. Please write a program to output the above summary report from an input connected graph.

这里写图片描述
输入

The input contains multiple instances of an undirected and unweighted connected graph (not multigraph). For each instance, the first line contains a single integer n. The second line contains a single integer e, denoting the number of edges in the graph. For the next e lines, each line contains two integers i, j, denoting that there is an edge connecting node i and node j. There is a 0 on a single line following the last test instance. There are at most 10 test instances.

输出

For each instance, there are several lines of output pending on the input. Each line of output consists of two integer numbers separated by a single space, where the first number is the path length and the second number is the total number of shortest paths of the corresponding length. The output should display the results from length 1 to the maximal length in the graph incrementally.

样例输入

3
3
0 1
1 2
2 0
5
5
0 1
1 2
1 3
2 4
3 4
0

样例输出

1 6
1 10
2 8
3 2

提示

  1. 2 ≤ n ≤ 200, nodes are numbered as 0, 1, 2, … , n − 1.
  2. Any counting number is within the range of integer.
    题意:有N个点,每两个直接相连的点之间的权值看做1,遍历这个图,统计从长度为1到长度为max 的路个有几条。
    思路:Floyd算法。
#include<bits/stdc++.h>
#define INF 0x3f3f3f3f
using namespace std;
int Map[205][205];
int cnt[205];
int n,e,a,b;
int main()
{
    while(scanf("%d",&n) &&n!=0)
    {
        memset(Map,INF,sizeof(Map));
        memset(cnt,0,sizeof(cnt));
        for(int i=0;i<n;i++)
            Map[i][i] = 0;
        scanf("%d",&e);
        for(int i=0;i<e;i++)
        {
            scanf("%d %d",&a,&b);
            Map[a][b]=Map[b][a]=1;
        }
        for(int k=0;k<n;k++)
        {
            for(int i=0;i<n;i++)
            {
                for(int j=0;j<n;j++)
                {
                    Map[i][j]=min(Map[i][j],Map[i][k]+Map[k][j]);
                }
            }
        }
        for(int i=0;i<n;i++)
        {
             for(int j=0;j<n;j++)
             {
                 if(Map[i][j]!=INF)
                    cnt[Map[i][j]]++;
             }
        }
        for(int i=1;i<=n-1;i++)
        {
            if(cnt[i]!=0)
                printf("%d %d\n",i,cnt[i]);
        }
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值