An unknown college student, known as Caigou in the world of martial arts
original author: jacky Li
Email: [email protected]
Time of completion:2022.12.10
Last edited: 2022.12.11
Table of contents
Algorithm 6.10-6.11 Shortest path
Level 1: Algorithm 6.10 Dijkstra Shortest Path
Algorithm 6.12 Topological sorting
The author has something to say
Algorithm 6.10-6.11 Shortest path
Level 1: Algorithm 6.10 Dijkstra Shortest Path
mission details
The task of this level: write a Dijkstra algorithm for the shortest path and use an adjacency matrix to represent the graph.
related information
In order to complete this task, you need to master: 1. How to create an adjacency matrix 2. How to write Dijkstra.
Programming requirements
Follow the prompts and add code in the editor on the right to output the shortest path corresponding to the two vertices.
Input and output instructions
Input instructions: The first line is the number of vertices n and the number of edges e. The second line is n vertex symbols. The next line e is e edges. The first two characters of each line represent an edge of the undirected graph, and the third represents the edge weight. The two vertices in the last row represent the starting point and the end point
Output Description: Shortest path between vertices
Test instruction
The platform will test the code you write:
Test input:
6 10
a b c d e f
a b 6
a c 5
a d 1
c d 5
b d 5
b e 3
d e 6
e f 6
d f 4
c f 2
a f
Test output:
a-->d
c-->f
b-->e
d-->f
b-->d
a-->d-->f
Reference Code
//算法6.8 普里姆算法
#include <iostream>
#include <algorithm>
#include <map>
#include <cmath>
#include <cstring>
#define IOS std::ios::sync_with_stdio(false)
//#define YES cout << "1"
//#define NO cout << "0"
#define MaxInt 0x3f
#define MVNum 100
#define OK 1
#define ERROR -1
const int N = 1003;
using namespace std;
typedef long long LL;
typedef char VerTexType;
typedef int ArcType;
int *D=new int[MVNum]; //用于记录最短路的长度
bool *S=new bool[MVNum]; //标记顶点是否进入S集合
int *Path=new int[MVNum]; //用于记录最短路顶点的前驱
//------------图的邻接矩阵-----------------
typedef struct{
VerTexType vexs[MVNum]; //顶点表
ArcType arcs[MVNum][MVNum]; //邻接矩阵
int vexnum,arcnum; //图的当前点数和边数
}AMGraph;
int LocateVex(AMGraph G , VerTexType v){
//确定点v在G中的位置
for(int i = 0; i < G.vexnum; ++i)
if(G.vexs[i] == v)
return i;
return -1;
}//LocateVex
void CreateUDN(AMGraph &G){
//采用邻接矩阵表示法,创建无向网G
int i , j , k;
cin >> G.vexnum >> G.arcnum; //输入总顶点数,总边数
for(i = 0; i < G.vexnum; ++i)
cin >> G.vexs[i]; //依次输入点的信息
for(i = 0; i < G.vexnum; ++i) //初始化邻接矩阵,边的权值均置为极大值MaxInt
for(j = 0; j < G.vexnum; ++j)
G.arcs[i][j] = MaxInt;
for(k = 0; k < G.arcnum;++k)
{ //构造邻接矩阵
VerTexType v1 , v2;
ArcType w;
cin >> v1 >> v2 >> w; //输入一条边依附的顶点及权值
i = LocateVex(G, v1); j = LocateVex(G, v2); //确定v1和v2在G中的位置,即顶点数组的下标
G.arcs[i][j] = w; //边<v1, v2>的权值置为w
G.arcs[j][i] = G.arcs[i][j]; //置<v1, v2>的对称边<v2, v1>的权值为w
}//for
}//CreateUDN
void ShortestPath_DIJ(AMGraph G, int v0){
//用Dijkstra算法求有向网G的v0顶点到其余顶点的最短路径
/*************************Begin***********************/
int v,w,minn;
int n=G.vexnum;
for( v=0;v<n;v++)
{
S[v]=false;
D[v]=G.arcs[v0][v];
if(D[v]<MaxInt) Path[v]=v0;
else Path[v]=-1;
}
S[v0]=true; D[v0]=0;
for(int i=1;i<n;i++)
{
minn=MaxInt;
for( w=0;w<n;w++)
if(!S[w])
if(D[w]<minn)
v = w, minn = D[w];
if(minn<MaxInt)
{
S[v]=true;
for( w=0;w<n;w++)
if(!S[w]&&(minn+G.arcs[v][w]<D[w]))
D[w] = minn + G.arcs[v][w], Path[w] = v;
}
else break;
}
}//ShortestPath_DIJ
void DisplayPath(AMGraph G , int begin ,int temp ){
//显示最短路
if(Path[temp] != -1){
DisplayPath(G , begin ,Path[temp]);
cout << G.vexs[Path[temp]] << "-->";
}
}//DisplayPath
int main()
{
AMGraph G;
int num_start , num_destination;
VerTexType start , destination;
CreateUDN(G);
cin >> start >> destination;
num_start = LocateVex(G , start);
num_destination = LocateVex(G , destination);
ShortestPath_DIJ(G , num_start);
DisplayPath(G , num_start , num_destination);
cout << G.vexs[num_destination]<<endl;
return 0;
}//main
Level 2: Algorithm 6.11 Floyd
mission details
The task of this level: write a Floy algorithm for the shortest path and use an adjacency matrix to represent the graph.
related information
In order to complete this task, you need to master: 1. How to create an adjacency matrix 2. How to write Floyd.
Programming requirements
Follow the prompts and add code in the editor on the right to output the shortest path corresponding to the two vertices.
Input and output instructions
Input instructions: The first line is the number of vertices n and the number of edges e. The second line is n vertex symbols. The next line e is e edges. The first two characters of each line represent an edge of the undirected graph, and the third represents the edge weight. The two vertices in the last row represent the starting point and the end point
Output Description: Shortest path between vertices
Test instruction
The platform will test the code you write:
Test input:
6 10
a b c d e f
a b 6
a c 5
a d 1
c d 5
b d 5
b e 3
d e 6
e f 6
d f 4
c f 2
a f
Test output:
a-->d-->f
The length of the shortest path is:
5
Reference Code
//算法6.8 普里姆算法
#include <iostream>
#include <algorithm>
#include <map>
#include <cmath>
#include <cstring>
#define IOS std::ios::sync_with_stdio(false)
//#define YES cout << "1"
//#define NO cout << "0"
#define MaxInt 0x3f
#define MVNum 100
#define OK 1
#define ERROR -1
const int N = 1003;
using namespace std;
typedef long long LL;
typedef char VerTexType;
typedef int ArcType;
int Path[MVNum][MVNum]; //最短路径上顶点vj的前一顶点的序号
int D[MVNum][MVNum]; //记录顶点vi和vj之间的最短路径长度
//------------图的邻接矩阵---------------
typedef struct{
VerTexType vexs[MVNum]; //顶点表
ArcType arcs[MVNum][MVNum]; //邻接矩阵
int vexnum,arcnum; //图的当前点数和边数
}AMGraph;
int LocateVex(AMGraph G , VerTexType v){
//确定点v在G中的位置
for(int i = 0; i < G.vexnum; ++i)
if(G.vexs[i] == v)
return i;
return -1;
}//LocateVex
void CreateUDN(AMGraph &G){
//采用邻接矩阵表示法,创建有向网G
int i , j , k;
cin >> G.vexnum >> G.arcnum; //输入总顶点数,总边数
for(i = 0; i < G.vexnum; ++i)
cin >> G.vexs[i]; //依次输入点的信息
for(i = 0; i < G.vexnum; ++i) //初始化邻接矩阵,边的权值均置为极大值MaxInt
for(j = 0; j < G.vexnum; ++j)
if(j != i) G.arcs[i][j] = MaxInt;
else G.arcs[i][j] = 0;
for(k = 0; k < G.arcnum; ++ k)
{ //构造邻接矩阵
VerTexType v1 , v2;
ArcType w;
cin >> v1 >> v2 >> w; //输入一条边依附的顶点及权值
i = LocateVex(G, v1); j = LocateVex(G, v2); //确定v1和v2在G中的位置,即顶点数组的下标
G.arcs[i][j] = w; //边<v1, v2>的权值置为w
}//for
}//CreateUDN
void ShortestPath_Floyed(AMGraph G)
{
//用Floyd算法求有向网G中各对顶点i和j之间的最短路径
int i, j, k;
for(i = 0; i < G.vexnum; i ++)
for(j = 0; j < G.vexnum; j ++)
{
D[i][j] = G.arcs[i][j];
if((D[i][j] < MaxInt) && (i != j)) Path[i][j] = i;
else Path[i][j] = -1;
}
for(k = 0; k < G.vexnum; k ++)
for(i = 0; i < G.vexnum; i ++)
for(j = 0; j < G.vexnum; j ++)
if(D[i][k] + D[k][j] < D[i][j])
D[i][j] = D[i][k] + D[k][j], Path[i][j] = Path[k][j];
}//ShortestPath_Floyed
void DisplayPath(AMGraph G , int begin ,int temp )
{
if(Path[begin][temp] != -1)
{
DisplayPath(G , begin ,Path[begin][temp]);
cout << G.vexs[Path[begin][temp]] << "-->";
}
}//DisplayPath
int main(){
AMGraph G;
char start , destination;
int num_start , num_destination;
CreateUDN(G);
ShortestPath_Floyed(G);
cin >> start >> destination;
num_start = LocateVex(G , start);
num_destination = LocateVex(G , destination);
DisplayPath(G , num_start , num_destination);
cout << G.vexs[num_destination] << endl;
cout << "最短路径的长度为:" << D[num_start][num_destination] << endl;
cout <<endl;
return 0;
}//main
Algorithm 6.12 Topological sorting
mission details
The task of this level: output the topological sorting sequence of the directed graph.
Programming requirements
Follow the prompts and add code in the editor on the right to calculate and output the topological sorting of the directed graph.
Input and output instructions
Input : The first line is the number of vertices n
and arcs e
, separated by spaces. The second line is n
the vertex symbol name, separated by spaces. The next e
line is each arc, separated by spaces.
Output : If there is no loop in the network, output the topological sorting sequence separated by commas. Otherwise, output "There is a loop in the network and topological sorting cannot be performed!"
Test input:
3 3
1 2 3
1 2
1 3
2 3
Expected output:
1 , 2 , 3
Reference Code
#include <iostream>
using namespace std;
#define MVNum 100 //最大顶点数
#define OK 1
#define ERROR 0
typedef char VerTexType;
//- - - - -图的邻接表存储表示- - - - -
typedef struct ArcNode{ //边结点
int adjvex; //该边所指向的顶点的位置
struct ArcNode *nextarc; //指向下一条边的指针
}ArcNode;
typedef struct VNode{
VerTexType data; //顶点信息
ArcNode *firstarc; //指向第一条依附该顶点的边的指针
}VNode, AdjList[MVNum]; //AdjList表示邻接表类型
typedef struct{
AdjList vertices; //邻接表
AdjList converse_vertices; //逆邻接表
int vexnum, arcnum; //图的当前顶点数和边数
}ALGraph;
//- - - - - - - - - - - - - - - -
//- - - - -顺序栈的定义- - - - -
typedef struct{
int *base;
int *top;
int stacksize;
}spStack;
//- - - - - - - - - - - - - - - -
int indegree[MVNum]; //数组indegree存放个顶点的入度
spStack S;
//------------栈的相关操作----------------------
void InitStack(spStack &S){
//初始化栈
S.base = new int[MVNum];
if(!S.base)
exit(1);
S.top = S.base;
S.stacksize = MVNum;
}//InitStack
void Push(spStack &S , int i){
//进栈
if(S.top - S.base == S.stacksize)
return;
*S.top++ = i;
}//Push
void Pop(spStack &S , int &i){
//出栈
if(S.top == S.base)
return;
i = *--S.top;
}//Pop
bool StackEmpty(spStack S){
//判断栈是否为空
if(S.top == S.base)
return true;
return false;
}//StackEmpty
//-------------------------------------------------
int LocateVex(ALGraph G , VerTexType v){
//确定点v在G中的位置
for(int i = 0; i < G.vexnum; ++i)
if(G.vertices[i].data == v)
return i;
return -1;
}//LocateVex
int CreateUDG(ALGraph &G){
//创建有向图G的邻接表、逆邻接表
int i , k;
cin >> G.vexnum >> G.arcnum; //输入总顶点数,总边数
for(i = 0; i < G.vexnum; ++i){ //输入各点,构造表头结点表
cin >> G.vertices[i].data; //输入顶点值
G.converse_vertices[i].data = G.vertices[i].data;
//初始化表头结点的指针域为NULL
G.vertices[i].firstarc=NULL;
G.converse_vertices[i].firstarc=NULL;
}//for
for(k = 0; k < G.arcnum;++k){ //输入各边,构造邻接表
VerTexType v1 , v2;
int i , j;
cin >> v1 >> v2; //输入一条边依附的两个顶点
i = LocateVex(G, v1); j = LocateVex(G, v2);
//确定v1和v2在G中位置,即顶点在G.vertices中的序号
ArcNode *p1=new ArcNode; //生成一个新的边结点*p1
p1->adjvex=j; //邻接点序号为j
p1->nextarc = G.vertices[i].firstarc; G.vertices[i].firstarc=p1;
//将新结点*p1插入顶点vi的边表头部
ArcNode *p2=new ArcNode; //生成一个新的边结点*p1
p2->adjvex=i; //逆邻接点序号为i
p2->nextarc = G.converse_vertices[j].firstarc; G.converse_vertices[j].firstarc=p2;
//将新结点*p1插入顶点vi的边表头部
}//for
return OK;
}//CreateUDG
void FindInDegree(ALGraph G){
//求出各顶点的入度存入数组indegree中
/******************************Begin***********************/
for(int i=0; i<G.vexnum; i++) {
int cnt = 0;
while(G.converse_vertices[i].firstarc != NULL) {
cnt++;
G.converse_vertices[i].firstarc = G.converse_vertices[i].firstarc->nextarc;
}
indegree[i] = cnt;
}
/******************************End*************************/
}//FindInDegree
int TopologicalSort(ALGraph G , int topo[]){
//有向图G采用邻接表存储结构
//若G无回路,则生成G的一个拓扑序列topo[]并返回OK,否则ERROR
/******************************Begin***********************/
FindInDegree(G);
int i,m=0;
ArcNode *p;
InitStack(S);
for(i=0;i<G.vexnum;i++)
if(!indegree[i]) Push(S,i);
while(!StackEmpty(S)){
Pop(S,i); topo[m]=i; m++;
for(p=G.vertices[i].firstarc;p;p=p->nextarc)
{
int k=p->adjvex;
if(!(--indegree[k])) Push(S,k);
}
}
if(m<G.vexnum) return 0; else return 1;
/******************************End*************************/
}//TopologicalSort
int main(){
ALGraph G;
CreateUDG(G);
int *topo = new int [G.vexnum];
if(TopologicalSort(G , topo)){
for(int j = 0 ; j < G.vexnum; j++){
if(j != G.vexnum - 1)
cout << G.vertices[topo[j]].data << " , ";
else
cout << G.vertices[topo[j]].data << endl << endl;
}//for
}
else
cout << "网中存在环,无法进行拓扑排序!" <<endl;
return OK;
}//main
6.13 Critical path
mission details
The task of this level: output the critical path of the network.
Programming requirements
Follow the prompts and add code in the editor on the right to calculate the critical path of the net.
Input and output instructions
Input: The first line is the number of vertices n
and arcs e
, separated by spaces. The second line is n
the vertex symbol name, separated by spaces. The next e
line is each arc and its weight, separated by spaces.
Output: If there is no loop in the network, output the critical path. Otherwise, output "There is a loop in the network and topological sorting cannot be performed!"
Test input:
3 3
1 2 3
1 2 5
1 3 6
2 3 1
Expected output:
1-->3
1-->2
2-->3
Reference Code
//算法6.13 关键路径算法
#include <iostream>
using namespace std;
#define MVNum 100 //最大顶点数
#define BDNum MVNum * (MVNum - 1) //最大边数
#define OK 1
#define ERROR 0
typedef char VerTexType;
//- - - - -图的邻接表存储表示- - - - -
typedef struct ArcNode{ //边结点
int adjvex; //该边所指向的顶点的位置
int weight; //权值
struct ArcNode *nextarc; //指向下一条边的指针
}ArcNode;
typedef struct VNode{
VerTexType data; //顶点信息
ArcNode *firstarc; //指向第一条依附该顶点的边的指针
}VNode, AdjList[MVNum]; //AdjList表示邻接表类型
typedef struct{
AdjList vertices; //邻接表
AdjList converse_vertices; //逆邻接表
int vexnum, arcnum; //图的当前顶点数和边数
}ALGraph;
//- - - - - - - - - - - - - - - -
//- - - - -顺序栈的定义- - - - -
typedef struct{
int *base;
int *top;
int stacksize;
}spStack;
//- - - - - - - - - - - - - - - -
int indegree[MVNum]; //数组indegree存放个顶点的入度
int ve[BDNum]; //事件vi的最早发生时间
int vl[BDNum]; //事件vi的最迟发生时间
int topo[MVNum]; //记录拓扑序列的顶点序号
spStack S;
//----------------栈的操作--------------------
void InitStack(spStack &S){
//栈的初始化
S.base = new int[MVNum];
if(!S.base)
exit(1);
S.top = S.base;
S.stacksize = MVNum;
}//InitStack
void Push(spStack &S , int i){
//入栈
if(S.top - S.base == S.stacksize)
return;
*S.top++ = i;
}//Push
void Pop(spStack &S , int &i){
//出栈
if(S.top == S.base)
return;
i = *--S.top;
}//Pop
bool StackEmpty(spStack S){
//判断栈是否为空
if(S.top == S.base)
return true;
return false;
}//StackEmpty
//---------------------------------------
int LocateVex(ALGraph G , VerTexType v){
//确定点v在G中的位置
for(int i = 0; i < G.vexnum; ++i)
if(G.vertices[i].data == v)
return i;
return -1;
}//LocateVex
int CreateUDG(ALGraph &G){
//创建有向图G的邻接表、逆邻接表
int i , k;
cin >> G.vexnum >> G.arcnum; //输入总顶点数,总边数
for(i = 0; i < G.vexnum; ++i){ //输入各点,构造表头结点表
cin >> G.vertices[i].data; //输入顶点值
G.converse_vertices[i].data = G.vertices[i].data;
//初始化表头结点的指针域为NULL
G.vertices[i].firstarc=NULL;
G.converse_vertices[i].firstarc=NULL;
}//for
for(k = 0; k < G.arcnum;++k){ //输入各边,构造邻接表
VerTexType v1 , v2;
int i , j , w;
cin >> v1 >> v2 >> w; //输入一条边依附的两个顶点
i = LocateVex(G, v1); j = LocateVex(G, v2);
//确定v1和v2在G中位置,即顶点在G.vertices中的序号
ArcNode *p1=new ArcNode; //生成一个新的边结点*p1
p1->adjvex=j; //邻接点序号为j
p1->nextarc = G.vertices[i].firstarc; G.vertices[i].firstarc=p1;
p1->weight = w;
//将新结点*p1插入顶点vi的边表头部
ArcNode *p2=new ArcNode; //生成一个新的边结点*p1
p2->adjvex=i; //逆邻接点序号为i
p2->nextarc = G.converse_vertices[j].firstarc; G.converse_vertices[j].firstarc=p2;
p2->weight = w;
//将新结点*p1插入顶点vi的边表头部
}//for
return OK;
}//CreateUDG
void FindInDegree(ALGraph G){
//求出各顶点的入度存入数组indegree中
int i , count;
for(i = 0 ; i < G.vexnum ; i++){
count = 0;
ArcNode *p = G.converse_vertices[i].firstarc;
if(p){
while(p){
p = p->nextarc;
count++;
}
}//if
indegree[i] = count;
}//for
}//FindInDegree
int TopologicalOrder(ALGraph G , int topo[]){
//有向图G采用邻接表存储结构
//若G无回路,则生成G的一个拓扑序列topo[]并返回OK,否则ERROR
int i , m;
FindInDegree(G); //求出各顶点的入度存入数组indegree中
InitStack(S); //栈S初始化为空
for(i = 0; i < G.vexnum; ++i)
if(!indegree[i]) Push(S, i); //入度为0者进栈
m = 0; //对输出顶点计数,初始为0
while(!StackEmpty(S)){ //栈S非空
Pop(S, i); //将栈顶顶点vi出栈
topo[m]=i; //将vi保存在拓扑序列数组topo中
++m; //对输出顶点计数
ArcNode *p = G.vertices[i].firstarc; //p指向vi的第一个邻接点
while(p){
int k = p->adjvex; //vk为vi的邻接点
--indegree[k]; //vi的每个邻接点的入度减1
if(indegree[k] ==0) Push(S, k); //若入度减为0,则入栈
p = p->nextarc; //p指向顶点vi下一个邻接结点
}//while
}//while
if(m < G.vexnum) return ERROR; //该有向图有回路
else return OK;
}//TopologicalOrder
int i,k,e,l,j;
int CriticalPath(ALGraph G){
//G为邻接表存储的有向网,输出G的各项关键活动
int i,k,j;
ArcNode *p;
//G为邻接表存储的有向网,输出G的各项关键活动
if (!TopologicalOrder(G, topo)) return ERROR;
//调用拓扑排序算法,使拓扑序列保存在topo中,若调用失败,则存在有向环
int n = G.vexnum; //n为顶点的个数
for (i = 0;i < n;i++) //给每个事件的最早发生时间置初值为0
ve[i] = 0;
/*按照拓扑次序求每个事件的最早发生时间*/
for (i = 0;i < n;i++) //for循环结束才能求得最早发生时间
{
k = topo[i]; //取得拓扑序列中的顶点序号k
p = G.vertices[k].firstarc; //p指向k的第一个邻接点
while (p != NULL) //依次更新k的所有邻接顶点的最早发生时间
{
j = p->adjvex; //j为邻接顶点的序号
if (ve[j] < ve[k] + p->weight) //更新顶点j的最早发生时间ve[j]
ve[j] = ve[k] + p->weight;
p = p->nextarc; //p指向k的下一个邻接顶点
}
}
for (i = 0;i < n;i++) //给每个事件的最迟发生时间置初值为ve[n-1]
vl[i] = ve[n - 1];
/*按逆拓扑次序求每个事件的最迟发生时间*/
for (i = n - 1;i >= 0;i--)
{
k = topo[i]; //取得拓扑序列中的顶点序号k
ArcNode* p = G.vertices[k].firstarc; //p指向k的第一个邻接顶点
while (p != NULL)
{ //根据k的邻接点,更新k的最迟发生时间
j = p->adjvex; //j为邻接顶点的序号
if (vl[k] > vl[j] - p->weight) //更新顶点k的最迟发生时间vl[k]
vl[k] = vl[j] - p->weight;
p = p->nextarc; //p指向下一个邻接顶点
}
}
/*判断每一活动是否为关键活动*/
for (i = 0;i < n;i++)
{ //每次循环针对vi为活动开始点的所有活动
p = G.vertices[i].firstarc; //p指向i的第一个邻接顶点
while (p != NULL)
{
j = p->adjvex; //j为i的邻接顶点的序号
e = ve[i]; //计算活动<vi,vj>的最早开始时间
l = vl[j] - p->weight; //计算活动<vi,vj>的最迟开始时间
if (e == l) //若为关键活动,输出<vi,vj>
cout<<G.vertices[i].data<<"-->"<<G.vertices[j].data<<" ";
p = p->nextarc; //p指向i的下一个邻接顶点
}
}
}//CriticalPath
int main(){
ALGraph G;
CreateUDG(G);
int *topo = new int [G.vexnum];
if(!CriticalPath(G))
cout << "网中存在环,无法进行拓扑排序!" << endl;
cout << endl;
return OK;
}//main
6.14 Six Dimensions of Space
mission details
The task of this level: verify the six-dimensional space theory.
Programming requirements
According to the prompts, add code in the editor on the right to calculate the percentage of paths starting from vertex 0 and whose length does not exceed 7.
Input and output instructions
Input: The first line is the number of vertices n
and edges e
, separated by spaces
接下来`$$e$$`行是每条边
Output: The percentage of paths whose fixed-point length does not exceed 7, represented in decimals, retaining 6 decimal places.
Test input:
10 9
0 1
1 2
2 3
3 4
4 5
5 6
6 7
7 8
8 9
Expected output:
0.800000
Reference Code
#include<stdio.h>
#include<vector>
#include<queue>
#include<algorithm>
using namespace std;
vector<int> g[1003];
int n,m,num,i,visit[1003]={0};
void makeg(){ //构成临界矩阵。
int x,y;
for(i=0;i<m;i++){
scanf("%d %d",&x,&y);
g[x].push_back(y);
g[y].push_back(x);
}
}
void BFS(int x)
{ //对每个用户进行遍历。
queue<int> q;
q.push(x);
visit[x] = 1, num ++;
for(int deep = 0; deep < 7; deep ++)
{
vector<int> t; //设置一个数组来静态存储当前层次待遍历用户,若用队列存储的话,需要一个当前层次队列,以及待遍历的下一层次队列,完成后涉及到队列的交替转换,难以实现。
while(!q.empty())
{
int temp=q.front();
q.pop();
t.push_back(temp);
}
for(i = 0; i < t.size(); i ++)
{
int k = t[i];
for(int j = 0; j < g[k].size(); j ++)
{
int temp = g[k][j];
if(visit[temp]==0)
{
visit[temp]=1, num++;
q.push(temp);
}
}
}
}
}
int main(){
scanf("%d %d",&n,&m);
makeg();
num=0;
fill(visit,visit+n+1,0); //由于是1-n编号,此处要初始化到visit+n+1;
BFS(0);
printf("%f\n",double(num)/(double)n);
return 0;
}
The author has something to say
If you feel that what the blogger said is useful to you, please click "Follow" to support it. We will continue to update such issues...