Description

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

Input

The first line contains two integers n and m (2 ≤ n ≤ 105, 0 ≤ m ≤ 105), where n is the number of vertices and m is the number of edges. Following m lines contain one edge each in form aibi and wi (1 ≤ ai, bi ≤ n, 1 ≤ wi ≤ 106), where ai, bi are edge endpoints and wi is the length of the edge.

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

Output

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.

Sample Input

Input
5 61 2 22 5 52 3 41 4 14 3 33 5 1

Output
1 4 3 5 

Input
5 61 2 22 5 52 3 41 4 14 3 33 5 1

Output
1 4 3 5 

最短路我用的DJ之前没写出来的原因是不会保存路径
 1 #include<stdio.h>
 2 #include<queue>
 3 #define LL long long
 4 using namespace std;
 5 const int maxn = 400005;//这个给大一点,四倍
 6 const LL maxd = 1E13;
 7 int v[maxn],w[maxn],next[maxn],pre[maxn],res[maxn];
 8 int first[maxn],inq[maxn],e;
 9 LL d[maxn];
10
11 void init()
12 {
13     for(int i=0;i<maxn;i++)
14     {
15         first[i]=-1;
16     }
17     e=0;
18 }
19
20
21 void addeage(int x,int y,int z)
22 {
23     v[e]=y;
24     w[e]=z;
25     next[e]=first[x];
26     first[x]=e;
27     e++;
28 }
29
30 void spfa(int s)
31 {
32     queue<int> q;
33     for(int i =0;i<maxn;i++)
34         d[i]=maxd;
35     d[s]=0;inq[s]=1;q.push(s);
36     while(q.empty()==false)
37     {
38         int u = q.front();
39         q.pop();
40         inq[u]=0;
41         for(int i =first[u];i!=-1;i=next[i])
42         {
43             if(d[v[i]]>d[u]+w[i])
44             {
45                 d[v[i]]=(d[u]+w[i]);
46                 pre[v[i]]=u;//把前一个点存储
47                 if(inq[v[i]]==0)
48                 {
49                     q.push(v[i]);
50                     inq[v[i]]=1;
51                 }
52             }
53         }
54     }
55 }
56
57 int main()
58 {
59     init();
60     int m,n;
61     scanf("%d%d",&m,&n);
62     int x,y,z;
63     while(n--)
64     {
65         scanf("%d%d%d",&x,&y,&z);
66         addeage(x,y,z);
67         addeage(y,x,z);//Undirected无向图,双向的
68     }
69     spfa(1);
70     if(d[m]==maxd)
71         printf("-1");
72     else
73     {
74         int now=m;//从终点开始把之前的一个个找出来
75         int cnt=0;
76         while(now!=1)
77         {
78            res[cnt++] = now;//用来保存路径
79            now = pre[now];
80         }
81         res[cnt++] = 1;
82         for(int i = cnt-1;i >= 0;i--)
83             printf("%d ",res[i]);
84     }
85     return 0;
86 }
87     

转载于:https://www.cnblogs.com/Run-dream/p/3889316.html

Problem A Codeforces 20C 最短路(dj,spfa)相关推荐

  1. HDOJ 2112 HDU Today (最短路 Dijkstra SPFA)

    HDU Today Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total ...

  2. Problem - 1335E2 - Codeforces(暴力+优化)

    Problem - 1335E2 - Codeforces 题目大意: 给定你长度为nnn的序列,让你将其分成三个子序列,满足如下关系[a,...,a⏟x,b,...,b⏟y,a,...,a⏟x][\ ...

  3. 最短路——最短路(spfa)

    题目链接 最短路--最短路(spfa) 题目描述 简单暴力的题目要求: 给定一个有n个顶点(从1到n编号),m条边的有向图(其中某些边权可能为负,但保证没有负环).请你计算从1号点到其他点的最短路. ...

  4. Problem - 1190B - Codeforces(分类讨论博弈)

    Problem - 1190B - Codeforces 题目大意:有nnn堆石子,每堆的数目为aia_iai​,现在Alice和Bob两个人可以移走任意一堆不为空的石子中的一颗石子,如果某人移动完石 ...

  5. CodeForces 173B Chamber of Secrets spfa

    Chamber of Secrets 题目连接: http://codeforces.com/problemset/problem/173/B Description "The Chambe ...

  6. Codeforces Round #439 (Div. 2) Problem C (Codeforces 869C) - 组合数学

    - This is not playing but duty as allies of justice, Nii-chan! - Not allies but justice itself, Onii ...

  7. 【CF464E】The Classic Problem(主席树+最短路)

    点此看题面 大致题意: 给你一张无向图,每条边的边权为\(2^{x_i}\),求\(s\)到\(t\)的最短路. 最短路 最短路,首先考虑\(Dijkstra\).这里用\(SPFA\)似乎不太好,因 ...

  8. 最短路计数(spfa)

    题目描述 给出一个N个顶点M条边的无向无权图,顶点编号为1-N.问从顶点1开始,到其他每个点的最短路有几条. 输入输出格式 输入格式: 输入第一行包含2个正整数N,M,为图的顶点数与边数. 接下来M行 ...

  9. nssl1141,jzoj3470-最短路【SPFA,暴力】

    正题 纪中题目链接:https://jzoj.net/senior/#main/show/3470 题目大意 在从起点经过k个标记点然后到终点的最短路. 解题思路 用SPFA求出起点和所有标记点的最短 ...

  10. 【2019icpc南京站网络赛 - H】Holy Grail(最短路,spfa判负环)

    题干: As the current heir of a wizarding family with a long history,unfortunately, you find yourself f ...

最新文章

  1. 关于Qt Designer程序/UI文件打开未响应的解决方法
  2. 海量数据处理之Bloom Filter详解
  3. python 反转链表
  4. (总结)Linux的/etc/services文件简析
  5. vue实现表格组件,带分页
  6. .NET Core实战项目之CMS 第六章 入门篇-Vue的快速入门及其使用
  7. php gaufrette,php – 使用Gaufrette Stream Wrappers和AsseticBundle
  8. jdbc、jpa、spring data jpa、hibernate、mybatis之间的关系及区别
  9. eclipse字体大小调整
  10. 幻灯片转换html动画,html5 css3仿ppt幻灯片播放动画效果
  11. oppo怎么广告接入_oppo搜索广告投放操作指南
  12. Anaconda Prompt :python.exe - 无法找到入口,无法定位程序输入点
  13. 动手学深度学习 PyTorch版-Day3
  14. 原装世嘉土星手柄(Sega Saturn)转USB小板,软硬件全开源
  15. 我的世界超级英雄无限服务器,我的世界超级英雄无限整合包
  16. 挂耳耳机十大品牌排行榜哪个好,目前排行靠前的五款耳机推荐
  17. BZOJ 2716/CH 4701 天使玩偶
  18. 【mysql or条件是否走索引】
  19. android homme2016款,丁日的最爱:Android Homme。WTF!安卓家?
  20. 2016年计算机考研408操作系统真题(客观题)

热门文章

  1. linux firefox 解雇ie,Fire IE
  2. linux下运行springboot项目jar包
  3. 支持移动触摸的jQuery图片Lightbox插件 1
  4. 扩展GridView控件(3) - 根据按钮的CommandName设置其客户端属性
  5. datagrid commandname
  6. 数据结构试卷及答案(十)
  7. 数据分析实战项目练习——餐厅订单数据
  8. [Android第三方类库]聊一聊Android的第三方开发组件
  9. 数字图像处理 matlab 报告总结,matlab 数字图像处理实验报告(五份)
  10. 【python量化】用python搭建一个股票舆情分析系统