数据结构PTA 案例6-1.5 旅游规划

案例6-1.5 旅游规划

题目

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

输入格式:
输入说明:输入数据的第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

解法

思路

  1. 这是一道基本的单源最短路径问题。注意单源最短路径问题中,源点到某一特定点的最短路径的时间复杂度源点到所有点的最短路径的时间复杂度相同
  2. 这里涉及到的操作有:初始化一个空图(用邻接矩阵实现,动态数组)、插入边、构建图,dijkstra算法。初始化空图时,权重的数据类型应该是一个结构体,既能存储长度距离,又能存储费用。
  3. 在dijkstra算法中,寻找下一顶点V的时候,仍然按照最小距离寻找,只不过在更新dist、cost、path这三个数组的时候,需要注意,
    距离的优先级是最高的,如果dist[W]因为添加V变小了,那么一定要进行更新。
    其次是距离相等的时候,cost[W]变小了就更新,否则cost不更新.

注意:
对于源点,collected[S]一定要赋值为1,而dist[S]不一定要赋值为0,因为collected[S]=0会导致后面在FindMinDist函数中也不会用到dist[S]。cost[S]和dist[S]同理

实现

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

#define INFINITY 100000
#define ERROR -2

typedef int Vertex;

struct Weight
{
    int Length;
    int Fee;
};
typedef struct Weight WeightType;

typedef struct GNode *PtrToGNode;
typedef PtrToGNode MGraph;
struct GNode
{
    int Nv;
    int Ne;
    WeightType **G;
};

typedef struct ENode *PtrToENode;
typedef PtrToENode Edge;
struct ENode
{
    Vertex V1, V2;
    WeightType W;
};

MGraph CreateGraph(int VertexNum)
{
    MGraph Graph = (MGraph)malloc(sizeof(struct GNode));
    Graph->Nv = VertexNum;
    //allocate the room
    int i,j;
    Graph->G = (WeightType **)malloc(Graph->Nv*sizeof(WeightType *));
    for(i=0; i<Graph->Nv; i++)
    {
        Graph->G[i] = (WeightType *)malloc(Graph->Nv*sizeof(WeightType));
    }
    //initial
    for(i=0; i<Graph->Nv; i++)
        for(j=0; j<Graph->Nv; j++)
        {
            Graph->G[i][j].Length = INFINITY;
            Graph->G[i][j].Fee = INFINITY;
        }


    return Graph;
}

void InsertEdge(MGraph Graph, Edge E)
{
    Graph->G[E->V1][E->V2] = E->W;  //can assign it directly
    Graph->G[E->V2][E->V1] = E->W;
}

MGraph BuildGraph(int VertexNum, int EdgeNum)
{
    MGraph Graph = CreateGraph(VertexNum);
    Graph->Ne = EdgeNum;

    Edge E = (Edge)malloc(sizeof(struct ENode));
    int i;
    for(i=0; i<Graph->Ne; i++)
    {
        scanf("%d %d %d %d", &E->V1, &E->V2, &E->W.Length, &E->W.Fee);
        InsertEdge(Graph, E);
    }

    return Graph;
}

Vertex FindMinDist(MGraph Graph, int dist[], int collected[])
{
    Vertex MinV, V;
    int MinDist = INFINITY;
    for(V=0; V<Graph->Nv; V++)
    {
        if(collected[V] == 0 && dist[V]<MinDist)
        {
            MinDist = dist[V];
            MinV = V;
        }
    }

    if(MinDist == INFINITY)
        return ERROR;

    return MinV;

}

bool Dijkstra(MGraph Graph, int dist[], int path[], int cost[], Vertex S)
{
    int *collected = (int *)malloc(Graph->Nv*sizeof(int));

    Vertex V;
    for(V=0; V<Graph->Nv; V++)
    {
        dist[V] = Graph->G[S][V].Length;
        cost[V] = Graph->G[S][V].Fee;
        if(dist[V] < INFINITY)
            path[V] = S;
        else
            path[V] = -1;
        collected[V] = 0;
    }
   // dist[S] = 0;
    collected[S] = 1;


    Vertex W;
    while(1)
    {
        V = FindMinDist(Graph, dist, collected);
        if(V == ERROR)
            break;
        collected[V] = 1;
        for(W=0; W<Graph->Nv; W++)
        {
            if(collected[W] == 0 && Graph->G[W][V].Length < INFINITY)
            {
                if(Graph->G[W][V].Length<0)
                    return false;
                if(dist[W] > dist[V]+Graph->G[W][V].Length || (dist[W] == dist[V]+Graph->G[W][V].Length && cost[W] > cost[V]+Graph->G[W][V].Fee))
                {
                    dist[W] = dist[V]+Graph->G[W][V].Length;
                    cost[W] = cost[V]+Graph->G[W][V].Fee;
                    path[W] = V;
                }
            }
        }
    }
    return true;
}

int main()
{
    int N,M;
    Vertex S,D;
    scanf("%d %d %d %d", &N, &M, &S, &D);
    MGraph Graph = BuildGraph(N, M);

    int *dist = (int *)malloc(Graph->Nv * sizeof(int));
    int *cost = (int *)malloc(Graph->Nv * sizeof(int));
    int *path = (int *)malloc(Graph->Nv * sizeof(int));

    Dijkstra(Graph, dist, path, cost, S);
    printf("%d %d", dist[D], cost[D]);

    return 0;
}

  • 1
    点赞
  • 22
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我可以为你解答这个问题。 根据带虚结点的先序序列创建二叉树的步骤如下: 1. 读入先序序列,包括虚结点和实结点。如果当前读入的结点是虚结点,则不需要创建对应的结点,直接跳过。 2. 如果当前读入的结点是实结点,则创建一个新的二叉树结点,将其值设置为当前读入的值。 3. 判断当前结点的父节点是否存在,如果存在,则将当前结点作为父节点的左子节点或右子节点,具体是左子节点还是右子节点取决于先序序列中下一个结点的类型。 4. 将当前结点加入到一个栈中保存,以便后续处理。 5. 如果下一个结点是虚结点,则直接跳过;如果是实结点,则重复步骤2-4,直到先序序列结束。 6. 返回根节点。 下面是一个示例代码,用于根据带虚结点的先序序列创建二叉树: ```c++ #include <iostream> #include <stack> using namespace std; struct TreeNode { int val; TreeNode* left; TreeNode* right; TreeNode(int x) : val(x), left(NULL), right(NULL) {} }; TreeNode* createTree(vector<int>& preorder) { stack<TreeNode*> st; TreeNode* root = new TreeNode(preorder[0]); st.push(root); for (int i = 1; i < preorder.size(); i++) { if (preorder[i] == -1) { continue; } TreeNode* curr = new TreeNode(preorder[i]); if (i % 2 == 1) { st.top()->left = curr; } else { st.top()->right = curr; } st.push(curr); while (!st.empty() && st.top()->right != NULL) { st.pop(); } } return root; } int main() { vector<int> preorder = {1, 2, -1, 3, 4, 5, -1, -1, -1, -1, -1}; TreeNode* root = createTree(preorder); return 0; } ``` 在这个示例代码中,我们使用了一个栈来保存当前处理到的结点,以及其祖先结点。对于每个读入的结点,我们先判断其类型(虚结点还是实结点),然后根据其在先序序列中的位置,将其作为左子节点或右子节点加入到其父节点中。如果当前结点的右子节点已经存在,说明其已经处理完毕,我们可以将其从栈中弹出。最后,我们返回根节点即可。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值