Data Structures and Algorithms (English) - 6-17 Shortest Path [4](25 分)

简介: Data Structures and Algorithms (English) - 6-17 Shortest Path [4](25 分)

题目链接:点击打开链接


题目大意:略。


解题思路:注意:因为一开始 mi==0 导致 0+无法到达的点(INF)==INF==dist[i],s.t. num[i]、path[i] 误被更新,所以需要再加个判断条件——“Graph->G[S][i]!=INFINITY”,这样也省了再末尾重新更新。


AC 代码

voidShortestDist( MGraphGraph, intdist[], intpath[], VertexS )
{
intvis[MaxVertexNum], num[MaxVertexNum];
for(inti=0;i<Graph->Nv;i++) num[i]=vis[i]=0, dist[i]=INFINITY, path[i]=-1;
dist[S]=0;
while(1)
    {
intmi=INFINITY; S=-1;
for(inti=0;i<Graph->Nv;i++)
if(!vis[i] &&mi>dist[i]) mi=dist[i], S=i;
if(S==-1) break;
vis[S]=1;
for(inti=0;i<Graph->Nv;i++)
        {
if(!vis[i] &&Graph->G[S][i]+mi<dist[i])
            {
dist[i]=Graph->G[S][i]+mi;
num[i]=num[S]+1;
path[i]=S;
            }
elseif(!vis[i] &&Graph->G[S][i]+mi==dist[i] &&Graph->G[S][i]!=INFINITY)
            {
if(num[S]+1<num[i])
                {
num[i]=num[S]+1;
path[i]=S;
                }
            }
        }
    }
for(inti=0;i<MaxVertexNum;i++)
if(INFINITY==dist[i]) dist[i]=-1;
}
目录
相关文章
|
存储 算法
PAT (Advanced Level) Practice 1046 Shortest Distance (20 分)
PAT (Advanced Level) Practice 1046 Shortest Distance (20 分)
91 0
PAT (Advanced Level) Practice 1046 Shortest Distance (20 分)
|
存储 容器
Data Structures and Algorithms (English) - 7-18 Hashing - Hard Version(30 分)
Data Structures and Algorithms (English) - 7-18 Hashing - Hard Version(30 分)
219 0
Data Structures and Algorithms (English) - 7-18 Hashing - Hard Version(30 分)
Data Structures and Algorithms (English) - 6-11 Shortest Path [2](25 分)
Data Structures and Algorithms (English) - 6-11 Shortest Path [2](25 分)
123 0
Data Structures and Algorithms (English) - 6-16 Shortest Path [3](25 分)
Data Structures and Algorithms (English) - 6-16 Shortest Path [3](25 分)
102 0
Data Structures and Algorithms (English) - 6-11 Shortest Path [1](25 分)
Data Structures and Algorithms (English) - 6-11 Shortest Path [1](25 分)
112 0
Data Structures and Algorithms (English) - 7-9 Huffman Codes(30 分)
Data Structures and Algorithms (English) - 7-9 Huffman Codes(30 分)
100 0
Data Structures and Algorithms (English) - 6-13 Topological Sort(25 分)
Data Structures and Algorithms (English) - 6-13 Topological Sort(25 分)
108 0
Data Structures and Algorithms (English) - 6-15 Iterative Mergesort(25 分)
Data Structures and Algorithms (English) - 6-15 Iterative Mergesort(25 分)
185 0
Data Structures and Algorithms (English) - 6-8 Percolate Up and Down(20 分)
Data Structures and Algorithms (English) - 6-8 Percolate Up and Down(20 分)
103 0
Data Structures and Algorithms (English) - 6-7 Isomorphic(20 分)
Data Structures and Algorithms (English) - 6-7 Isomorphic(20 分)
124 0