有了一张自驾旅游路线图,你会知道城市间的高速公路长度、以及该公路要收取的过路费。现在需要你写一个程序,帮助前来咨询的游客找一条出发地和目的地之间的最短路径。如果有若干条路径都是最短的,那么需要输出最便宜的一条路径。
输入格式:
输入说明:输入数据的第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
Floyd算法:
在Floyd算法中,我们通常只考虑一种度量(例如,距离或成本),并使用这种度量来更新所有的路径。如果想同时考虑两种度量,可能需要使用其他的算法。
显然,我们是不能用Floyd算法解决的,所以这里使用Dijkstra算法
(试过Floyd算法了,因为在Floyd算法代码中,试图在同一次迭代中更新距离和成本,这可能会导致一些问题。)
#include <iostream>
typedef int VertexType;
#define INFINTY 32767
#define MVNUM 500
using namespace std;
typedef struct ArcType
{
int length;
int cost;
}ArcType;
typedef struct MGraph{
VertexType vexs[MVNUM];
ArcType arcs[MVNUM][MVNUM];
int vexnum,arcnum;
}MGraph;
void inputMGraph(MGraph &G,int _vexnum,int _arcnum){
G.vexnum=_vexnum;
G.arcnum=_arcnum;
int i,j;
for(i=0;i<MVNUM;i++){
for(j=0;j<MVNUM;j++){
G.arcs[i][j].length=INFINTY;
G.arcs[i][j].cost=INFINTY;
}
}
int a,b,abcost,ablength;
for(i=0;i<G.arcnum;i++){
cin>>a>>b>>ablength>>abcost;
G.arcs[a][b].cost=abcost;
G.arcs[a][b].length=ablength;
}
}
void Floyd(MGraph G,int sta,int des){
int C[G.vexnum][G.vexnum];
int D[G.vexnum][G.vexnum];
int i,j,k;
for(i=0;i<G.vexnum;i++){
for(j=0;j<G.vexnum;j++){
C[i][j]=G.arcs[i][j].cost;
D[i][j]=G.arcs[i][j].length;
}
}
for(k=0;k<G.vexnum;k++){
for(i=0;i<G.vexnum;i++){
for(j=0;j<G.vexnum;j++){
if((D[i][k]+D[k][j]<D[i][j])||(C[i][k]+C[k][j]<C[i][j])&&(D[i][k]+D[k][j]==D[i][j])){
D[i][j]=D[i][k]+D[k][j];
C[i][j]=C[i][k]+C[k][j];
}
}
}
}
cout<<D[sta][des]<<" "<<C[sta][des];
}
int main()
{
int n,m,s,d;
cin>>n>>m>>s>>d;
MGraph G;
inputMGraph(G,n,m);
Floyd(G,s,d);
return 0;
}
(错误示范)
(使用Dijkstra算法就对了)
#include <iostream>
typedef int VertexType;
#define INFINTY 32767
#define MVNUM 500
using namespace std;
typedef struct ArcType
{
int length;
int cost;
}ArcType;//边的属性:长度和过路费
typedef struct MGraph{
VertexType vexs[MVNUM];
ArcType arcs[MVNUM][MVNUM];
int vexnum,arcnum;
}MGraph;//图的邻接矩阵定义
void inputMGraph(MGraph &G,int _vexnum,int _arcnum){
G.vexnum=_vexnum;
G.arcnum=_arcnum;
int i,j;
for(i=0;i<MVNUM;i++){
for(j=0;j<MVNUM;j++){
G.arcs[i][j].length=INFINTY;
G.arcs[i][j].cost=INFINTY;
}
G.arcs[i][i].cost=G.arcs[i][i].length=0;
}//初始化图
int a,b,abcost,ablength;
for(i=0;i<G.arcnum;i++){
cin>>a>>b>>ablength>>abcost;
G.arcs[a][b].cost=G.arcs[b][a].cost=abcost;
G.arcs[a][b].length=G.arcs[b][a].length=ablength;
}//输入图的若干边
}//图的输入
void Dijkstra(MGraph G,int sta,int des){
bool finished[G.vexnum];//标记是否已访问的数组
int i,j,k;//循环变量
int D[G.vexnum];//长度
int C[G.vexnum];//过路费
finished[sta]=true;
D[sta]=0;
C[sta]=0;
for(i=0;i<G.vexnum;i++){
if(i!=sta){
finished[i]=false;
D[i]=G.arcs[sta][i].length;
C[i]=G.arcs[sta][i].cost;
}
}
for(i=0;i<G.vexnum;i++){
int min=INFINTY;//初始化最小长度
int mincost=INFINTY;//初始化最小过路费
for(j=0;j<G.vexnum;++j){
if(!finished[j]&&(D[j]<min||(D[j]==min&&C[j]<mincost))){
k=j;
min=D[j];
mincost=C[j];
}
}//寻找未访问的点中长度最小或当长度相等时距离最小的点
finished[k]=true;
for(j=0;j<G.vexnum;j++){
if(!finished[j]&&(((min+G.arcs[k][j].length)<D[j])||((min+G.arcs[k][j].length)==D[j]&&(mincost+G.arcs[k][j].cost)<C[j]))){
D[j]=min+G.arcs[k][j].length;
C[j]=mincost+G.arcs[k][j].cost;
}
}//根据上面找到的点更新其他点的数据
}
cout<<D[des]<<" "<<C[des];//输出到终点的数据
}//Dijkstra算法
int main()
{
int n,m,s,d;
cin>>n>>m>>s>>d;
MGraph G;
inputMGraph(G,n,m);//输入图
Dijkstra(G,s,d);//利用Dijkstra算法求解
return 0;
}