PTA旅游规划

PTA旅游规划问题 C语言实现

数据结构:图
实现算法:Dijkstra算法

main()函数框架:
{
根据输入建立图;
Dijkstra函数;
输出;
}

int main()
{
	MGraph G=BuildGraph();
	Dijkstra( G, dist, path,cost,S );
	printf("%d %d",dist[D],cost[D]);
	
}

Dijkstra函数需要注意的就是本题中的边有两个权重——距离和花费,所以在收录一个新节点的时候,要同时更新两个相应值。

void Dijkstra( MGraph Graph, int dist[], int path[], int cost[],Vertex S )
{
    int collected[MaxVertexNum];
    Vertex V, W;
 
    /* 初始化:此处默认邻接矩阵中不存在的边用INFINITY表示 */
    for ( V=0; V<Graph->Nv; V++ ) {
        dist[V] = Graph->G[S][V];
        cost[V]=Graph->M[S][V];
        if ( dist[V]<INFINITY )
            path[V] = S;
        else
            path[V] = -1;
        collected[V] = false;
    }
    /* 先将起点收入集合 */
    dist[S] = 0;
    cost[S]=0;
    collected[S] = true;
 
    while (1) {
        /* V = 未被收录顶点中dist最小者 */
        V = FindMinDist( Graph, dist, cost,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 ) {
                //if ( Graph->G[V][W]<0 ) /* 若有负边 */
                   // return false; /* 不能正确解决,返回错误标记 */
                /* 若收录V使得dist[W]变小 */
                if ( dist[V]+Graph->G[V][W] < dist[W] ) {
                    dist[W] = dist[V]+Graph->G[V][W]; /* 更新dist[W] */
                    path[W] = V; /* 更新S到W的路径 */
                    cost[W] = cost[V]+Graph->M[V][W];/*更新cost[W]*/
                }
                /*若找到另一个距离相等但费用更少的点*/
                else if(dist[V]+Graph->G[V][W]==dist[W] && cost[V]+Graph->M[V][W]<cost[W]){
                	cost[W]=cost[V]+Graph->M[V][W];
                	path[W]=V;
				}
            }
    } /* while结束*/
}

以下为程序完整代码:

#include<stdio.h>
#include<stdlib.h>

#define MaxVertexNum 500  //最大顶点数设为500 
#define INFINITY 65535  //∞设为双字节无符号整数的最大值65535 
#define ERROR -1
typedef int Vertex;  //用顶点下标表示顶点,为整型 
typedef int WeightType;  //边的权值设为整型 

//边的定义
typedef struct ENode *PtrToENode;
struct ENode
{
	Vertex V1,V2;//有向边<V1,V2> 
	WeightType roadlength;//权重 高速公路长度 
	WeightType money;//收费额 
};
typedef PtrToENode Edge;
 
 //图节点的定义
typedef struct GNode *PtrToGNode;
struct GNode
{
	int Nv;//顶点数
	int Ne;//边数
	WeightType G[MaxVertexNum][MaxVertexNum];//邻接矩阵 存边长度 
	WeightType M[MaxVertexNum][MaxVertexNum];//存边收费额 
	//DataType Data[MaxVertexNum];//存顶点的数据
	//注:顶点无数据时,Data可以不用出现 
};
typedef PtrToGNode MGraph;//以邻接矩阵存储的图类型

MGraph CreateGraph(int VertexNum);
void InsertEdge(MGraph Graph,Edge E);
Vertex FindMinDist( MGraph Graph, int dist[], int cost[],int collected[] );
void Dijkstra( MGraph Graph, int dist[], int path[], int cost[],Vertex S );

int main()
{
	int dist[MaxVertexNum];//存储距离 
	int cost[MaxVertexNum];//存储收费额 
	int path[MaxVertexNum];//存储路径 
	//Mgraph G=BuildGraph(),本来构造图的过程可以用函数实现,但是读取输入值的过程中S和D需要作为全局变量,所以索性就直接在主函数里实现 
	MGraph G;
	Edge E;
	Vertex S,D;
	//Vertex V;
	int Nv,i;
	
	scanf("%d",&Nv);//读入顶点个数
	G=CreateGraph(Nv);
	
	scanf("%d",&G->Ne);//读入边数
	scanf("%d %d",&S,&D);//读入出发地和目的地 
	if(G->Ne!=0)//如果有边 
	{
		E=(Edge)malloc(sizeof(struct ENode));//建立边节点
		for(i=0;i<G->Ne;i++)//读入边,格式为“起点 终点 长度 收费额”,插入邻接矩阵 
		{
			scanf("%d %d %d %d",&E->V1,&E->V2,&E->roadlength,&E->money);
			//E->V1--;E->V2--;//起点编号从0开始 
			InsertEdge(G,E);
		}
	}
	Dijkstra( G, dist, path,cost,S );
	printf("%d %d",dist[D],cost[D]);
	
}

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->M[V][W]=INFINITY;
		}
	}
	return Graph;
}

void InsertEdge(MGraph Graph,Edge E)
{
	//插入边<V1,V1>
	Graph->G[E->V1][E->V2]=E->roadlength;
	Graph->M[E->V1][E->V2]=E->money;
	//若是无向图,还要插入<V2,V1> 
	Graph->G[E->V2][E->V1]=E->roadlength;
	Graph->M[E->V2][E->V1]=E->money;
}


Vertex FindMinDist( MGraph Graph, int dist[], int cost[],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 path[], int cost[],Vertex S )
{
    int collected[MaxVertexNum];
    Vertex V, W;
 
    /* 初始化:此处默认邻接矩阵中不存在的边用INFINITY表示 */
    for ( V=0; V<Graph->Nv; V++ ) {
        dist[V] = Graph->G[S][V];
        cost[V]=Graph->M[S][V];
        if ( dist[V]<INFINITY )
            path[V] = S;
        else
            path[V] = -1;
        collected[V] = false;
    }
    /* 先将起点收入集合 */
    dist[S] = 0;
    cost[S]=0;
    collected[S] = true;
 
    while (1) {
        /* V = 未被收录顶点中dist最小者 */
        V = FindMinDist( Graph, dist, cost,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 ) {
                //if ( Graph->G[V][W]<0 ) /* 若有负边 */
                   // return false; /* 不能正确解决,返回错误标记 */
                /* 若收录V使得dist[W]变小 */
                if ( dist[V]+Graph->G[V][W] < dist[W] ) {
                    dist[W] = dist[V]+Graph->G[V][W]; /* 更新dist[W] */
                    path[W] = V; /* 更新S到W的路径 */
                    cost[W] = cost[V]+Graph->M[V][W];/*更新cost[W]*/
                }
                /*若找到另一个距离相等但费用更少的点*/
                else if(dist[V]+Graph->G[V][W]==dist[W] && cost[V]+Graph->M[V][W]<cost[W]){
                	cost[W]=cost[V]+Graph->M[V][W];
                	path[W]=V;
				}
            }
    } /* while结束*/
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值