PTA-Shortest Path1-2-3-4
pta上的四道最短路径的问题,第一次学习图就上这些代码实在是令我感到头大
6-1 Shortest Path [1] (25分)
Write a program to find the unweighted shortest distances from any vertex to a given source vertex in a digraph.
Format of functions:
void ShortestDist( LGraph Graph, int dist[], Vertex S );
where LGraph is defined as the following:
typedef struct AdjVNode *PtrToAdjVNode;
struct AdjVNode{
Vertex AdjV;
PtrToAdjVNode Next;
};
typedef struct Vnode{
PtrToAdjVNode FirstEdge;
} AdjList[MaxVertexNum];
typedef struct GNode *PtrToGNode;
struct GNode{
int Nv;
int Ne;
AdjList G;
};
typedef PtrToGNode LGraph;
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.
Sample program of judge:
#include <stdio.h>
#include <stdlib.h>
typedef enum {false, true} bool;
#define MaxVertexNum 10 /* maximum number of vertices */
typedef int Vertex; /* vertices are numbered from 0 to MaxVertexNum-1 */
typedef struct AdjVNode *PtrToAdjVNode;
struct AdjVNode{
Vertex AdjV;
PtrToAdjVNode Next;
};
typedef struct Vnode{
PtrToAdjVNode FirstEdge;
} AdjList[MaxVertexNum];
typedef struct GNode *PtrToGNode;
struct GNode{
int Nv;
int Ne;
AdjList G;
};
typedef PtrToGNode LGraph;
LGraph ReadG(); /* details omitted */
void ShortestDist( LGraph Graph, int dist[], Vertex S );
int main()
{
int dist[MaxVertexNum];
Vertex S, V;
LGraph G = ReadG();
scanf("%d", &S);
ShortestDist( G, dist, S );
for ( V=0; V<G->Nv; V++ )
printf("%d ", dist[V]);
return 0;
}
/* Your function will be put here */
Sample Input (for the graph shown in the figure):
7 9
0 1
0 5
0 6
5 3
2 1
2 6
6 4
4 5
6 5
2
Sample Output:
-1 1 0 3 2 2 1
代码如下:
void ShortestDist(LGraph Graph,int dist[],Vertex S)
{
for(int i=0;i<Graph->Nv;i++)
{
dist[i] = -1;
}
dist[S] = 0;
int visited[MaxVertexNum] = {0};
while(1)
{
int m = MaxVertexNum;
int t=-1;
for(int i=0;i<Graph->Nv;i++)
{
if(!visited[i] && dist[i] != -1 && dist[i]<m)
{
m = dist[i];
t = i;
}
}
if(t==-1)
{
break;
}
visited[t] = 1;
PtrToAdjVNode p = Graph->G[t].FirstEdge;
while(p)
{
if(!visited[p->AdjV])
{
if(dist[p->AdjV] == -1 || dist[p->AdjV] > dist[t]+1 )
{
dist[p->AdjV] = dist[t]+1;
}
}
p = p->Next;
}
}
}
6-2 Shortest Path [2] (25分)
Write a program to find the weighted shortest distances from any vertex to a given source vertex in a digraph. It is guaranteed that all the weights are positive.
Format of functions:
void ShortestDist( MGraph Graph, int dist[], 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.
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[], Vertex S );
int main()
{
int dist[MaxVertexNum];
Vertex S, V;
MGraph G = ReadG();
scanf("%d", &S);
ShortestDist( G, dist, S );
for ( V=0; V<G->Nv; V++ )
printf("%d ", dist[V]);
return 0;
}
/* Your function will be put here */
Sample Input (for the graph shown in the figure):
7 9
0 1 1
0 5 1
0 6 1
5 3 1
2 1 2
2 6 3
6 4 4
4 5 5
6 5 12
2
Sample Output:
-1 2 0 13 7 12 3
代码程序
void ShortestDist(MGraph Graph,int dist[],Vertex S)
{
for(int i=0;i<Graph->Nv;i++)
{
dist[i] = Graph->G[S][i];
}
int collected[MaxVertexNum] = {0};
collected[S] = 1;
for(int i=0;i<Graph->Nv;i++)
{
int min = INFINITY;
int t = INFINITY;
for(int j=0;j<Graph->Nv;j++)
{
if(!collected[j] && dist[j]<min)
{
min = dist[j];
t = j;
}
}
if(t==INFINITY)
{
continue;
}
collected[t] = 1;
for(int j=0;j<Graph->Nv;j++)
{
if(!collected[j])
{
if(dist[j] > Graph->G[t][j]+min)
{
dist[j] = Graph->G[t][j]+min;
}
}
}
}
for(int j=0;j<Graph->Nv;j++)
{
if(j == S)
{
dist[S] = 0;
}
if(dist[j] == INFINITY)
{
dist[j] = -1;
}
}
}
6-3 Shortest Path [3] (25分)
Write a program to not only find the weighted shortest distances, but also count the number of different minimum paths from any vertex to a given source vertex in a digraph. It is guaranteed that all the weights are positive.
Format of functions:
void ShortestDist( MGraph Graph, int dist[], int count[], 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. The number of different minimum paths from V to the source S is supposed to be stored in count[V] and count[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 count[], Vertex S );
int main()
{
int dist[MaxVertexNum], count[MaxVertexNum];
Vertex S, V;
MGraph G = ReadG();
scanf("%d", &S);
ShortestDist( G, dist, count, S );
for ( V=0; V<G->Nv; V++ )
printf("%d ", dist[V]);
printf("\n");
for ( V=0; V<G->Nv; V++ )
printf("%d ", count[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 30
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
1 1 4 1 1 3 0 3
代码如下:
void ShortestDist(MGraph Graph , int dist[],int count[], Vertex S)
{
int collected[MaxVertexNum] = {0};
for(int i=0;i<Graph->Nv;i++)
{
dist[i] = INFINITY;
count[i] = 0;
}
dist[S] = 0;
count[S] = 1;
while(1)
{
int t = -1;
int m = INFINITY;
for(int i=0;i<Graph->Nv;i++)
{
if(!collected[i] && dist[i]<m)
{
m = dist[i];
t = i;
}
}
if(t == -1)
{
break;
}
collected[t] = 1;
for(int i=0;i<Graph->Nv;i++)
{
if(collected[i] || Graph->G[t][i] == INFINITY)
{
continue;
}
if(dist[t]+Graph->G[t][i] < dist[i])
{
dist[i] = dist[t]+Graph->G[t][i];
count[i] = count[t];
}
else if(dist[i] == dist[t]+Graph->G[t][i])
{
count[i] += count[t];
}
}
}
for(int i=0;i<Graph->Nv;i++)
{
if(dist[i] == INFINITY)
dist[i] = -1;
}
}
6-4 Shortest Path [4] (25分)
6-4 Shortest Path [4] (25分)
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
void ShortestDist(MGraph Graph,int dist[],int path[],Vertex S)
{
int i;
int visited[MaxVertexNum];
for(i=0;i<Graph->Nv;i++)
{
dist[i] = INFINITY;
path[i] = -1;
visited[i] = 0;
if(i==S)
{
dist[i] = 0;
}
}
while(1)
{
int minindex;
int min = INFINITY;
for(i=0;i<Graph->Nv;i++)
{
if(!visited[i]&&dist[i]<min)
{
minindex = i;
min = dist[minindex];
}
}
if(min == INFINITY)break;
visited[minindex] = 1;
int j;
for(j = 0;j<Graph->Nv;j++)
{
if(!visited[j]&&Graph->G[minindex][j]!=INFINITY)
{
if(dist[minindex]+Graph->G[minindex][j]<dist[j])
{
dist[j] = dist[minindex]+Graph->G[minindex][j];
path[j] = minindex;
}
}
}
}
for(i=0;i<Graph->Nv;i++)
{
if(dist[i] == INFINITY)
dist[i] = -1;
}
}