NewSjf @ 2019-10-18 22:57:54
#include<iostream>
#include<algorithm>
#include<queue>
#define MAXN 2000001
#define INF 252645135
using namespace std;
struct side
{
int from,to,next,w,color;
}edge[MAXN];
int head[MAXN],dis[MAXN],visit[MAXN],len,n,m;
int ans[MAXN];
void ins(int x,int y,int d,int c)
{
edge[++len]=(side){x,y,head[x],d,c};
head[x]=len;
}
void bfs(int st,int ed)
{
fill(dis,dis+1+n,INF);
fill(visit,visit+1+n,false);
fill(ans,ans+1+n,INF);
queue<int>q;
q.push(ed);
dis[ed]=0;
while(q.size())
{
auto now=q.front();
q.pop();
for(int k=head[now];k;k=edge[k].next)
if(dis[edge[k].to]==INF)
dis[edge[k].to]=dis[now]+1,
q.push(edge[k].to);
}
q.push(st);
while(q.size())
{
auto now=q.front();
q.pop();
int MinColor=INF;
for(int k=head[now];k;k=edge[k].next)
if(dis[edge[k].to]+1==dis[now])
MinColor=min(MinColor,edge[k].color);
ans[dis[now]]=min(ans[dis[now]],MinColor);
for(int k=head[now];k;k=edge[k].next)
if(dis[edge[k].to]+1==dis[now]&&!visit[edge[k].to]&&edge[k].color==MinColor)
visit[edge[k].to]=true,
q.push(edge[k].to);
}
cout<<dis[1]<<endl;
for(int i=dis[1];i;--i)
{
cout<<ans[i];
if(i!=1)cout<<" ";
}
cout<<endl;
}
int main()
{
while(cin>>n>>m)
{
fill(head,head+1+n,false);
len=0;
for(int i=1;i<=m;i++)
{
int x,y,color;
cin>>x>>y>>color;
if(x==y)continue;
ins(x,y,1,color);
ins(y,x,1,color);
}
bfs(1,n);
}
}
by 菰冭 @ 2019-10-19 13:03:54
%