有了一张自驾旅游路线图,你会知道城市间的高速公路长度、以及该公路要收取的过路费。现在需要你写一个程序,帮助前来咨询的游客找一条出发地和目的地之间的最短路径。如果有若干条路径都是最短的,那么需要输出最便宜的一条路径。
输入格式:
输入说明:输入数据的第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
结尾无空行
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#define MaxVertexNum 505
#define INFINITY 65535
typedef int Vertex;
typedef int WeightType;
typedef int PriceType;
//图结点的定义,把图的信息打个包
typedef struct GNode * PtrToGNode;
struct GNode {
int Nv;//顶点数
int Ne;//边数
WeightType G[MaxVertexNum][MaxVertexNum];//邻接矩阵
PriceType P[MaxVertexNum][MaxVertexNum];
//DataType Data[MaxVertexNum];
};
typedef PtrToGNode MGraph;
//边结点的定义
typedef struct ENode * PtrToENode;
struct ENode {
Vertex V1,V2;
WeightType Weight;
PriceType Price;
};
typedef PtrToENode Edge;
//创建一个有Vertex个顶点,边为0的图
MGraph CreateGraph(int VertexNum){
Vertex Vi,Vj;
MGraph Graph=(MGraph)malloc(sizeof(struct GNode));
Graph->Nv=VertexNum;
Graph->Ne=0;
//将邻接矩阵设为没边
for(Vi=0;Vi<VertexNum;Vi++){
for(Vj=0;Vj<VertexNum;Vj++){
Graph->G[Vi][Vj]=INFINITY;
Graph->P[Vi][Vj]=INFINITY;
}
}
return Graph;
}
void InsertEdge(MGraph graph,Edge E){
//插入有向边<V1,V2>
graph->G[E->V1][E->V2]=E->Weight;
graph->P[E->V1][E->V2]=E->Price;
//若是无向图,还要插入<V2,V1>
graph->G[E->V2][E->V1]=E->Weight;
graph->P[E->V2][E->V1]=E->Price;
}
Vertex S,D;
MGraph ReadBuildGraph(){
//输入格式为顶点 边数 起点 终点\n然后每行:起点 终点 权重 价格
MGraph graph;
int VertexNum,i;
Edge E;
scanf("%d",&VertexNum);
graph=CreateGraph(VertexNum);
scanf("%d",&(graph->Ne));
scanf("%d %d",&S,&D);
if(graph->Ne){
E=(Edge)malloc(sizeof(struct ENode));
for(i=0;i<graph->Ne;i++){
scanf("%d %d %d %d",&(E->V1),&(E->V2),&(E->Weight),&E->Price);
InsertEdge(graph,E);
}
}
return graph;
}
WeightType dist[MaxVertexNum]={INFINITY};
Vertex path[MaxVertexNum]={-1};
PriceType price[MaxVertexNum]={INFINITY};
/* 邻接矩阵存储 - 有权图的单源最短路算法 */
#define ERROR -1
Vertex FindMinDist( MGraph Graph, int collected[] )
{ /* 返回未被收录顶点中dist最小者 */
Vertex MinV, V;
int MinDist = INFINITY;
for (V=0; V<Graph->Nv; V++) {
if ( collected[V]==false && dist[V]<MinDist) {
MinDist = dist[V]; /* 更新最小距离 */
MinV = V; /* 更新对应顶点 */
}
}
if (MinDist < INFINITY) /* 若找到最小dist */
return MinV; /* 返回对应的顶点下标 */
else return ERROR; /* 若这样的顶点不存在,返回错误标记 */
}
//Vertex FindMinDist( MGraph Graph, int collected[] )
//{ /* 返回未被收录顶点中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; /* 若这样的顶点不存在,返回错误标记 */
//}
bool Dijkstra( MGraph Graph, Vertex S )
{
int collected[MaxVertexNum];
Vertex V, W;
/* 初始化:此处默认邻接矩阵中不存在的边用INFINITY表示 */
for ( V=0; V<Graph->Nv; V++ ) {
dist[V] = Graph->G[S][V];
if ( dist[V]<INFINITY ){
path[V] = S;
price[V]=Graph->P[S][V];
}
else{
path[V] = -1;
price[V]=INFINITY;
}
collected[V] = false;
}
/* 先将起点收入集合 */
dist[S] = 0;
price[S]=0;
collected[S] = true;
while (1) {
/* V = 未被收录顶点中dist最小者 */
V = FindMinDist( Graph,collected );
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]变小 或dist[W]不变但price[W]变小*/
if ( dist[V]+Graph->G[V][W] < dist[W] || (dist[V]+Graph->G[V][W]==dist[W]&&price[V]+Graph->P[V][W]<price[W]) ) {
dist[W] = dist[V]+Graph->G[V][W]; /* 更新dist[W] */
path[W] = V; /* 更新S到W的路径 */
price[W]=price[V]+Graph->P[V][W];
}
}
} /* while结束*/
return true; /* 算法执行完毕,返回正确标记 */
}
int main()
{
MGraph graph=ReadBuildGraph();
Dijkstra(graph,S);
int i;
PriceType price=0;
for(i=D;path[i]!=-1;i=path[i])
price+=graph->P[i][path[i]];
printf("%d %d",dist[D],price);
}
while (1) {
/* V = 未被收录顶点中dist最小者 */
V = FindMinDist( Graph,collected );
if ( V==ERROR ) /* 若这样的V不存在 */
break; /* 算法结束 */
collected[V] = true; /* 收录V */
if(V==D) break;//**********->其实也可以加上这句话<-***********
此前版本:
先前在下面这个函数里,我还多加上了一个条件 ,都是通过的,不过我感觉还是这样写稳一点
>=< 有时间再来想下
(9.7.2021更新:只要保证每个节点到源点的最短路径是相等的最短路径中price最低的就行了,所以以下更改都不需要)
Vertex FindMinDist( MGraph Graph, int collected[] )
{ /* 返回未被收录顶点中dist最小者 */
Vertex MinV, V;
int MinDist = INFINITY;
for (V=0; V<Graph->Nv; V++) {
if ( collected[V]==false && dist[V]<INFINITY && dist[V]<=MinDist) {
if(dist[V]<MinDist || (dist[V]==MinDist&&price[V]<price[MinV]) ){
MinDist = dist[V]; /* 更新最小距离 */
MinV = V; /* 更新对应顶点 */
}
}
}
if (MinDist < INFINITY) /* 若找到最小dist */
return MinV; /* 返回对应的顶点下标 */
else return ERROR; /* 若这样的顶点不存在,返回错误标记 */
}
也多加上过这个条件
Vertex FindMinDist( MGraph Graph, int collected[] )
{ /* 返回未被收录顶点中dist最小者 */
Vertex MinV, V;
int MinDist = INFINITY;
for (V=0; V<Graph->Nv; V++) {
if ( collected[V]==false && dist[V]<INFINITY && dist[V]<=MinDist) {
if(dist[V]<MinDist || (dist[V]==MinDist&&Graph->P[V][path[V]]<Graph->P[MinV][path[MinV]]) ){
MinDist = dist[V]; /* 更新最小距离 */
MinV = V; /* 更新对应顶点 */
}
}
}
if (MinDist < INFINITY) /* 若找到最小dist */
return MinV; /* 返回对应的顶点下标 */
else return ERROR; /* 若这样的顶点不存在,返回错误标记 */
}
其实这两个条件都是不加可以过