当前位置:网站首页>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;
}边栏推荐
- 【LeetCode】145.二叉树的后序遍历
- 1. 获取数据-requests.get()
- 【LeetCode】1374. Generate a string with an odd number of each character
- 【LeetCode】102. Level order traversal of binary tree
- Duplicate entry ‘XXX‘ for key ‘XXX.PRIMARY‘解决方案。
- 总体写作原则
- Chrome浏览器无法加载已解压的.crx文件的解决办法
- 8万字带你入门Rust
- 【LeetCode】145. Postorder Traversal of Binary Tree
- 很有意思的经历,很有意思的项目--文件夹对比工具
猜你喜欢

第一章——线性表(顺序表和链表)

aws s3上传文件

Go语学习笔记 - gorm使用 - 表增删改查 Web框架Gin(八)

svm.SVC应用实践1--乳腺癌检测

MySQL8 -- use msi (graphical user interface) under Windows installation method

aws s3 upload file

PHP WebSehll backdoor script and detection tool

WebShell connection tools (Chinese kitchen knife, WeBaCoo, Weevely) use

常见的SQL面试题:经典50例

Navicat cannot connect to database Mysql because of WiFi
随机推荐
第10章_索引优化与查询优化
JSP Webshell free kill
第 304 场力扣周赛
Reasons and solutions for Invalid bound statement (not found)
Chrome浏览器无法加载已解压的.crx文件的解决办法
【LeetCode】1374. 生成每种字符都是奇数个的字符串
2W字!梳理50道经典计算机网络面试题(收藏版)
WebShell连接工具(中国菜刀、WeBaCoo、Weevely)使用
just write blindly = feelings
feign调用不通问题,JSON parse error Illegal character ((CTRL-CHAR, code 31)) only regular white space (r
EasyGBS平台播放视频时偶尔出现播放失败是什么原因?
简单的页面跳转活动
国标GB28181协议EasyGBS平台兼容老版本收流端口的功能实现
第一章——线性表(顺序表和链表)
【LeetCode】94.二叉树的中序遍历
Tree Chain Segmentation-
AcWing 1053. 修复DNA 题解(状态机DP、AC自动机)
mysql使用on duplicate key update批量更新数据
PHP WebSehll backdoor script and detection tool
Common SQL interview questions: 50 classic examples