CF20C Dijkstra?【Dijkstra+路径记录】

题目描述

You are given a weighted undirected graph. The vertices are enumerated from 1 to nn . Your task is to find the shortest path between the vertex 11 and the vertex nn .

输入格式

The first line contains two integers nn and mm ( 2<=n<=10^{5},0<=m<=10^{5}2<=n<=105,0<=m<=105 ), where nn is the number of vertices and mm is the number of edges. Following mm lines contain one edge each in form a_{i}ai​ , b_{i}bi​ and w_{i}wi​ ( 1<=a_{i},b_{i}<=n,1<=w_{i}<=10^{6}1<=ai​,bi​<=n,1<=wi​<=106 ), where a_{i},b_{i}ai​,bi​ are edge endpoints and w_{i}wi​ is the length of the edge.

It is possible that the graph has loops and multiple edges between pair of vertices.

输出格式

Write the only integer -1 in case of no path. Write the shortest path in opposite case. If there are many solutions, print any of them.

题意翻译

题目大意

给出一张图,请输出其中任意一条可行的从点 11 到点 nn 的最短路径。

输入输出格式

输入格式

第一行:两个整数n,m,分别表示点数和边数

接下来m行:每行三个整数u,v,w,表示u和v之间连一条边权为w的双向边。

输出格式

一行:一个可行的路径,如果不存在这种路径输出-1

2<=n<=10^5,0<=m<=10^5

输入输出样例

输入 #1

5 6
1 2 2
2 5 5
2 3 4
1 4 1
4 3 3
3 5 1

输出 #1

1 4 3 5 

输入 #2

5 6
1 2 2
2 5 5
2 3 4
1 4 1
4 3 3
3 5 1

输出 #2

1 4 3 5 

奇葩的坑点:刚开始写第31的测试点一直wrong,后来发现INF=0x3f3f3f3f太小了,啪啪啪,恨恨的打脸上了,以后INF不可能小了,以后我程序的INF=0x7f7f7f7f7f7f7f7f,呜呜呜

AC代码:

#include<bits/stdc++.h>
#define INF 9223372036854775806
using namespace std;
typedef long long ull;
const int Max=1e5+100;
struct node{
    int to;
    ull data;
};
struct S_node{
    int value;
    ull key;
    friend bool operator<(S_node a,S_node b){
        return a.key>b.key;
    }
}room[Max];
int n,m;
int flag=0;
vector<node>mp[Max];
bool vis[Max];
int pos[Max],ans[Max];
void dijkstra(){
    S_node start,next;
    priority_queue<S_node>Q;
    for(int i=1;i<=n;i++){
        room[i].value=i;
        room[i].key=INF;
    }
    room[1].key=0;
    start=room[1];
    Q.push(start);
    //cout<<start.key<<endl;
    while(!Q.empty()){
        start=Q.top();
        Q.pop();
        for(int i=0;i<mp[start.value].size();i++){
            next.value=mp[start.value][i].to;
            if(!vis[next.value]&&room[next.value].key>mp[start.value][i].data+room[start.value].key){
                room[next.value].key=mp[start.value][i].data+room[start.value].key;
                next.key=room[next.value].key;
                Q.push(next);
                pos[next.value]=start.value;//记录路径
            }
        }
        vis[start.value]=true;
    }
}
int main(){
    scanf("%d%d",&n,&m);
    for(int i=0;i<m;i++){
        node temp;
        int u,v,w;
        scanf("%d%d%d",&u,&v,&w);
        temp.to=v;temp.data=w;
        mp[u].push_back(temp);
        temp.to=u;
        mp[v].push_back(temp);
    }
    dijkstra();
    ans[flag]=1;
    int word=0;
    for(int i=n;i;i=pos[i]){
       // cout<<i<<endl;
        ans[++flag]=i;
        if(i==1) word=1;
    }
    if(word==0){
        printf("-1\n");return 0;
    }
    for(int i=flag;i>=1;i--) printf("%d ",ans[i]);
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

瘾ิۣۖิۣۖิۣۖิꦿ

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值