Skip to content

Instantly share code, notes, and snippets.

@lackofdream
Created May 13, 2015 12:13
Show Gist options
  • Select an option

  • Save lackofdream/a4368bb75f06e3fe17b5 to your computer and use it in GitHub Desktop.

Select an option

Save lackofdream/a4368bb75f06e3fe17b5 to your computer and use it in GitHub Desktop.
Codeforces 20C
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <queue>
#include <cmath>
#define abs(x) ((x)>0?(x):(0-(x)))
#define MAX(a,b) (a>b?a:b)
#include <set>
#include <climits>
#define LL long long
using namespace std;
const int maxn = 400005;
const LL maxd = 1E13;
int v[maxn],w[maxn],next[maxn],pre[maxn],res[maxn];
int first[maxn],inq[maxn],e;
LL d[maxn];
void init()
{
memset(first,-1,sizeof(first));
e=0;
}
void addeage(int x,int y,int z)
{
v[e]=y;
w[e]=z;
next[e]=first[x];
first[x]=e;
e++;
}
void spfa(int s)
{
queue<int> q;
for(int i =0;i<maxn;i++)
d[i]=maxd;
d[s]=0;inq[s]=1;q.push(s);
while(q.empty()==false)
{
int u = q.front();
q.pop();
inq[u]=0;
for(int i =first[u];i!=-1;i=next[i])
{
if(d[v[i]]>d[u]+w[i])
{
d[v[i]]=(d[u]+w[i]);
pre[v[i]]=u;
if(inq[v[i]]==0)
{
q.push(v[i]);
inq[v[i]]=1;
}
}
}
}
}
int main()
{
init();
int m,n;
scanf("%d%d",&m,&n);
int x,y,z;
while(n--)
{
scanf("%d%d%d",&x,&y,&z);
addeage(x,y,z);
addeage(y,x,z);
}
spfa(1);
if(d[m]==maxd)
printf("-1");
else
{
int now=m;
int cnt=0;
while(now!=1)
{
res[cnt++] = now;
now = pre[now];
}
res[cnt++] = 1;
for(int i = cnt-1;i >= 0;i--)
printf("%d ",res[i]);
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment