PAT甲级 1030 Travel Plan(30) (Dijkstra)

题目

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, 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.

输出

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.

样例输入 

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

样例输出 

0 2 3 3 40

题意理解

大致题意就是给你n个点,m条边,注意这里点是从0开始的,然后给你起点 终点坐标

每条边有 距离 和 成本花费

让你求出最后最短距离下,最小成本花费是多少。优先是要保证最短路径。

这题还要记录下路径

所以我们开一个path数组记录路径 注意一开始全部设置成-1方便我们最后从终点回溯过来

举个例子我们从 2这个顶点转移动到3这个顶点 那么我们的路径数组就是path[3]=2,表示3由2转移过来

然后我们只有在距离相同的情况下去更新我们的成本花费

那么一旦我们找到更短的路径

成本直接就是 ve点->j点

cost[j]=cost[ve]+d;

而我们找到同样短的路径时候

去判断一下成本是否会会更低,会的话更新,不会的话就不更新

这里笔者用Dijkstra算法的堆优化版本实现

代码 

#include<bits/stdc++.h>
using namespace std;
typedef pair<int,int> PII;
#define fx first 
#define fy second
const int N=1e5+10,INF=0x3f3f3f3f;
int h[N],w[N],e[N],co[N],ne[N],idx;//链式前向星
int cost[N];//总花费
void add(int a,int b,int c,int d){
    e[idx]=b;w[idx]=c;co[idx]=d;ne[idx]=h[a];h[a]=idx++;
}
int path[N];//路径
bool st[N];//判重数组
vector<int>ans;//路径回溯之后的真正路径
int n,m;
int ss,en;//起点 终点
int dis[N];//距离数组
void dijkstra(){
     memset(dis,INF,sizeof dis);
     memset(cost,INF,sizeof cost);
     cost[ss]=0;dis[ss]=0;
     priority_queue<PII,vector<PII>,greater<PII> >heap;
     heap.push({0,ss});
     while(heap.size()){
        PII t=heap.top();
        heap.pop();
        int ve=t.fy;
        if(st[ve])continue;
        st[ve]=1;
        
        for(int i=h[ve];~i;i=ne[i]){
            int j=e[i],c=w[i],d=co[i];
            if(dis[j]>dis[ve]+c){
                path[j]=ve;
                dis[j]=dis[ve]+c;
                cost[j]=cost[ve]+d;
                heap.push({dis[j],j});
            }
            else if(dis[j]==dis[ve]+c){
                if(cost[j]>cost[ve]+d){
                    path[j]=ve;
                    cost[j]=cost[ve]+d;
                    heap.push({dis[j],j});
                }
            }
        }
     }
}
int main(){
    memset(h,-1,sizeof h);
    memset(path,-1,sizeof path);
    scanf("%d%d%d%d",&n,&m,&ss,&en);
    while(m--){
        int a,b,c,d;
        scanf("%d%d%d%d",&a,&b,&c,&d);
        add(a,b,c,d);
        add(b,a,c,d);
    }
    dijkstra();
    int ee=en;
    ans.push_back(ee);
    while(path[ee]!=-1){
        ee=path[ee];
        ans.push_back(ee);
    }
    reverse(ans.begin(),ans.end());
    for(int i=0;i<ans.size();i++){
        printf("%d ",ans[i]);
    }
    printf("%d %d",dis[en],cost[en]);
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值