Data Structures and Algorithms (English) - 6-11 Shortest Path [2](25 分)

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

题目链接:点击打开链接

题目大意:略。

解题思路:注意:if(S==-1) break; 而不是 if(S==-1) return; 否则最后的 -1 的更新没办法更新上去。

AC 代码

voidShortestDist( MGraphGraph, intdist[], VertexS )
{
intvis[MaxVertexNum];
for(inti=0;i<MaxVertexNum;i++) vis[i]=0, dist[i]=INFINITY;
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] &&mi+Graph->G[S][i]<dist[i])
dist[i]=mi+Graph->G[S][i];
    }
for(inti=0;i<MaxVertexNum;i++)
if(dist[i]==INFINITY) dist[i]=-1;
}
目录
打赏
0
0
0
0
37
分享
相关文章
Data Structures and Algorithms (English) - 6-17 Shortest Path [4](25 分)
Data Structures and Algorithms (English) - 6-17 Shortest Path [4](25 分)
125 0
Data Structures and Algorithms (English) - 6-11 Shortest Path [1](25 分)
Data Structures and Algorithms (English) - 6-11 Shortest Path [1](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 分)
116 0
Data Structures and Algorithms (English) - 7-9 Huffman Codes(30 分)
Data Structures and Algorithms (English) - 7-9 Huffman Codes(30 分)
115 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 分)
115 0
Data Structures and Algorithms (English) - 6-15 Iterative Mergesort(25 分)
Data Structures and Algorithms (English) - 6-15 Iterative Mergesort(25 分)
200 0
Data Structures and Algorithms (English) - 6-13 Topological Sort(25 分)
Data Structures and Algorithms (English) - 6-13 Topological Sort(25 分)
119 0
Data Structures and Algorithms (English) - 7-18 Hashing - Hard Version(30 分)
Data Structures and Algorithms (English) - 7-18 Hashing - Hard Version(30 分)
230 0
Data Structures and Algorithms (English) - 7-18 Hashing - Hard Version(30 分)
Data Structures and Algorithms (English) - 6-1 Deque(25 分)
Data Structures and Algorithms (English) - 6-1 Deque(25 分)
110 0
Data Structures and Algorithms (English) - 6-7 Isomorphic(20 分)
Data Structures and Algorithms (English) - 6-7 Isomorphic(20 分)
138 0

热门文章

最新文章

AI助理

你好,我是AI助理

可以解答问题、推荐解决方案等