首页 > 其他 > 详细

DIJKSTRA 临接表

时间:2017-09-21 17:24:43      阅读:259      评论:0      收藏:0      [点我收藏+]
技术分享
 1 #include <iostream>
 2 #include <algorithm>
 3 #include <cmath>
 4 #include <cstdio>
 5 #include <cstring>
 6 #include <cstdlib>
 7 using namespace std;
 8 int value[10010],to[10010],next[10010];
 9 int head[10010],total;
10 int book[10010];
11 int dis[10010];
12 int n,m;
13 void adl(int a,int b,int c)
14 {
15     total++;
16     to[total]=b;
17     value[total]=c;
18     next[total]=head[a];
19     head[a]=total;
20 }
21 void dijkstra(int u)
22 {
23     memset(dis,88,sizeof(dis));
24     memset(book,0,sizeof(book));
25     dis[u]=0;
26     for(int i=1;i<=n;i++)
27     {
28         int start=-1;
29         for(int j=1;j<=n;j++)
30             if(book[j]==0 && (dis[start]>dis[j] || start==-1))
31                 start=j;
32         book[start]=1;
33         for(int e=head[start];e;e=next[e])
34             dis[to[e]]=min(dis[to[e]],dis[start]+value[e]);
35     }
36 }
37 int main()
38 {
39     cin>>n>>m;
40     for(int i=1;i<=m;i++)
41     {
42         int a,b,c;
43         cin>>a>>b>>c;
44         adl(a,b,c);
45      } 
46      dijkstra(1);
47      for(int i=1;i<=n;i++)
48          cout<<dis[i]<<" ";
49 }
View Code

 

DIJKSTRA 临接表

原文:http://www.cnblogs.com/dfzg/p/7569533.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!