当前位置:网站首页>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】104. Maximum depth of binary tree
OperatingSystemMXBean获取系统性能指标
【LeetCode】206. Reverse linked list
内卷的正确打开方式
Docker-compose安装mysql
VPS8701 电源管理(PMIC) VPS8701
给你一个大厂面试的机会,你能面试上吗?进来看看!
2022牛客多校四_G M
【LeetCode】94.二叉树的中序遍历
ReentrantLock工作原理
【每日一道LeetCode】——9. 回文数
IPIDEA的使用方式
OperatingSystemMXBean to get system performance metrics
【LeetCode】102.二叉树的层序遍历
PHP WebSehll backdoor script and detection tool
Chapter 11_Database Design Specifications
cadence landscape bindkey
数仓:数仓从ETL到ELT架构的转化以及俩者的区别
【LeetCode】104.二叉树的最大深度
Heuristic merge, DSU on Tree









