PAT 1030 Travel Plan

本文介绍了一种使用Dijkstra算法求解旅行者最短路径问题的方法。在给定的城市地图中,算法寻找从起点到终点的最短路径及最低费用。代码示例分别展示了两种实现方式,并通过调整错误理解为有向图的问题,最终得到正确答案。作者强调了学习和调试过程的重要性,并表示将继续努力学习以应对考研挑战。
摘要由CSDN通过智能技术生成

1030 Travel Plan

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.

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

总结:有了之前写最短路的经验,这次很快就有思路了,时间主要就是花在了调试上,因为我把它当成了有向图,只拿了18分,改成有向图后,成功AC了,还是挺满意的

思路:用二维数组g表示两个城市之间的距离,q表示两个城市之间高铁的费用(注意是无向图,因为起点和终点一开始是未知的),数组 p 记录到达 i 点的前一个点,dijkstra算法中算出最短路和到达当前点(最短路的前提下)要花的费用,因为数组 p 存的是到达当前点的前一个点,所以需要将路劲翻转一下

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

const int N=510;
int g[N][N],q[N][N];//分别表示两城市的距离和高铁的价格
int p[N],dist[N],c[N];//表示达到当前城市的前一个城市和到达当前城市的距离
bool st[N];
int n,m,s,d;

int dijkstra(int s){
    memset(dist,0x3f,sizeof dist);
    dist[s]=0;
    
    for(int i=0;i<n-1;i++){
        int t=-1;
        
        for(int j=0;j<n;j++){
            if(!st[j] && (dist[t]>dist[j] || t==-1))
                t=j;
        }
        
        for(int j=0;j<n;j++){
            if(dist[j]>dist[t]+g[t][j]){
                dist[j]=dist[t]+g[t][j];
                p[j]=t;
                c[j]=c[t]+q[t][j];
            }
            else if(dist[j]==dist[t]+g[t][j]){
                if(c[j]>c[t]+q[t][j]){
                    c[j]=c[t]+q[t][j];
                    p[j]=t;
                }
            }
        }
        st[t]=true;
    }
}

int main(){
    cin >> n >> m >> s >> d;
    
    memset(g,0x3f,sizeof g);
    
    for(int i=0;i<m;i++){
        int a,b,c,d;
        scanf("%d%d%d%d",&a,&b,&c,&d);
        g[a][b]=g[b][a]=c;
        q[a][b]=q[b][a]=d;
    }
    
    dijkstra(s);
    int w[N],index=0;
    for(int i=d;;i=p[i]){ 
        w[index++]=i;
        if(i==s)    break;
    }
    for(int i=--index;i>=0;i--) printf("%d ",w[i]);
    printf("%d %d",dist[d],c[d]);
    
    return 0;
}

大佬的代码:

这种思路可以好好的学习一下,之前那个共享单车那题也是用了这种方法,先求出最短路径,用pre vector的数组保存当前点的前一个点,然后dfs终点到起始点,算出每一条路劲从起点到终点的花费,从中取出最小花费!

#include <cstdio>
#include <algorithm>
#include <vector>
using namespace std;
int n, m, s, d;
int e[510][510], dis[510], cost[510][510];
vector<int> pre[510];
bool visit[510];
const int inf = 99999999;
vector<int> path, temppath;
int mincost = inf;
void dfs(int v) {
    temppath.push_back(v);
    if(v == s) {
        int tempcost = 0;
        for(int i = temppath.size() - 1; i > 0; i--) {
            int id = temppath[i], nextid = temppath[i-1];
            tempcost += cost[id][nextid];
        }
        if(tempcost < mincost) {
            mincost = tempcost;
            path = temppath;
        }
        temppath.pop_back();
        return ;
    }
    for(int i = 0; i < pre[v].size(); i++)
        dfs(pre[v][i]);
    temppath.pop_back();
}
int main() {
    fill(e[0], e[0] + 510 * 510, inf);
    fill(dis, dis + 510, inf);
    scanf("%d%d%d%d", &n, &m, &s, &d);
    for(int i = 0; i < m; i++) {
        int a, b;
        scanf("%d%d", &a, &b);
        scanf("%d", &e[a][b]);
        e[b][a] = e[a][b];
        scanf("%d", &cost[a][b]);
        cost[b][a] = cost[a][b];
    }
    pre[s].push_back(s);
    dis[s] = 0;
    for(int i = 0; i < n; i++) {
        int u = -1, minn = inf;
        for(int j = 0; j < n; j++) {
            if(visit[j] == false && dis[j] < minn) {
                u = j;
                minn = dis[j];
            }
        }
        if(u == -1) break;
        visit[u] = true;
        for(int v = 0; v < n; v++) {
            if(visit[v] == false && e[u][v] != inf) {
                if(dis[v] > dis[u] + e[u][v]) {
                    dis[v] = dis[u] + e[u][v];
                    pre[v].clear();
                    pre[v].push_back(u);
                } else if(dis[v] == dis[u] + e[u][v]) {
                    pre[v].push_back(u);
                }
            }
        }
    }
    dfs(d);
    for(int i = path.size() - 1; i >= 0; i--)
        printf("%d ", path[i]);
    printf("%d %d", dis[d], mincost);
    return 0;
}

 好好学习,天天向上!

我要考研!

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值