PAT 1030 Travel Plan (30 分)

题目描述

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.

输入

Each input file contains one test case. Each case starts with a line containing 4 positive integers N N N, M M M, S S S, and D D D, where N ( ≤ 500 ) N (≤500) N(500) is the number of cities (and hence the cities are numbered from 0 0 0 to N − 1 N−1 N1); M M M is the number of highways; S S S and D D D are the starting and the destination cities, respectively. Then M M 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 500 500, and are separated by a space.

输出

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.

Sample Input:

4 5 0 3
0 1 1 20
1 3 2 30
0 3 4 10
0 2 2 20
2 3 1 20

Sample Output:

0 2 3 3 40

思路

这道题没有过多的要素需要考虑,仅需要考虑在记录路径的同时,将该路径的花费一起进行记录,在到达终点时进行比较,出现以下两种情况需要进行更新:

  • 目前路径的距离小于最短路径距离
  • 目前路径的距离等于最短路径距离,但目前路径的花费更少

采用DFS算法,首先进行是否到达终点的判断,如果到达终点,则进行比较和更新。
如果未达到终点但距离已经大于等于最短路径的距离,直接return不考虑(剪枝)。
如果既未到达终点,同时目前的距离也小于最短路径的距离,则继续搜索其它可以到达但还未进行探索的城市。
需要注意的一点就是,每次进行DFS之后需要将状态还原!!!
思路特别简单,下面附上代码以及部分注释。

代码

#include <iostream>
#include <cstring>
#include <vector>
using namespace std;

#define inf 0x3f3f3f3f

/*为了方便而定义全局变量,同时也可以减少系统栈的占用,如果定义为局部变量则需在DFS函数中加入对应的参数*/
int N, M, S, D;
//map记录各个城市之间的距离,考虑到N最大为500,这里将数组设置为最大可容纳505*505个
int map[505][505];
//costs记录各个城市之间旅行的耗费
int costs[505][505];
//path为最短路径,temp为每次到达终点所得到的路径,将temp与path进行比较以更新path
vector<int> path, temp;
//mindis代表最短路径距离,mincos代表最低花费,初始都设置为无穷大
int mindis = inf, mincos = inf;
//tempdis和tempcos分别表示每次搜索到达终点时的距离长度和花费
int tempdis = 0, tempcos = 0; 
//如果城市i已经被探索过,则vis[i] = true;
bool vis[505] = {0};

void dfs(int start){
    if (start == D){
        //如果到达了目的地,则进行比较
        if(tempdis < mindis || tempdis == mindis && tempcos < mincos){
            //如果当前路径距离更短或者花费更低,则更新mindis、mincos和path并返回
            mindis = tempdis;
            mincos = tempcos;
            path = temp;
            return ;
        }
    } else {
        //未到达目的地
        if(tempdis >= mindis){
            //当前路径距离已经大于最短路径距离,则无需探索后续的城市,直接剪枝
            return;
        }else{
            //否则,遍历城市start相连接的城市
            for(int i = 0; i < N; i++){
                if(map[start][i] < inf && vis[i] == false){
                    //如果城市可以到达且未被探索过,则将该城市收入路径中,更新目前的距离和花费
                    temp.push_back(i);
                    tempdis += map[start][i];
                    tempcos += costs[start][i];
                    vis[i] = true;
                    //探索该城市
                    dfs(i);
                    //探索完将状态还原
                    temp.pop_back();
                    tempdis -= map[start][i];
                    tempcos -= costs[start][i];
                    vis[i] = false;
                }
            }
            return;
        }
    }
}

int main(void){
    /*初始将所有距离都设置为无穷大
     *memset(a, 0x3f, sizeof(a))的用法可以上网查一下,挺有用的
     *这里将每个城市到自身的距离也设置为了无穷大,不影响结果
     */
    memset(map, 0x3f, sizeof(map));
    cin>>N>>M>>S>>D;
    int a, b, distance, cost;
    for(int i = 0; i < M; i++){
        scanf("%d %d %d %d", &a, &b, &distance, &cost);
        map[a][b] = map[b][a] = distance;
        costs[a][b] = costs[b][a] = cost;
    }
    //初始将起点加入路径中,同时标明起点已经探索过
    temp.push_back(S);
    vis[S] = true;
    dfs(S);
    //输出
    for(int i = 0; i < path.size(); i++){
        printf("%d ", path[i]);
    }
    printf("%d %d\n", mindis, mincos);
    return 0;
}

结语

看到图的第一反应是使用DFS,后面AC完突然想起可以用Dijkstra,蚌埠住了觉得自己脑子好笨,上网搜了一下发现大多数博主分享的思路都是Dijkstra+DFS结合起来的方法,看了一下他们的代码发现比我自己实现的要复杂一些。同时耗时和内存占用也都差不多,故将我的写法分享出来,希望能够帮助到各位!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值