07-图6 旅游规划

有了一张自驾旅游路线图,你会知道城市间的高速公路长度、以及该公路要收取的过路费。现在需要你写一个程序,帮助前来咨询的游客找一条出发地和目的地之间的最短路径。如果有若干条路径都是最短的,那么需要输出最便宜的一条路径。
输入格式:
输入说明:输入数据的第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>

#define MaxVertexNum 500    /* 最大顶点数设为100 */
#define INFINITY 65535        /* ∞设为双字节无符号整数的最大值65535*/
typedef int Vertex;         /* 用顶点下标表示顶点,为整型 */
typedef int WeightType;        /* 边的权值设为整型 */
Vertex s, e;
int dist[MaxVertexNum], path[MaxVertexNum], cost[MaxVertexNum];

/* 边的定义 */
typedef struct ENode *Edge;
struct ENode
{
    Vertex V1, V2;      /* 有向边<V1, V2> */
    WeightType Weight;  /* 权重 */
    WeightType Cost; /*收费额*/
};
        
/* 图结点的定义 */
typedef struct GNode *MGraph;
struct GNode
{
    int Nv;  /* 顶点数 */
    int Ne;  /* 边数   */
    WeightType G[MaxVertexNum][MaxVertexNum][2]; /* 邻接矩阵 */
};

MGraph BuildGraph();
void Dijkstra( MGraph Graph, int dist[], int path[], Vertex S );

int main() 
{
	MGraph G = BuildGraph();
	Dijkstra(G, dist, path, s);
	printf("%d %d", dist[e], cost[e]);
	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][0] = INFINITY;            
    return Graph; 
}
        
void InsertEdge( MGraph Graph, Edge E )
{
     Graph->G[E->V1][E->V2][0] = E->Weight;
	 Graph->G[E->V1][E->V2][1] = E->Cost;    
     Graph->G[E->V2][E->V1][0] = E->Weight;
     Graph->G[E->V2][E->V1][1] = E->Cost;
}
 
MGraph BuildGraph()
{
    MGraph Graph;
    Edge E;
    Vertex V;
    int Nv, i;    
    scanf("%d", &Nv);   /* 读入顶点个数 */
    Graph = CreateGraph(Nv); /* 初始化有Nv个顶点但没有边的图 */     
    scanf("%d", &(Graph->Ne));   /* 读入边数 */
    scanf("%d%d", &s, &e); /*出发地和目的地*/ 
    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->Cost); 
            InsertEdge( Graph, E );
        }
    } 
    return Graph;
}

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] == 0 && dist[V]<MinDist) 
		{/* 若V未被收录,且dist[V]更小 */
            MinDist = dist[V]; /* 更新最小距离 */
            MinV = V; /* 更新对应顶点 */
        }
    }
    if (MinDist < INFINITY) /* 若找到最小dist */
        return MinV; /* 返回对应的顶点下标 */
    else return -1;  /* 若这样的顶点不存在,返回错误标记 */
}
 
void Dijkstra( MGraph Graph, int dist[], int path[], Vertex S )
{
    int collected[MaxVertexNum];
    Vertex V, W;
    for ( V=0; V<Graph->Nv; V++ ) 
	{
        dist[V] = Graph->G[S][V][0];
        if ( dist[V]<INFINITY )
        {
        	path[V] = S;
            cost[V] = Graph->G[S][V][1];
		}
        else
            path[V] = -1;
        collected[V] = 0;        
    }
    /* 先将起点收入集合 */
    dist[S] = 0;
    cost[S] = 0;
    collected[S] = 1;
    while (1) 
	{/* V = 未被收录顶点中dist最小者 */
        V = FindMinDist( Graph, dist, collected );
        if ( V==-1 ) /* 若这样的V不存在 */
            break;      /* 算法结束 */
        collected[V] = 1;  /* 收录V */        
        for( W=0; W<Graph->Nv; W++ ) /* 对图中的每个顶点W */
        {/* 若W是V的邻接点并且未被收录 */
            if ( collected[W] == 0 && Graph->G[V][W][0]<INFINITY ) 
			{/* 若收录V使得dist[W]变小 */
                if ( dist[V] + Graph->G[V][W][0] < dist[W] ) 
				{
                    dist[W] = dist[V] + Graph->G[V][W][0]; /* 更新dist[W] */
                    path[W] = V; /* 更新S到W的路径 */
                    cost[W] = cost[V] + Graph->G[V][W][1];/*更新W的收费*/ 
                }
                else if( (dist[V] + Graph->G[V][W][0] == dist[W]) && (cost[V] + Graph->G[V][W][1] < cost[W]) )
				{ 
					cost[W] = cost[V] + Graph->G[V][W][1]; 
					path[W] = V; 
				}
            }
		}
    } 
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值