两种答案,先说思路,第一段代码即答案,第二段也是正确答案,第一个答案更符合陈越老师教材,第二种答案是对第一种的改良,但是两种思路一样,题目在最后:
建立在Dijkstra算法的基础上,多加了一个数组PathNeS来记录从S到每个点经过的最少边数,对于和S相邻的边,我们初始化其PathNeS为1;
在while的每次循环里确定v后,在for循环里,
我们先判断S通过v到达i的距离(dist[v]+Graph->G[v][i])是否等于dist[i],若等于,则判断S通过v到达i经过的最短边数(PathNeS[v]+1)是否小于原先记录的最短边数(PathNeS[i])若满足,则更新PathNeS[i]和path[i],使其通过v来到达;(注意这道题要求到这个点最短时边数最少的路)
再判断是否收集i和通过v到i是否会比原先记录的短,若满足,更新dist和path时也更新PathNeS[i]=PathNeS[v]+1;因为通过v到i的边数即再v边数基础上加一;(v已被收集,则说明dist[v]已是最短);
答案代码如下:
void ShortestDist( MGraph Graph, int dist[], int path[], Vertex S ){
int v,i,min;
static int collected[9999],PathNeS[9999];
for(i=0;i<Graph->Nv;i++){
path[i]=-1;
dist[i]=Graph->G[S][i];
if(dist[i]<INFINITY){
path[i]=S;
PathNeS[i]=1;//和S相邻的点的边数初始就是1
}
else PathNeS[i]=INFINITY;//和S不想-相连的初始化为最大值
}
collected[S]=1;//收集S
dist[S]=0;
while(1){
min=INFINITY;
v=-1;
for(i=0;i<Graph->Nv;i++){
if(!collected[i]&&dist[i]<min){
v=i;
min=dist[v];
}
}
if(v==-1) break;
collected[v]=1;
for(i=0;i<Graph->Nv;i++){
if(dist[i]==dist[v]+Graph->G[v][i]){//连个if写在一起用&&连接也可以,判断是否有更少边数的路径
if(PathNeS[v]+1<PathNeS[i]){
PathNeS[i]=PathNeS[v]+1;
path[i]=v;
}
}
if(!collected[i]&&dist[i]>dist[v]+Graph->G[v][i]){
dist[i]=dist[v]+Graph->G[v][i];
path[i]=v;
PathNeS[i]=PathNeS[v]+1;//更新最短边数
}
}
}
for(i=0;i<Graph->Nv;i++){
if(dist[i]==INFINITY) dist[i]=-1;
}
return;
}
中间再插一个改良版,陈越老师的教材中的算法(也就是进入while之前就收集S)更符合第一种答案,但是进入while以后再收集S可以让代码更简洁,但是思路是一样的,以下即这种思路的答案:
void ShortestDist( MGraph Graph, int dist[], int path[], Vertex S ) {
static int collected[9999],PathNeS[9999];
for(int i = 0;i < Graph -> Nv;i ++) {
dist[i] = INFINITY;
path[i] = -1;
}
dist[S] = 0;
path[S] = -1;
PathNeS[S] = 1;
while(1) {
int v= -1,m = INFINITY;
for(int i = 0;i < Graph -> Nv;i ++) {
if(!collected[i] && m > dist[i]) m = dist[i],v = i;
}
if(v == -1) break;
collected[v] = 1;
for(int i = 0;i < Graph -> Nv;i ++) {
if(collected[i] || Graph -> G[v][i] == INFINITY) continue;
if(dist[v] + Graph -> G[v][i] < dist[i]) {
dist[i] = dist[v] + Graph -> G[v][i];
path[i] = v;
PathNeS[i] =PathNeS[v] + 1;
}
else if(dist[v] + Graph -> G[v][i] == dist[i] && PathNeS[i] > PathNeS[v] + 1) {
PathNeS[i] = PathNeS[v] + 1;
path[i] = v;
}
}
}
for(int i = 0;i < Graph -> Nv;i ++) {
if(dist[i] == INFINITY) dist[i] = -1;
}
}
题目:
Write a program to find the weighted shortest distances from any vertex to a given source vertex in a digraph. If there is more than one minimum path from v to w, a path with the fewest number of edges is chosen. It is guaranteed that all the weights are positive and such a path is unique for any vertex.
Format of functions:
void ShortestDist( MGraph Graph, int dist[], int path[], Vertex S );
where MGraph
is defined as the following:
typedef struct GNode *PtrToGNode; struct GNode{ int Nv; int Ne; WeightType G[MaxVertexNum][MaxVertexNum]; }; typedef PtrToGNode MGraph;
The shortest distance from V
to the source S
is supposed to be stored in dist[V]
. If V
cannot be reached from S
, store -1 instead. If W
is the vertex being visited right before V
along the shortest path from S
to V
, then path[V]=W
. If V
cannot be reached from S
, path[V]=-1
, and we have path[S]=-1
.
Sample program of judge:
#include <stdio.h>
#include <stdlib.h>
typedef enum {false, true} bool;
#define INFINITY 1000000
#define MaxVertexNum 10 /* maximum number of vertices */
typedef int Vertex; /* vertices are numbered from 0 to MaxVertexNum-1 */
typedef int WeightType;
typedef struct GNode *PtrToGNode;
struct GNode{
int Nv;
int Ne;
WeightType G[MaxVertexNum][MaxVertexNum];
};
typedef PtrToGNode MGraph;
MGraph ReadG(); /* details omitted */
void ShortestDist( MGraph Graph, int dist[], int path[], Vertex S );
int main()
{
int dist[MaxVertexNum], path[MaxVertexNum];
Vertex S, V;
MGraph G = ReadG();
scanf("%d", &S);
ShortestDist( G, dist, path, S );
for ( V=0; V<G->Nv; V++ )
printf("%d ", dist[V]);
printf("\n");
for ( V=0; V<G->Nv; V++ )
printf("%d ", path[V]);
printf("\n");
return 0;
}
/* Your function will be put here */
Sample Input (for the graph shown in the figure):
8 11
0 4 5
0 7 10
1 7 40
3 0 40
3 1 20
3 2 100
3 7 70
4 7 5
6 2 1
7 5 3
7 2 50
3
Sample Output:
40 20 100 0 45 53 -1 50
3 3 3 -1 0 7 -1 0