1 int cost[Maxn][Maxn];
2 int dist[Maxn], pre[Maxn];//初始化时一般先将cost[][]初始化为无穷大,pre[]存放结点前驱,s[]标记数组,标记S中走过的结点
3 void Dijkstra(int n)
4 {
5 int v,i,j,k,min,s[Maxn];
6 v = n;
7 for(i=1; i<=n; i++)
8 {
9 dist[i] = cost[v][i];
10 s[i] = 0;
11 if (dist[i] < Maxint)
12 pre[i] = v;
13 else
14 pre[i] = 0;
15 }
16 s[v] = 1;
17 pre[v] = 0;
18 for (i=1; i<n; i++)
19 {
20 min = Maxint;
21 k = 0;
22 for(j=1; j<=n; j++)
23 {
24 if(s[j]==0 && dist[j]<min)
25 {
26 min = dist[j];
27 k = j;
28 }
29 }
30 }
31 s[k] = 1;
32 for (j=1; j<=n; j++)
33 {
34 if(s[j]==0 && dist[k]+cost[k][j]<dist[j])
35 {
36 dist[j] = dist[k] + cost[k][j];
37 pre[j] = k;
38 }
39 }
40 }