[PAT]1030 Travel Plan——Dijkstra+DFS

本文介绍了一种解决旅行者寻找从起点到终点最短路径问题的算法,使用Dijkstra算法进行路径查找,同时考虑距离和成本,确保找到的路径不仅最短,而且成本最低。文章提供了详细的代码实现,包括初始化图、Dijkstra算法实现和路径回溯打印。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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:

#include<cstdio>
#include<cmath>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
const int maxv=510;
const int inf=100000000;

int g[maxv][maxv],d[maxv],c[maxv],cost[maxv][maxv],pre[maxv];
bool vis[maxv]={false};
int n,m,s,de;

void dijkstra(int s)
{
    fill(d,d+maxv,inf);
    fill(c,c+maxv,inf);
    for(int i=0;i<n;i++) pre[i]=i; //学习0:赋并查集初值

    d[s]=0;
    c[s]=0;
    for(int i=0;i<n;i++)
    {
        int u=-1,mm=inf;
        for(int j=0;j<n;j++)
        {
            if(vis[j]==false && d[u]<mm)
            {
                u=j;
                mm=d[j];
            }
        }
        if(u==-1) return;
        vis[u]=true;
        for(int j=0;j<n;j++)
        {
            if(vis[j]==false && g[u][j]<inf)
            {
                if(d[u]+g[u][j]<d[j])
                {
                    d[j]=d[u]+g[u][j];
                    c[j]=c[u]+cost[u][j];
                    pre[j]=u;
                }
                else if(d[u]+g[u][j]==d[j]){
                        //学习一:是当有更优时,因为题中说了路径唯一!
                    if(c[u]+cost[u][j]<c[j]) {c[j]=c[u]+cost[u][j];pre[j]=u;}
                }
            }
        }
    }

}
void dfs(int v)//学习二:打印路径
{
    if(v==s)
    {
        printf("%d ",v);
        return;
    }
    dfs(pre[v]);
    printf("%d ",v);
}


int main()
{
   scanf("%d %d %d %d",&n,&m,&s,&de);
    fill(g[0],g[0]+maxv*maxv,inf);//学习三:初始化图
    for(int i=0;i<m;i++)
    {
        int c1,c2;
        scanf("%d%d",&c1,&c2);
        scanf("%d",&g[c1][c2]);
        g[c2][c1]=g[c1][c2];
        scanf("%d",&cost[c1][c2]);
        cost[c2][c1]=cost[c1][c2];
    }
    dijkstra(s);
    dfs(de);
    printf("%d %d",d[de],c[de]);
    return 0;
}

AC版:(上面代码只能部分AC,没找出问题,烦劳哪位大佬看出来了告知下)

#include<bits/stdc++.h>
#include<cmath>
 
#define mem(a,b) memset(a,b,sizeof a)
#define ssclr(ss) ss.clear(), ss.str("")
#define INF 0x3f3f3f3f
#define MOD 1000000007
 
using namespace std;
 
typedef long long ll;
 
const int maxn=510;
 
int n,m,s,d;
int mp[maxn][maxn], cost[maxn][maxn];
int vis[maxn], dis[maxn], cst[maxn], pre[maxn];
vector<int> vec;
 
void init()
{
    mem(vis,0), mem(cst,0), mem(dis,INF), mem(pre,-1);
    mem(mp,INF), mem(cost,INF);
}
 
int dijkstra(int s)
{
    dis[s]=0;
    while(1)
    {
        int mi=INF,s=-1;
        for(int j=0;j<n;j++)
        {
            if(!vis[j] && dis[j]<mi)
                mi=dis[j], s=j;
        }
 
        if(s==d) return 1;
        if ( s==-1 ) return 0;
        vis[s] = 1;
 
        for(int j=0;j<n;j++)
        {
            if(!vis[j] && mi+mp[s][j]<dis[j])
            {
                pre[j]=s;
                dis[j]=mi+mp[s][j];
                cst[j]=cst[s]+cost[s][j];
            }
            else if(!vis[j] && mi+mp[s][j]==dis[j] && cst[j]>cst[s]+cost[s][j])
                cst[j]=cst[s]+cost[s][j], pre[j]=s;
        }
    }
}
 
int main()
{
    init();
    int a,b,u,v;
    scanf("%d%d%d%d",&n,&m,&s,&d);
    for(int i=0;i<m;i++)
    {
        scanf("%d%d%d%d",&u,&v,&a,&b);
        mp[u][v]=mp[v][u]=a;
        cost[u][v]=cost[v][u]=b;
    }
    if(dijkstra(s))
    {
        vec.clear();
        int h=d;
        while(h!=-1)
        {
            vec.push_back(h);
            h=pre[h];
        }
 
        for(int i=vec.size()-1;i>=0;i--) printf("%d ",vec[i]);
        printf("%d %d\n",dis[d],cst[d]);
    }
    else puts("-1");
 
    return 0;
}
三、小结

参考
DFS+Dijkstra

void dfs(int v)
{
    temppath.push_back(v);
   if(v == st) {
        int tempcost=0; //记录路径花费
        for(int i = temppath.size() - 1; i >= 0; i--) {
            int id=temppath[i],idnext=temppath[i-1];
            tempcost+=cost[id][idnext];
        }
        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();
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值