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

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

题目链接:点击打开链接

题目大意:略。

解题思路:为何此题不需要更新最小值呢?因为是有向图,而且默认相邻两点路径一定为1,那么如图中的 6->5 和 4->5 可以看出来,一定是先加入队列的,一定是最小的结果了,所以不需要再判断更新最小值。

AC 代码

voidShortestDist(LGraphGraph, intdist[], VertexS)
{
intfront=0, rear=0;
PtrToAdjVNodep=NULL;
Vertex*que=(Vertex*)malloc(MaxVertexNum*sizeof(Vertex));
for(inti=0;i<MaxVertexNum;i++)
que[i]=dist[i]=-1;
dist[S]=0;
que[0]=S;
while(front<=rear)
    {
Vertexs=que[front];
p=Graph->G[s].FirstEdge;
while(p)
        {
Vertexv=p->AdjV;
if(dist[v]==-1)
            {
dist[v]=1+dist[s];
que[++rear]=v;
            }
p=p->Next;
        }
front++;
    }
}
目录
相关文章
|
存储 算法
PAT (Advanced Level) Practice 1046 Shortest Distance (20 分)
PAT (Advanced Level) Practice 1046 Shortest Distance (20 分)
88 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 分)
215 0
Data Structures and Algorithms (English) - 7-18 Hashing - Hard Version(30 分)
Data Structures and Algorithms (English) - 6-16 Shortest Path [3](25 分)
Data Structures and Algorithms (English) - 6-16 Shortest Path [3](25 分)
98 0
Data Structures and Algorithms (English) - 6-17 Shortest Path [4](25 分)
Data Structures and Algorithms (English) - 6-17 Shortest Path [4](25 分)
104 0
Data Structures and Algorithms (English) - 6-11 Shortest Path [2](25 分)
Data Structures and Algorithms (English) - 6-11 Shortest Path [2](25 分)
117 0
Data Structures and Algorithms (English) - 6-13 Topological Sort(25 分)
Data Structures and Algorithms (English) - 6-13 Topological Sort(25 分)
104 0
Data Structures and Algorithms (English) - 7-9 Huffman Codes(30 分)
Data Structures and Algorithms (English) - 7-9 Huffman Codes(30 分)
98 0
Data Structures and Algorithms (English) - 6-15 Iterative Mergesort(25 分)
Data Structures and Algorithms (English) - 6-15 Iterative Mergesort(25 分)
183 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 分)
99 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 分)
136 0