Data Structures and Algorithms (English) - 6-16 Shortest Path [3](25 分)

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

题目链接:点击打开链接

题目大意:略。

解题思路:注意:因为一开始 mi==0 导致 0+无法到达的点(INF)==INF==dist[i],s.t. count[i] 误被更新,所以结束判断时,如果没有到达的点 i,需要 count[i]=0。

AC 代码

voidShortestDist( MGraphGraph, intdist[], intcount[], VertexS )
{
intvis[MaxVertexNum];
for(inti=0;i<MaxVertexNum;i++) dist[i]=INFINITY, vis[i]=count[i]=0;
dist[S]=0, count[S]=1;
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] &&mi+Graph->G[S][i]<dist[i])
dist[i]=mi+Graph->G[S][i], count[i]=count[S];
elseif(!vis[i] &&mi+Graph->G[S][i]==dist[i]) // 注释针对点count[i]+=count[S];
        }
    }
for(inti=0;i<MaxVertexNum;i++)
if(dist[i]==INFINITY) dist[i]=-1, count[i]=0;
}
目录
相关文章
|
存储 算法
PAT (Advanced Level) Practice 1046 Shortest Distance (20 分)
PAT (Advanced Level) Practice 1046 Shortest Distance (20 分)
73 0
PAT (Advanced Level) Practice 1046 Shortest Distance (20 分)
Data Structures and Algorithms (English) - 6-11 Shortest Path [1](25 分)
Data Structures and Algorithms (English) - 6-11 Shortest Path [1](25 分)
90 0
Data Structures and Algorithms (English) - 6-17 Shortest Path [4](25 分)
Data Structures and Algorithms (English) - 6-17 Shortest Path [4](25 分)
90 0
Data Structures and Algorithms (English) - 6-11 Shortest Path [2](25 分)
Data Structures and Algorithms (English) - 6-11 Shortest Path [2](25 分)
103 0
Data Structures and Algorithms (English) - 6-13 Topological Sort(25 分)
Data Structures and Algorithms (English) - 6-13 Topological Sort(25 分)
86 0
Data Structures and Algorithms (English) - 7-9 Huffman Codes(30 分)
Data Structures and Algorithms (English) - 7-9 Huffman Codes(30 分)
81 0
Data Structures and Algorithms (English) - 6-15 Iterative Mergesort(25 分)
Data Structures and Algorithms (English) - 6-15 Iterative Mergesort(25 分)
166 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 分)
85 0
Data Structures and Algorithms (English) - 7-12 How Long Does It Take(25 分)
Data Structures and Algorithms (English) - 7-12 How Long Does It Take(25 分)
87 0
Data Structures and Algorithms (English) - 6-2 Two Stacks In One Array(20 分)
Data Structures and Algorithms (English) - 6-2 Two Stacks In One Array(20 分)
117 0

热门文章

最新文章