题目描述
有了一张自驾旅游路线图,你会知道城市间的高速公路长度、以及该公路要收取的过路费。现在需要你写一个程序,帮助前来咨询的游客找一条出发地和目的地之间的最短路径。如果有若干条路径都是最短的,那么需要输出最便宜的一条路径。
输入格式:
输入说明:输入数据的第1行给出4个正整数N、M、S、D,其中N(2≤N≤500)是城市的个数,顺便假设城市的编号为0~(N−1);M是高速公路的条数;S是出发地的城市编号;D是目的地的城市编号。随后的M行中,每行给出一条高速公路的信息,分别是:城市1、城市2、高速公路长度、收费额,中间用空格分开,数字均为整数且不超过500。输入保证解的存在。
输出格式:
在一行里输出路径的长度和收费总额,数字间以空格分隔,输出结尾不能有多余空格。
输入样例:
4 5 0 3 0 1 1 20 1 3 2 30 0 3 4 10 0 2 2 20 2 3 1 20
输出样例:
3 40
解题思路
根据题意,可以看出这是一个单源有权图最短路径问题,使用Dijkstra算法解决,需要注意的是本题最短路径的含义是先比较长度,再比较花费
。
代码
#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; }
来源:https://www.cnblogs.com/AndyHY-Notes/p/12631241.html