#include<iostream> #include<vector> #include<iterator> #include<limits.h> #include<string> using namespace std; int n; //顶点个数 vector<vector<int> >g; //g:图(graph)(用邻接矩阵(adjacent matrix)表示) int s; //s:源点(source) vector<bool>known; //known:各点是否知道最短路径 vector<int>dist; //dist:源点 s 到各点的最短路径长度 vector<int>pre; //prev 各点的最短路径的前一顶点 void Dijkstra() { //贪心算法 known.assign(n,false); dist.assign(n,INT_MAX); pre.resize(n); //初始化 known、dist、prev dist[s]=0; //初始化源点 s 到自身的路径 for(;;) { int min=INT_MAX,v=s; for(int i=0; i<n; ++i) if(!known[i]&&min>dist[i]) min=dist[i],v=i; //寻找未知的最短路径的顶点 v if(min==INT_MAX)break; //如果找不到,退出 known[v]=true; for(int w=0; w<n; ++w) //遍历所有 v 指向的顶点 w if(!known[w]&&g[v][w]<INT_MAX && dist[w]>dist[v]+g[v][w]) //调整顶点 w 的最短路径长度 dist 和最短路径的前一顶点 prev dist[w]=dist[v]+g[v][w],pre[w]=v; } } void Print_SP(int v) { if(v!=s)Print_SP(pre[v]); cout<<v<<" "; } int main() { n=5; g.assign(n,vector<int>(n,INT_MAX)); //构建图 g[0][1]=10; g[0][3]=30; g[0][4]=100; g[1][2]=50; g[2][4]=10; g[3][2]=20; g[3][4]=60; s=0; Dijkstra(); copy(dist.begin(),dist.end(),ostream_iterator<int>(cout," ")); cout<<endl; for(int i=0; i<n; ++i) if(dist[i]!=INT_MAX) { cout<<s<<"->"<<i<<":"; Print_SP(i); cout<<endl; } return 0; }每行代码什么意思
时间: 2024-04-04 18:33:43 浏览: 14
这段代码实现了 Dijkstra 算法,用于求解带权图中一个顶点到其他顶点的最短路径。
- 第 1 行:引入所需的头文件。
- 第 3 行:定义变量 n,表示图中顶点的个数。
- 第 4 行:定义变量 g,表示图(邻接矩阵)。
- 第 5~7 行:定义变量 s,表示源点。
- 第 8~9 行:定义变量 known,表示各点是否知道最短路径。
- 第 10~11 行:定义变量 dist,表示源点 s 到各点的最短路径长度。
- 第 12~13 行:定义变量 pre,表示各点的最短路径的前一顶点。
- 第 15~31 行:实现 Dijkstra 算法的函数,采用贪心策略。
- 第 33~39 行:实现递归输出最短路径的函数。
- 第 41~47 行:主函数,构建带权图,调用 Dijkstra 算法,输出最短路径。
- 第 49~51 行:返回 0,表示程序正常结束。
相关问题
统一文件名只用#include<iostream> #include<string.h>
这两个头文件的作用不同,<iostream> 是用于输入输出流的库,而<string.h> 是用于字符串处理的库。如果你的程序中需要使用字符串相关函数,如strcpy、strlen等,就需要用到<string.h>头文件。如果要使用cout和cin等输入输出流对象则需要使用<iostream>头文件。如果你两个都需要用到,那么你就需要同时包含这两个头文件。例如:
```cpp
#include <iostream>
#include <string.h>
using namespace std;
int main() {
char str1[10] = "hello";
char str2[10] = "world";
strcat(str1, str2); // 使用<string.h>中的字符串拼接函数strcat
cout << str1 << endl; // 使用<iostream>中的输出流对象cout
return 0;
}
```
#include<iostream>与#include<stdio.h>的区别
#include<iostream> 和#include<stdio.h>是两个不同的头文件引用语句,它们有以下区别:
1. 功能:#include<iostream> 是C++标准库中的头文件,用于输入输出操作,包含了cin、cout等对象和函数的定义。而#include<stdio.h>是C语言标准库中的头文件,用于输入输出操作,包含了printf、scanf等函数的定义。
2. 命名空间:#include<iostream> 引入的是std命名空间,需要使用std::cout、std::cin等来访问其中的元素。而#include<stdio.h>没有命名空间的概念,函数名直接使用即可。
3. 扩展名:#include<iostream> 的头文件没有扩展名,因为它是C++标准库中的头文件。而#include<stdio.h>的头文件扩展名为.h,因为它是C语言标准库中的头文件。
4. 文件位置:#include<iostream> 是C++标准库中的头文件,通常位于编译器的include路径中。而#include<stdio.h>是C语言标准库中的头文件,也位于编译器的include路径中。
总之,#include<iostream> 用于C++程序中,提供了更方便的输入输出操作;而#include<stdio.h> 用于C程序中,提供了基本的输入输出功能。
相关推荐
![text/plain](https://img-home.csdnimg.cn/images/20210720083646.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)