1. 程式人生 > 其它 >PTA Data Structures and Algorithms (English) 6-16

PTA Data Structures and Algorithms (English) 6-16

技術標籤:PTA演算法dfs有向圖

6-16Shortest Path [3](25point(s))

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 );

whereMGraphis defined as the following:

typedef struct GNode *PtrToGNode;
struct GNode{
    int Nv;
    int Ne;
    WeightType G[MaxVertexNum][MaxVertexNum];
};
typedef PtrToGNode MGraph;

The shortest distance fromVto the sourceS

is supposed to be stored indist[V]. IfVcannot be reached fromS, store -1 instead. The number of different minimum paths fromVto the sourceSis supposed to be stored incount[V]andcount[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 

Example:

int DFS ( MGraph Graph, Vertex S, Vertex D, int dist )
{
    int ret = 0;
    if(dist >= 0) {
        if(S == D) {
            if(dist == 0) ret = 1;
        } else {
            for(int i = 0; i < Graph->Nv; i++) {
                if(S != i && dist-Graph->G[S][i] >= 0) {
                    ret += DFS(Graph, i, D, dist-Graph->G[S][i]);
                }
            }
        }
    }
    return ret;
}

void ShortestDist( MGraph Graph, int dist[], int count[], Vertex S )
{
    int visited[MaxVertexNum];
    for (int i = 0; i < MaxVertexNum; i++) {
        dist[i] = -1;
        count[i] = 0;
        visited[i] = false;
    }
    dist[S] = 0;
    for(int i = 0; i < Graph->Nv; i++) {
        int min = -1;
        for(int j = 0; j < Graph->Nv; j++) {
            if(!visited[j] && dist[j] != -1) {
                if(min == -1 || dist[min] > dist[j]) {
                    min = j;
                }
            }
        }
        if(min == -1) break;
        visited[min] = true;
        for(int j = 0; j < Graph->Nv; j++) {
            if(min != j && Graph->G[min][j] < INFINITY) {
                if(dist[j] == -1 || (dist[j] > dist[min] + Graph->G[min][j])) {
                    dist[j] = dist[min] + Graph->G[min][j];
                }
            }
        }
    }
    for(int i = 0; i < Graph->Nv; i++) {
        count[i] = DFS(Graph, S, i, dist[i]);
    }
}

思路:

先用迪傑斯特拉演算法,求得S到個點的最短距離dist[],然後從點S開始深度優先搜尋,對於到達點D時,距離餘量為0時為其中的一條路徑,count+1.