07- 6 Tourism Planning (25 points)

Title Description

With a road map for travel by car, you will know the length of the highway between the city and the road tolls to be charged. Now you need to write a program to help the tourists to come to counseling to find the shortest path between a departure and destination. If there are several paths are the shortest, you need to output the least expensive path.

Input formats:

Input Description: first input data line 4 is given positive integers N, M, S, D, where N (2≤N≤500) is the number of the city, the city assumed way numbered 0 ~ (N-1 ); M is the number of the highway; S is the point of departure cities numbered; D is the number of urban destinations. Subsequent M rows, each row is given an information highway, are: 1 urban, urban 2, the length of the highway, toll revenue, separated by a space intermediate, figures are an integer of not more than 500. Enter exist to ensure solution.

Output formats:

The total length and charge output path, separated by spaces between the numbers in a row, the end of the output can not have extra space.

Sample input:

4 5 0 3
0 1 1 20
1 3 2 30
0 3 4 10
0 2 2 20
2 3 1 20

Sample output:

3 40

Problem-solving ideas

According meaning of the questions, we can see that this is a single-source shortest path problem diagram entitled, Dijkstra's algorithm to solve, to note that the meaning of the shortest path problem this is 先比较长度,再比较花费.

Code

#include <stdio.h>
#include <stdlib.h>

#define MAXSIZE 500
#define INFINITY 600

typedef struct {
    int length;
    int pay;
} Edge;

typedef struct {
    int vertexCount;
    int edgeCount;
    Edge matrix[MAXSIZE][MAXSIZE];
} Graph, *PGraph;

PGraph createGraph(int N, int M);
void initDistance(Edge dis[], PGraph graph, int S);
void dijkstra(Edge dis[], PGraph graph, int S);
int getNext(Edge dis[], int N);

visited[MAXSIZE] = {0};

int main() {
    int N, M, S, D;
    scanf("%d %d %d %d", &N, &M, &S, &D);
    PGraph graph = createGraph(N, M);
    Edge dis[MAXSIZE];              //记录路径长度与花费
    initDistance(dis, graph, S);
    dijkstra(dis, graph, S);
    printf("%d %d\n", dis[D].length, dis[D].pay);
    return 0;
}

PGraph createGraph(int N, int M) {
    PGraph graph = (PGraph) malloc(sizeof(Graph));
    graph->vertexCount = N;
    graph->edgeCount = M;
    for (int i = 0; i < N; i++) {
        for (int j = 0; j < N; j++) {
            graph->matrix[i][j].length = INFINITY;
            graph->matrix[i][j].pay = INFINITY;
        }
    }
    for (int i = 0; i < M; i++) {
        int x, y, len, pay;
        scanf("%d %d %d %d", &x, &y, &len, &pay);
        graph->matrix[x][y].length = len;
        graph->matrix[x][y].pay = pay;
        graph->matrix[y][x].length = len;
        graph->matrix[y][x].pay = pay;
    }
    return graph;
}

void initDistance(Edge dis[], PGraph graph, int S) {
    for (int i = 0; i < graph->vertexCount; i++) {
        if (i == S) {
            dis[i].length = 0;
            dis[i].pay = 0;
        } else {
            dis[i].length = graph->matrix[S][i].length;
            dis[i].pay = graph->matrix[S][i].pay;
        }
    }
}

void dijkstra(Edge dis[], PGraph graph, int S) {
    visited[S] = 1;         //将起点标记为已访问
    int N = graph->vertexCount;
    while (1) {
        int next = getNext(dis, N);
        if (next == -1) break;
        visited[next] = 1;
        for (int i = 0; i < N; i++) {
            //对next每个未访问的邻接点
            if (graph->matrix[next][i].length != INFINITY && !visited[i]) {
                if (dis[i].length > dis[next].length + graph->matrix[next][i].length) {
                    dis[i].length = dis[next].length + graph->matrix[next][i].length;
                    dis[i].pay = dis[next].pay + graph->matrix[next][i].pay;
                } else if (dis[i].length == dis[next].length + graph->matrix[next][i].length) {
                    if (dis[i].pay > dis[next].pay + graph->matrix[next][i].pay) {
                        dis[i].length = dis[next].length + graph->matrix[next][i].length;
                        dis[i].pay = dis[next].pay + graph->matrix[next][i].pay;
                    }
                }
            }
        }
    }   
}

int getNext(Edge dis[], int N) {
    int next = -1;
    int minLength = INFINITY, minPay = INFINITY;
    for (int i = 0; i < N; i++) {
        if (!visited[i] && minLength >= dis[i].length) {
            if (minLength > dis[i].length) {
                minLength = dis[i].length;
                minPay = dis[i].pay;
                next = i;
            } else if (minLength == dis[i].length) {
                if (minPay > dis[i].pay) {
                    minLength = dis[i].length;
                    minPay = dis[i].pay;
                    next = i;
                }
            }
        }
    }
    return next;
}

Guess you like

Origin www.cnblogs.com/AndyHY-Notes/p/12631241.html