1030. Travel Plan (30)-PAT甲级真题

本文介绍了一种解决旅行者寻找从起点到终点最短路径及其最低成本的问题的方法。使用Dijkstra算法找到最短路径,并通过记录前驱节点来确定最低成本路径。

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.

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


分析:这道题是很标准的最短路径加上开销的问题,就是用Dijkstra求出最短的路径,再用一个pre保存每一个节点的前置节点,再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)
{
    if (v == s)
    {
        temppath.push_back (v);
        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;
    }
    temppath.push_back (v);
    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_s ("%d%d%d%d" , &n , &m , &s , &d);
    for (int i = 0; i < m; i++)
    {
        int a , b;
        scanf_s ("%d%d" , &a , &b);
        scanf_s ("%d" , &e[a][b]);
        e[b][a] = e[a][b];
        scanf_s ("%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 = 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;
}
构建进程终止异常: D:\Xuexi\java8\jdk-1.8\bin\java.exe -Xmx700m -Djava.awt.headless=true "-Djna.boot.library.path=D:\Xuexi\IntelliJ IDEA 2024.1/lib/jna/amd64" -Djna.nosys=true -Djna.noclasspath=true --add-opens jdk.compiler/com.sun.tools.javac.api=ALL-UNNAMED --add-opens jdk.compiler/com.sun.tools.javac.util=ALL-UNNAMED --add-opens jdk.compiler/com.sun.tools.javac.code=ALL-UNNAMED --add-opens jdk.compiler/com.sun.tools.javac.comp=ALL-UNNAMED --add-opens jdk.compiler/com.sun.tools.javac.file=ALL-UNNAMED --add-opens jdk.compiler/com.sun.tools.javac.main=ALL-UNNAMED --add-opens jdk.compiler/com.sun.tools.javac.model=ALL-UNNAMED --add-opens jdk.compiler/com.sun.tools.javac.parser=ALL-UNNAMED --add-opens jdk.compiler/com.sun.tools.javac.processing=ALL-UNNAMED --add-opens jdk.compiler/com.sun.tools.javac.tree=ALL-UNNAMED --add-opens jdk.compiler/com.sun.tools.javac.jvm=ALL-UNNAMED -Dexternal.project.config=C:\Users\19006\AppData\Local\JetBrains\IntelliJIdea2024.1\projects\online-travel-api.4d3c8dd1\external_build_system -Dcompile.parallel=false -Drebuild.on.dependency.change=true -Didea.IntToIntBtree.page.size=32768 -Djdt.compiler.useSingleThread=true -Daether.connector.resumeDownloads=false -Dio.netty.initialSeedUniquifier=-7882072350026939274 -Dfile.encoding=GBK -Duser.language=zh -Duser.country=CN -Didea.paths.selector=IntelliJIdea2024.1 -Djps.language.bundle=C:/Users/19006/AppData/Roaming/JetBrains/IntelliJIdea2024.1/plugins/zh.241.271/lib/zh.241.271.jar "-Didea.home.path=D:/Xuexi/IntelliJ IDEA 2024.1" -Didea.config.path=C:/Users/19006/AppData/Roaming/JetBrains/IntelliJIdea2024.1 -Didea.plugins.path=C:/Users/19006/AppData/Roaming/JetBrains/IntelliJIdea2024.1/plugins -Djps.log.dir=C:/Users/19006/AppData/Local/JetBrains/IntelliJIdea2024.1/log/build-log "-Djps.fallback.jdk.home=D:/Xuexi/IntelliJ IDEA 2024.1/jbr" -Djps.fallback.jdk.version=17.0.10 -Dio.netty.noUnsafe=true -Djava.io.tmpdir=C:/Users/19006/AppData/Local/JetBrains/IntelliJIdea2024.1/compile-server/online-travel-api_6
最新发布
08-06
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值