如果有若干条路径都是最短的,那么需要输出最便宜的一条路径。
输入格式:
输入说明:输入数据的第1行给出4个正整数N、M、S、D,其中N(2≤N≤500)是城市的个数,顺便假设城市的编号为0~(N−1);M是高速公路的条数;S是出发地的城市编号;D是目的地的城市编号。随后的M行中,每行给出一条高速公路的信息,分别是:城市1、城市2、高速公路长度、收费额,中间用空格分开,数字均为整数且不超过500。输入保证解的存在。
输出格式:
在一行里输出路径的长度和收费总额,数字间以空格分隔,输出结尾不能有多余空格。
#include<iostream>
#include<cstdlib>
#include<string>
#include<cmath>
using namespace std;
#define MaxVertexNum 500
#define INFINITY 65535
typedef int Vertex;
struct DistCost {
int Weight;
int Cost;
};
struct GNode {
int Nv;
int Ne;
struct DistCost G[MaxVertexNum][MaxVertexNum];
};
typedef struct GNode* MGraph;
struct ENode {
int V1, V2;
int Weight;
int Cost;
};
typedef struct ENode* Edge;
MGraph CreateGraph(int VertexNum);
void InsertEdge(MGraph Graph, Edge E);
void Dijkstra(MGraph Graph, Vertex S, Vertex D);
int main()
{
int S, D;
MGraph Graph;
Edge E;
Vertex V;
int N, i;
cin >> N;
Graph = CreateGraph(N);
cin >> Graph->Ne;
cin >> S >> D;
if (Graph->Ne != 0) {
E = (Edge)malloc(sizeof(struct ENode));
for (i = 0; i < Graph->Ne; ++i) {
cin >> E->V1 >> E->V2 >> E->Weight >> E->Cost;
InsertEdge(Graph, E);
}
}
Dijkstra(Graph, S, D);
return 0;
}
MGraph CreateGraph(int VertexNum)
{
MGraph Graph = (MGraph)malloc(sizeof(struct GNode));
Graph->Ne = 0;
Graph->Nv = VertexNum;
for (int i = 0; i < Graph->Nv; ++i) {
for (int j = 0; j < Graph->Nv; ++j) {
Graph->G[i][j].Weight = INFINITY;
Graph->G[i][j].Cost = INFINITY;
}
}
return Graph;
}
//插入每条边的长度和收费额
void InsertEdge(MGraph Graph, Edge E)
{
Graph->G[E->V1][E->V2].Weight = E->Weight;
Graph->G[E->V2][E->V1].Weight = E->Weight;
Graph->G[E->V1][E->V2].Cost = E->Cost;
Graph->G[E->V2][E->V1].Cost = E->Cost;
}
int FindMinDist(MGraph Graph, int dist[], int collected[])
{
int MinV, V;
int MinDist = INFINITY;
//访问每个节点找到距离最短路径最短的节点
for (V = 0; V < Graph->Nv; ++V) {
if (!collected[V] && dist[V] < MinDist) {
MinDist = dist[V];
MinV = V;
}
}
if (MinDist < INFINITY)
return MinV;
else return -1; //若无未访问节点,或不连通,返回-1
}
void Dijkstra(MGraph Graph, Vertex S, Vertex D)
{
int collected[MaxVertexNum]; //记录是否已访问
//dist记录邻接点到最短路径的距离, path记录邻接点的父节点(上一个节点)
int dist[MaxVertexNum], path[MaxVertexNum], cost[MaxVertexNum];
Vertex V, W;
for (V = 0; V < Graph->Nv; ++V) {
dist[V] = Graph->G[S][V].Weight;
cost[V] = Graph->G[S][V].Cost;
if (dist[V] < INFINITY) { //若是源点邻接点则父节点初始化为源点
path[V] = S;
}
else {
path[V] = -1; //否则为-1
}
collected[V] = false;
}
dist[S] = 0; //源点
collected[S] = true;
while (1) {
//找到未访问的距离最短路径最短的节点.
V = FindMinDist(Graph, dist, collected);
if (V == -1) {
break; //没有剩余节点或不连通退出循环
}
collected[V] = true; //记录为已访问
for (W = 0; W < Graph->Nv; ++W) {
//访问每个邻接点
if (!collected[W] && Graph->G[V][W].Weight < INFINITY) {
//如果最短路径+VW间距离<W到源点距离
if (dist[V] + Graph->G[V][W].Weight < dist[W]) {
//W到源点的最短距离为最短路径+VW间距离
dist[W] = dist[V] + Graph->G[V][W].Weight;
//前节点为V
path[W] = V;
//过路费也同样设为最短路径过路费
cost[W] = cost[V] + Graph->G[V][W].Cost;
}
//如果路径长度相等,选择过路费低的
else if (dist[W] == dist[V] + Graph->G[V][W].Weight
&& cost[W] > cost[V] + Graph->G[V][W].Cost) {
//只改变前节点和过路费,因为长度相等没有必要改变.
cost[W] = cost[V] + Graph->G[V][W].Cost;
path[W] = V;
}
}
}
}
cout << dist[D] << " " << cost[D] << endl;
}