02Ljh @ 2023-02-02 14:24:19
感觉算法和紫书上的没问题
#include <bits/stdc++.h>
using namespace std;
#define INF 0x3f3f3f3f3f
#define ll long long
#define int long long
#define MAXN 200019
#define none 1145141919810
#define WA puts("CCF")
int n,m;
vector<int> bq[MAXN];
vector<int> bqf[MAXN];//
vector<int> pq[MAXN];
vector<int> pqf[MAXN];//
int fl[MAXN];
deque<int> q;
deque<int> dep;
void bfs1()
{
q.clear();
dep.clear();
q.push_back(1);
dep.push_back(1);
while(!q.empty())
{
int top=q.front();
int dp=dep.front();
dep.pop_front();
q.pop_front();
if(fl[top]>dp) fl[top]=dp;
else continue;
for(int i=0;i<pq[top].size();i++)
{
int g=pq[top][i];
dep.push_back(dp+1);
q.push_back(g);
}
}
return ;
}
//vector<int> ans;
int qq[MAXN];//minn
void bfs2()
{
q.clear();
dep.clear();//前驱
q.push_back(1);
dep.push_back(-1);
qq[0]=-1;
//dep.push_back(1);
while(!q.empty())
{
int top=q.front();
//cout<<top<<"\n";
int dp=dep.front();
dep.pop_front();
q.pop_front();
if(dp!=qq[fl[top]-1]) continue;
//WA;
if(top==n) break;
int minn=INF,mins;
for(int i=0;i<pq[top].size();i++)
{
int g=pq[top][i];
int val=bq[top][i];
if(minn>val&&fl[top]+1==fl[g])
{
minn=val;
mins=g;
}
}
//q.push_back(mins);
//ans.push_back(minn);
for(int i=0;i<pq[top].size();i++)
{
int g=pq[top][i];
int val=bq[top][i];
if(fl[top]+1==fl[g]&&minn==val)
{
q.push_back(g);
dep.push_back(val);
//ans[fl[top]]=minn;
}
}
//9cout<<top<<" "<<minn<<"\n";
qq[fl[top]]=min(qq[fl[top]],minn);
}
return ;
}
main()
{
//cout<<INF;
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
while(cin>>n>>m)
{
for(int i=1;i<=n;i++) { qq[i]=INF; fl[i]=INF; bq[i].clear(); pq[i].clear(); /*bqf[i].clear(); pqf[i].clear();*/ }
for(int i=1;i<=m;i++)
{
int u,v,w;
cin>>u>>v>>w;
pq[u].push_back(v);
bq[u].push_back(w);
pq[v].push_back(u);
bq[v].push_back(w);
}
bfs1();
cout<<(fl[n]-1)<<"\n";
//for(int i=1;i<=n;i++) cout<<fl[i]<<" ";
bfs2();
for(int i=1;i<=fl[n]-1;i++) { cout<<qq[i]; if(fl[n]-1!=i) { cout<<" "; } }
//while(!ans.empty()) { cout<<ans.back()<<" "; ans.pop_back(); }
cout<<"\n";
}
return 0;
}
/*
6 6
6 5 1
5 4 4
4 1 2
1 2 2
2 3 2
3 6 3
*/