当前位置:网站首页>7-36 社交网络图中结点的“重要性”计算 (30 分) 不用迪杰斯特拉也不用弗洛伊德
7-36 社交网络图中结点的“重要性”计算 (30 分) 不用迪杰斯特拉也不用弗洛伊德
2022-08-02 02:56:00 【兄dei!】
原题:https://pintia.cn/problem-sets/15/problems/863
这种无权图的最短路径直接用类似于BFS的思路就可以计算了;
#include<iostream>
#include<queue>
#define MAXSIZE 10010
#define inf 666666666
using namespace std;
int N,M,K;
int FLAG=1;
int G[MAXSIZE][MAXSIZE];
int dist[MAXSIZE];//距离
void unweight(int x)//无权图的最短路径
{
int i;
queue<int>q;
for(i=1;i<=N;i++)dist[i]=-1;//初始化为-1
q.push(x); //入队
dist[x]=0;//并且自己到自己距离为0
while(!q.empty())
{
int v=q.front();
q.pop();
for(i=1;i<=N;i++)
{
if(dist[i]==-1&&G[v][i]==1)//只要是-1的就说明还未访问过,并且v和I直接有边
{
dist[i]=dist[v]+1;//直接加1就行了
q.push(i); //记得入队
}
}
}
}
int main()
{
int i,j;
double sum=0.0;
scanf("%d%d",&N,&M);
for(i=1;i<=M;i++)
{
int x,y;
scanf("%d%d",&x,&y);
G[x][y]=G[y][x]=1;
}
scanf("%d",&K);
for(i=0;i<K;i++)
{
int x;
scanf("%d",&x);
unweight(x);//计算最短路径
for(j=1;j<=N;j++)
{
if(j==x)continue;
if(dist[j]==-1)
{
FLAG=0; break;
}
sum+=dist[j];
}
if(FLAG==1)
{
sum/=(N-1);
printf("Cc(%d)=%.2f\n",x,1/sum);
}
else
printf("Cc(%d)=0.00\n",x);
sum=0;
}
return 0;
}
边栏推荐
- feign调用不通问题,JSON parse error Illegal character ((CTRL-CHAR, code 31)) only regular white space (r
- 2W字!梳理50道经典计算机网络面试题(收藏版)
- IPFS deployment and file upload (golang)
- 详解最强分布式锁工具:Redisson
- MySQL八股文背诵版
- 2022牛客多校三_F G
- MySQL8.0.26安装配置教程(windows 64位)
- iVX低代码平台系列详解 -- 概述篇(二)
- leetcode 143. 重排链表
- analog IC layout-Design for reliability
猜你喜欢
随机推荐
【Koltin Flow(三)】Flow操作符之中间操作符(一)
DVWA之SQL注入
01-Node-Express系统框架搭建(express-generator)
8万字带你入门Rust
2022牛客多校三_F G
VPS8505 微功率隔离电源隔离芯片 2.3-6V IN /24V/1A 功率管
aws s3 upload file
启发式合并、DSU on Tree
aws s3上传文件
国标GB28181协议EasyGBS平台兼容老版本收流端口的功能实现
MySQL8.0.26安装配置教程(windows 64位)
MySQL8 -- use msi (graphical user interface) under Windows installation method
(一)Redis: 基于 Key-Value 的存储系统
内卷的正确打开方式
IPFS部署及文件上传(golang)
Invalid bound statement (not found)出现的原因和解决方法
“带薪划水”偷刷阿里老哥的面经宝典,三次挑战字节,终成正果
架构:微服务网关(SIA-Gateway)简介
centos安装mysql8
第一章——线性表(顺序表和链表)