题目
有了一张自驾旅游路线图,你会知道城市间的高速公路长度、以及该公路要收取的过路费。现在需要你写一个程序,帮助前来咨询的游客找一条出发地和目的地之间的最短路径。如果有若干条路径都是最短的,那么需要输出最便宜的一条路径。
输入格式:
输入说明:输入数据的第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算法。初始化空图时,权重的数据类型应该是一个结构体,既能存储长度距离,又能存储费用。
- 在dijkstra算法中,寻找下一顶点V的时候,仍然按照最小距离寻找,只不过在更新dist、cost、path这三个数组的时候,需要注意,
距离的优先级是最高的,如果dist[W]因为添加V变小了,那么一定要进行更新。
其次是距离相等的时候,cost[W]变小了就更新,否则cost不更新.
注意:
对于源点,collected[S]一定要赋值为1,而dist[S]不一定要赋值为0,因为collected[S]=0会导致后面在FindMinDist函数中也不会用到dist[S]。cost[S]和dist[S]同理
实现
#include<stdio.h>
#include<stdlib.h>
#include<stdbool.h>
#define INFINITY 100000
#define ERROR -2
typedef int Vertex;
struct Weight
{
int Length;
int Fee;
};
typedef struct Weight WeightType;
typedef struct GNode *PtrToGNode;
typedef PtrToGNode MGraph;
struct GNode
{
int Nv;
int Ne;
WeightType **G;
};
typedef struct ENode *PtrToENode;
typedef PtrToENode Edge;
struct ENode
{
Vertex V1, V2;
WeightType W;
};
MGraph CreateGraph(int VertexNum)
{
MGraph Graph = (MGraph)malloc(sizeof(struct GNode));
Graph->Nv = VertexNum;
//allocate the room
int i,j;
Graph->G = (WeightType **)malloc(Graph->Nv*sizeof(WeightType *));
for(i=0; i<Graph->Nv; i++)
{
Graph->G[i] = (WeightType *)malloc(Graph->Nv*sizeof(WeightType));
}
//initial
for(i=0; i<Graph->Nv; i++)
for(j=0; j<Graph->Nv; j++)
{
Graph->G[i][j].Length = INFINITY;
Graph->G[i][j].Fee = INFINITY;
}
return Graph;
}
void InsertEdge(MGraph Graph, Edge E)
{
Graph->G[E->V1][E->V2] = E->W; //can assign it directly
Graph->G[E->V2][E->V1] = E->W;
}
MGraph BuildGraph(int VertexNum, int EdgeNum)
{
MGraph Graph = CreateGraph(VertexNum);
Graph->Ne = EdgeNum;
Edge E = (Edge)malloc(sizeof(struct ENode));
int i;
for(i=0; i<Graph->Ne; i++)
{
scanf("%d %d %d %d", &E->V1, &E->V2, &E->W.Length, &E->W.Fee);
InsertEdge(Graph, E);
}
return Graph;
}
Vertex FindMinDist(MGraph Graph, int dist[], int collected[])
{
Vertex MinV, V;
int MinDist = INFINITY;
for(V=0; V<Graph->Nv; V++)
{
if(collected[V] == 0 && dist[V]<MinDist)
{
MinDist = dist[V];
MinV = V;
}
}
if(MinDist == INFINITY)
return ERROR;
return MinV;
}
bool Dijkstra(MGraph Graph, int dist[], int path[], int cost[], Vertex S)
{
int *collected = (int *)malloc(Graph->Nv*sizeof(int));
Vertex V;
for(V=0; V<Graph->Nv; V++)
{
dist[V] = Graph->G[S][V].Length;
cost[V] = Graph->G[S][V].Fee;
if(dist[V] < INFINITY)
path[V] = S;
else
path[V] = -1;
collected[V] = 0;
}
// dist[S] = 0;
collected[S] = 1;
Vertex W;
while(1)
{
V = FindMinDist(Graph, dist, collected);
if(V == ERROR)
break;
collected[V] = 1;
for(W=0; W<Graph->Nv; W++)
{
if(collected[W] == 0 && Graph->G[W][V].Length < INFINITY)
{
if(Graph->G[W][V].Length<0)
return false;
if(dist[W] > dist[V]+Graph->G[W][V].Length || (dist[W] == dist[V]+Graph->G[W][V].Length && cost[W] > cost[V]+Graph->G[W][V].Fee))
{
dist[W] = dist[V]+Graph->G[W][V].Length;
cost[W] = cost[V]+Graph->G[W][V].Fee;
path[W] = V;
}
}
}
}
return true;
}
int main()
{
int N,M;
Vertex S,D;
scanf("%d %d %d %d", &N, &M, &S, &D);
MGraph Graph = BuildGraph(N, M);
int *dist = (int *)malloc(Graph->Nv * sizeof(int));
int *cost = (int *)malloc(Graph->Nv * sizeof(int));
int *path = (int *)malloc(Graph->Nv * sizeof(int));
Dijkstra(Graph, dist, path, cost, S);
printf("%d %d", dist[D], cost[D]);
return 0;
}