邻接表实现Dijkstra最短路径算法

#include 
#define INF 9999
using namespace std;
struct node
{
	int node,next;
	int weight;
}edge[10000];
int first_arc[300],cnt;
void addEdge(int x,int y,int w)
{
	edge[cnt].node=y;
	edge[cnt].next=first_arc[x];
	first_arc[x]=cnt;
	edge[cnt].weight=w;
	++cnt;
}
int main()
{
	int n,m,u,v,w,start;
	cin>>n>>m>>start;
	for(int i=0;i>u>>v>>w;
		addEdge(u,v,w);
	}
	int dist[100],s[100],path[100];
	for(int i=0;i=0;--j)
		    	   cout<<','<

测试数据:

7 12 0
0 1 4
0 3 6
0 2 6
1 4 7
1 2 1
3 2 2
3 5 5
2 4 6
2 5 4
4 6 6
5 4 1

5 6 8

输出:

0 to 1 with length 4 :0,1
0 to 2 with length 5 :0,1,2
0 to 3 with length 6 :0,3
0 to 4 with length 10 :0,1,2,5,4
0 to 5 with length 9 :0,1,2,5
0 to 6 with length 16 :0,1,2,5,4,6

参考 数据结构教程/李春葆主编.—4版.—北京:清华大学出版社,2013.1

你可能感兴趣的:(数据结构)