07-图6 旅游规划 (25分)
有了一张自驾旅游路线图,你会知道城市间的高速公路长度、以及该公路要收取的过路费。现在需要你写一个程序,帮助前来咨询的游客找一条出发地和目的地之间的最短路径。如果有若干条路径都是最短的,那么需要输出最便宜的一条路径。
输入格式:
输入说明:输入数据的第1行给出4个正整数NN、MM、SS、DD,其中NN(2\le N\le 5002≤N≤500)是城市的个数,顺便假设城市的编号为0~(N-1N−1);MM是高速公路的条数;SS是出发地的城市编号;DD是目的地的城市编号。随后的MM行中,每行给出一条高速公路的信息,分别是:城市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算法进行修改:
1.需要在Gnode里面加一个权重矩阵COST[][]来记录从一个点到另个点所花薪资
2.使用有权图单源最短路算法时,注意之前对每点从原点到此点i的cost[i]的初始化i,每次的和dist[i]一样的更新,然后在相同距离dist[i]时,用cost[i]进行比较和更新就好了
#include<stdio.h>
#include<stdlib.h>
#define ERROR -1
#define MaxVertexNum 550 /* 最大顶点数设为500 */
#define INFINITY 65535 /* ∞设为双字节无符号整数的最大值65535*/
typedef int Vertex; /* 用顶点下标表示顶点,为整型 */
typedef int WeightType; /* 边的权值设为整型 */
typedef char DataType; /* 顶点存储的数据类型设为字符型 */
/* 边的定义 */
typedef struct ENode *PtrToENode;
struct ENode{
Vertex V1, V2; /* 有向边<V1, V2> */
WeightType Weight; /* 权重 */
int money; //这里用来存钱
};
typedef PtrToENode Edge;
/* 图结点的定义 */
typedef struct GNode *PtrToGNode;
struct GNode{
int Nv; /* 顶点数 */
int Ne; /* 边数 */
WeightType G[MaxVertexNum][MaxVertexNum]; /* 邻接矩阵 */
int COST[MaxVertexNum][MaxVertexNum];
};
typedef PtrToGNode MGraph; /* 以邻接矩阵存储的图类型 */
int S,D;//出发城市和到达城市
int dist[MaxVertexNum],cost[MaxVertexNum]; //记录每一个点距离和花费
MGraph CreateGraph( int VertexNum );/* 初始化一个有VertexNum个顶点但没有边的图 */
void InsertEdge( MGraph Graph, Edge E );/* 插入边 <V1, V2> */
MGraph BuildGraph();//读数据,建立邻接矩阵
Vertex FindMinDist( MGraph Graph, int dist[], int collected[] );
void Dijkstra( MGraph Graph, int dist[], int path[], Vertex S );
void initialize(MGraph Graph);
int main()
{
MGraph graph;
graph = BuildGraph(); //建表
initialize(graph);//初始化
Dijkstra(graph,dist,cost,S);
printf("%d %d\n",dist[D],cost[D]);
return 0;
}
MGraph CreateGraph( int VertexNum )
{ /* 初始化一个有VertexNum个顶点但没有边的图 */
Vertex V, W;
MGraph Graph;
Graph = (MGraph)malloc(sizeof(struct GNode)); /* 建立图 */
Graph->Nv = VertexNum;
Graph->Ne = 0;
/* 初始化邻接矩阵 */
/* 注意:这里默认顶点编号从0开始,到(Graph->Nv - 1) */
for (V=0; V<Graph->Nv; V++)
for (W=0; W<Graph->Nv; W++) {
Graph->G[V][W] = INFINITY;
Graph->COST[V][W] = INFINITY;
}
return Graph;
}
void InsertEdge( MGraph Graph, Edge E )
{
/* 插入边 <V1, V2> */
Graph->G[E->V1][E->V2] = E->Weight;
/* 若是无向图,还要插入边<V2, V1> */
Graph->G[E->V2][E->V1] = E->Weight;
//插入钱
Graph->COST[E->V1][E->V2] = E->money;
Graph->COST[E->V2][E->V1] = E->money;
}
MGraph BuildGraph()
{
MGraph Graph;
Edge E;
int Nv, i;
//freopen("in1.txt","r",stdin);
scanf("%d", &Nv); /* 读入顶点个数 */
Graph = CreateGraph(Nv); /* 初始化有Nv个顶点但没有边的图 */
scanf("%d", &(Graph->Ne)); /* 读入边数 */
scanf("%d%d",&S,&D);//都出发点和目的地
if ( Graph->Ne != 0 ) { /* 如果有边 */
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->money);
/* 注意:如果权重不是整型,Weight的读入格式要改 */
InsertEdge( Graph, E );
}
}
return Graph;
}
void initialize(MGraph Graph)
{
for (int i=0; i<MaxVertexNum; i++)
{
dist[i] = INFINITY;
cost[i] = INFINITY;
}
for (int i=0; i<Graph->Nv; i++) //!!!!!很重要,一定要在这里初始化
{
cost[i] = Graph->COST[S][i];
}
}
/* 邻接矩阵存储 - 有权图的单源最短路算法 */
Vertex FindMinDist( MGraph Graph, int dist[], 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; /* 若这样的顶点不存在,返回错误标记 */
}
void Dijkstra( MGraph Graph, int dist[], int cost[], Vertex S)
{
int collected[MaxVertexNum];
Vertex V, W;
/* 初始化:此处默认邻接矩阵中不存在的边用INFINITY表示 */
for ( V=0; V<Graph->Nv; V++ ) {
dist[V] = Graph->G[S][V];
collected[V] = false;
}
/* 先将起点收入集合 */
dist[S] = 0; cost[S] = 0;
collected[S] = true;
while (1)
{
/* V = 未被收录顶点中dist最小者 */
V = FindMinDist( Graph, dist, 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 )
{
/* 若收录V使得dist[W]变小 */
if ( dist[V]+Graph->G[V][W] < dist[W] ) {
dist[W] = dist[V]+Graph->G[V][W]; /* 更新dist[W] */
cost[W] = cost[V]+Graph->COST[V][W];//更新花销
}
else if(dist[V]+Graph->G[V][W] == dist[W] && cost[V]+Graph->COST[V][W]<cost[W])
{//相同距离选择更短花销
cost[W] = cost[V]+Graph->COST[V][W];
}
}
}
} /* while结束*/
}