代码如下
#include <bits/stdc++.h>
#define PBAR cout<<"*******\n";
#define fst first
#define snd second
#define INFILE(x) freopen(x,"r",stdin)
#define OUTFILE(x) freopen(x,"w",stdout)
using namespace std;
using ll=long long;
using ul=unsigned long long;
using ui=unsigned int;
using db=double;
char _buf[1<<21],*_p1=_buf,*_p2=_buf;
int _getc(){return _p1==_p2&&(_p2=(_p1=_buf)+fread(_buf,1,1<<21,stdin),_p1==_p2)?EOF:*(_p1++);}
template<class T>
T read() {
T x = 0, w = 1;
char ch = 0;
while (!isdigit(ch)) {
if (ch == '-') w = -1;
ch = _getc();
}
while (isdigit(ch)) {
x = ((x<<3)+(x<<1)) + (ch - '0');
ch = _getc();
}
return x*w;
}
ll readll(){return read<ll>();}
int readi(){return read<int>();}
const int maxn=50;
int n,m,s[maxn][maxn],k;
ll r,unreached[maxn],path[maxn];
int main() {
//INFILE("IOFile/CF20C_6.in");
//memset(s,0x3f,sizeof(s));
n=readi(),m=readi();
while(m--){
int u=readi(),v=readi(),w=readi();
s[u][v]=w;
s[v][u]=w;
}
r=0,unreached[n]=-1,k=n;
//memset(unreached,0x3f,sizeof(unreached));
while (k^1){
int m=1;
for(int i=1;i<n;++i){
if(unreached[i]==-1)continue;
if(unreached[i]&&(!unreached[m]||unreached[i]<unreached[m]))m=i;
if(s[k][i]&&(!unreached[i]||unreached[i]>r+s[k][i])){
unreached[i]=r+s[k][i];
path[i]=k;
if(!unreached[m]||unreached[i]<unreached[m])m=i;
}
}
k=m,r=unreached[m],unreached[m]=-1;
}
if(!path[1]){
cout<<"-1\n";
return 0;
}
while(k){
cout<<k<<' ';
cout.flush();
k=path[k];
}
cout<<'\n';
return 0;
}
在codeforces上显示Idleness limit exceeded,不知道为什么,上网搜索关于这个错误的信息发现好像是交互题才会有。