PAT 1030 Travel Plan 链式前向星

博客详细介绍了如何使用链式前向星数据结构结合迪杰斯特拉算法解决旅行者地图的最短路径问题。内容包括输入输出规格,题解思路,以及关键的代码实现,强调了在距离相同时需要比较花费来选择最优路径。
摘要由CSDN通过智能技术生成

题目
A traveler’s map gives the distances between cities along the highways, together with the cost of each highway. Now you are supposed to write a program to help a traveler to decide the shortest path between his/her starting city and the destination. If such a shortest path is not unique, you are supposed to output the one with the minimum cost, which is guaranteed to be unique.

Input Specification:
Each input file contains one test case. Each case starts with a line containing 4 positive integers N, M, S, and D, where N (≤500) is the number of cities (and hence the cities are numbered from 0 to N−1); M is the number of highways; S and D are the starting and the destination cities, respectively. Then M lines follow, each provides the information of a highway, in the format:

City1 City2 Distance Cost

where the numbers are all integers no more than 500, and are separated by a space.

Output Specification:
For each test case, print in one line the cities along the shortest path from the starting point to the destination, followed by the total distance and the total cost of the path. The numbers must be separated by a space and there must be no extra space at the end of output.

题解思路:

首先我们需要存储所有路的distance和cost(也就是边edge),此处我们使用链式前向星。之后是迪杰斯特拉算法求解最短路。
所以我们需要几个标志状态的数组:
int distances[500]; //当前到达结点的最短距离
int costs[500]; //当前到达该结点的最小花费
int parents[500]; //记录哪个节点结点到该节点距离最短(距离一样则cost最小)
bool visited[500]; //记录某结点 是否已经被确定为距离最短(即没有可能发现其他路到他的距离更短)
但需要注意:由于有可能出现有两条路到达某一结点的距离相同,所以在距离相同时,需要比较两条路花费大小(题目已经告诉不可能出现两条路长度和花费都一样),然后用距离小的父节点,距离,花费去更新上面几个数组即可。
每一次更新之后,遍历distances数组,找到值最小且visited为false的一个(或几个)结点,将其visited置为true
循环进行上述过程直至visited[dst]为true
然后从dst开始逐个找出父节点确定路径,输出即可。

代码
#include <iostream>
#include <stdio.h>
#include<vector>
#include<stack>
using namespace std;
#define INF 1e8
struct Edge
{
    int to;
    int next;
    int weight;
    int cost;
};
Edge edges[500000];  //500*500*2
int heads[500];
int tot = 0;

void add_edge(int u,int to,int weight,int cost)
{
    edges[tot].to = to;
    edges[tot].weight = weight;
    edges[tot].cost = cost;
    edges[tot].next = heads[u];
    heads[u] = tot;
    tot++;
}

int distances[500];
int costs[500];
int parents[500];
bool visited[500];

int main()
{
    int N,M,start,dst;
    scanf("%d %d %d %d",&N,&M,&start,&dst);
    for(int i = 0;i < N;i++)
    {
        heads[i] = -1;
        distances[i] = costs[i] = INF;
        visited[i] = false;
    }
    int city1,city2,weight,cost;
    for(int i = 0;i < M;i++)
    {
        scanf("%d %d %d %d",&city1,&city2,&weight,&cost);
        add_edge(city1,city2,weight,cost);
        add_edge(city2,city1,weight,cost);
    }
    vector<int> last_ensure;  //存储迪杰斯特拉算法每一步所确定的最短距离的点,由于可能不止一个,所以用vector
    last_ensure.push_back(start);   //首先起始点肯定是最短的
    distances[start] = 0;
    costs[start] = 0;
    visited[start] = true;
    vector<int>::iterator upptr;
    while(visited[dst] != true)  //
    {
        for(upptr = last_ensure.begin();upptr!=last_ensure.end();upptr++)
        {    //cout<<"up"<<up<<endl;
            int up = *upptr;
            for(int i = heads[up];i != -1;i = edges[i].next)
            {
                int d = edges[i].to;
                if(visited[d])
                    continue;
                if(distances[up] + edges[i].weight < distances[d])  //路径长度短,则更新
                {
                    distances[d] = distances[up] + edges[i].weight;
                    parents[d] = up;
                    costs[d] = costs[up] + edges[i].cost;
                }
                if(distances[up] + edges[i].weight == distances[d])  //路径长度一样
                {
                    if(costs[up] + edges[i].cost < costs[d])   //但cost更低,则更新
                    {
                        parents[d] = up;
                        costs[d] = costs[up] + edges[i].cost;
                    }
                }
            }
        }
        int tmpmax = INF;
        for(int i = 0;i<N;i++)  //求出更新之后可到达的距离最短的点
        {
            if(visited[i] == true)
                continue;
            if(distances[i] < tmpmax)
            {
                last_ensure.clear();
                last_ensure.push_back(i);
                tmpmax = distances[i];
            }
            else if(distances[i] == tmpmax)
            {
                last_ensure.push_back(i);  //同为最小,则一同记录下来
            }
        }
        for(upptr = last_ensure.begin();upptr!=last_ensure.end();upptr++)
        {
            visited[*upptr] = true;
           // cout<<"up: "<<up<<endl;
        }
    }
    stack<int> stk;
    stk.push(dst);
    while(stk.top() != start)
    {
        stk.push(parents[stk.top()]);
    }
    while(!stk.empty())
    {
        printf("%d ",stk.top());
        stk.pop();
    }
    printf("%d ",distances[dst]);
    printf("%d",costs[dst]);
    return 0;
}

//    for(int i = 0;i < N;i++)
//        printf("%d\n",costs[i]);

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值