07-图6 旅游规划
一、题目
有了一张自驾旅游路线图,你会知道城市间的高速公路长度、以及该公路要收取的过路费。现在需要你写一个程序,帮助前来咨询的游客找一条出发地和目的地之间的最短路径。如果有若干条路径都是最短的,那么需要输出最便宜的一条路径。
输入格式:
输入说明:输入数据的第1行给出4个正整数N、M、S、D,其中N(2)是城市的个数,顺便假设城市的编号为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的改编(增添了fare数组)
关键逻辑:
1.fare的优先级低于dist。也就是一旦收录V导致其邻接点(记为W)发生改变,同时更新dist[W]与fare[W]。
2.当发现另一条最短路也能达到dist,比较fare,若小,则更新fare与parent(记录路径的数组)
三、参考源代码
(notes: 大部分是Copy慕课上老师给出的参考代码,
createMGraph太懒了就自己写了。)
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
#include <cstdio> #include <stdlib.h> const int INFINITY = 65536; const int MaxVertexNum = 501; const int ERROR = -1; int dist[MaxVertexNum], path[MaxVertexNum], collected[MaxVertexNum],fare[MaxVertexNum]; typedef int Vertex; typedef int WeightType; typedef struct GNode * MGraph; struct GNode { int Nv; int Ne; WeightType G[MaxVertexNum][MaxVertexNum]; WeightType F[MaxVertexNum][MaxVertexNum]; }; Vertex FindMinDist( MGraph Graph ) { /* 返回未被收录顶点中dist最小者 */ Vertex MinV, V; int MinDist = INFINITY; for (V=0; V<Graph->Nv; V++) { if ( collected[V]==false && dist[V]<MinDist) { /* 若V未被收录,且dist[V]更小 */ MinDist = dist[V]; /* 更新最小距离 */ MinV = V; /* 更新对应顶点 */ } } if (MinDist < INFINITY) /* 若找到最小dist */ return MinV; /* 返回对应的顶点下标 */ else return ERROR; /* 若这样的顶点不存在,返回错误标记 */ } void init(MGraph Graph) { Vertex V; for( V=0; V<Graph->Nv; V++) { fare[V] = INFINITY; } } bool Dijkstra( MGraph Graph, Vertex S ) { Vertex V, W; /* 初始化:此处默认邻接矩阵中不存在的边用INFINITY表示 */ for ( V=0; V<Graph->Nv; V++ ) { dist[V] = Graph->G[S][V]; fare[V] = Graph->F[S][V]; if ( dist[V]<INFINITY ) path[V] = S; else path[V] = -1; collected[V] = false; } /* 先将起点收入集合 */ dist[S] = 0; collected[S] = true; while (1) { /* V = 未被收录顶点中dist最小者 */ V = FindMinDist( Graph ); if ( V==ERROR ) /* 若这样的V不存在 */ break; /* 算法结束 */ collected[V] = true; /* 收录V */ for( W=0; W<Graph->Nv; W++ ) /* 对图中的每个顶点W */ /* 若W是V的邻接点并且未被收录 */ if ( collected[W]==false && Graph->G[V][W]<INFINITY ) { if ( Graph->G[V][W]<0 ) /* 若有负边 */ return false; /* 不能正确解决,返回错误标记 */ /* 若收录V使得dist[W]变小 */ if ( dist[V]+Graph->G[V][W] < dist[W]) { dist[W] = dist[V]+Graph->G[V][W]; /* 更新dist[W] */ path[W] = V; /* 更新S到W的路径 */ fare[W] = fare[V]+Graph->F[V][W]; } /*若收录V使得fare[W]变小*/ else if(dist[V]+Graph->G[V][W] == dist[W] and fare[V] + Graph->F[V][W] < fare[W] ) { path[W] = V; /* 更新S到W的路径 */ fare[W] = fare[V]+Graph->F[V][W]; } } } /* while结束*/ return true; /* 算法执行完毕,返回正确标记 */ } MGraph buildGraph(int Nv, int Ne) { int S, E, len, fees; MGraph Graph = (MGraph) malloc(sizeof(struct GNode)); //init Graph->Nv = Nv; Graph->Ne = Ne; for(int i=0; i<Nv; i++) { for(int j=0; j<Nv; j++) { Graph->G[i][j] = INFINITY; Graph->F[i][j] = INFINITY; } } for(int i=0; i<Ne; i++) { scanf("%d %d %d %d", &S, &E, &len, &fees); Graph->G[S][E] = len; Graph->G[E][S] = len; Graph->F[E][S] = fees; Graph->F[S][E] = fees; } return Graph; } int main() { int N, M, S, D; scanf("%d%d%d%d", &N, &M, &S, &D); MGraph Graph = buildGraph(N, M); Dijkstra(Graph, S); printf("%d %d\n", dist[D], fare[D]); }