当前位置:网站首页>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;
}边栏推荐
猜你喜欢

Tree Chain Segmentation-

mysql8.0.28下载和安装详细教程,适配win11

数仓:为什么说 ETL 的未来不是 ELT,而是 EL (T)

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

Chapter 11_Database Design Specifications

DVWA之SQL注入

WebShell Feature Value Summary and Detection Tool
![[Daily LeetCode]——1. The sum of two numbers](/img/11/8a68f4ecb24fa19e3c804d536cdbec.png)
[Daily LeetCode]——1. The sum of two numbers

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

MySQL八股文背诵版
随机推荐
Duplicate entry ‘XXX‘ for key ‘XXX.PRIMARY‘解决方案。
【LeetCode】83.删除排序链表中的重复元素
WebShell特征值汇总与检测工具
mysql8.0.28下载和安装详细教程,适配win11
Curriculum Vitae;CV
Go语学习笔记 - gorm使用 - 表增删改查 Web框架Gin(八)
leetcode 143. 重排链表
iVX低代码平台系列详解 -- 概述篇(二)
OperatingSystemMXBean获取系统性能指标
7-43 字符串关键字的散列映射 (25 分) 谜之测试点
【LeetCode】1374. Generate a string with an odd number of each character
7、MySQL Workbench 导出导入数据库
[LeetCode] 94. Inorder traversal of binary tree
PyTorch(六)——PyTorch可视化
生成器知道鉴别器在无条件GANs中应该学习什么
Qt自定义控件和模板分享
【LeetCode】102.二叉树的层序遍历
svm.SVC application practice 1--Breast cancer detection
2022.8.1-----leetcode.1374
Recursively check if a configuration item has changed and replace it