组队训练赛第五场5058 Problem D Network Report

问题 D: Network Report

时间限制: 1 Sec  内存限制: 128 MB
提交: 140  解决: 59
[提交] [状态] [讨论版] [命题人: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个节点,编号为0——n-1;输入e行,每行有两个数x、y,表示x和y之间的边长为1;求每个节点到除本身外其余节点的最短距离,然后统计具有相同的一个节点到另一个节点的最短距离的数量;

思路:计算每个节点到除本身外其余节点的最短距离,然后统计相同距离的数量就可以了……

#include<cstdio>
#include<cstring>
#include<math.h>
#include<algorithm>
#include<queue>
#include<vector>
#include<iostream>
#include<map>
#include<queue>
#define mes(a,b) memset(a,b,sizeof(a))
#define rep(i,m,n) for(i=m;i<=n;i++)
typedef long long ll;
using namespace std;
int max3(int a,int b,int c){return max(max(a,b),c);}
ll min3(ll a,ll b,ll c){return min(min(a,b),c);}
const double PI=acos(-1);
const int inf=0x3f3f3f3f;
const double esp=1e-6;
const int maxn=1e6+5;
const int mod=1e9+7;
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
ll lcm(ll a,ll b){return a/gcd(a,b)*b;}
ll inv(ll b){if(b==1)return 1; return (mod-mod/b)*inv(mod%b)%mod;}
ll fpow(ll n,ll k){ll r=1;for(;k;k>>=1){if(k&1)r=r*n%mod;n=n*n%mod;}return r;}
ll Fpow(ll n,ll k){ll r=1;for(;k;k>>=1){if(k&1)r=r*n;n=n*n;}return r;}
int main()
{
   int n;
   while(scanf("%d",&n)!=EOF)
   {
       if(n==0)
        break;
       int e;
       scanf("%d",&e);
       int i;
       int a[210][210];
       for(i=0;i<210;i++)
       {
           for(int j=0;j<210;j++)
           {
               a[i][j]=inf;
           }
           a[i][i]=0;
       }
       for(i=0;i<e;i++)
       {
           int x,y;
           scanf("%d%d",&x,&y);
           a[x][y]=a[y][x]=1;
       }
       for(i=0;i<n;i++)//计算每个点到其余点的最短距离
       {
           for(int j=0;j<n;j++)
           {
               for(int k=0;k<n;k++)
               {
                   a[j][k]=min(a[j][k],a[j][i]+a[i][k]);
               }
           }
       }

       int vis[100010]={0};
       for(i=0;i<n;i++)
       {
           for(int j=0;j<n;j++)
           {
               if(a[i][j]<inf)
               {
                   vis[a[i][j]]++;
               }
           }
       }
       for(i=1;i<100010;i++)
       {
           if(vis[i]!=0)
           {
               printf("%d %d\n",i,vis[i]);
           }
       }
   }


    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值