NewSjf @ 2019-10-21 07:39:32
#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())
{
queue<int>q2;
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,
q2.push(edge[k].to);
q=q2;
}
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 lqhsr @ 2019-10-21 07:40:44
常有之事 总是题解AC自己照着打WA
by A_Plus_Gu @ 2019-10-21 07:43:29
@NewSjf 细节问题???
by NewSjf @ 2019-10-21 08:09:30
@lqhsr 过于真实