题目描述
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]);
}