1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
| #include<cstdio> #include<cstring> #include<vector> #include<queue> using namespace std; const int maxn=100010; const int inf=1<<30; int n,vis[maxn],d[maxn]; vector<int> g[maxn],ans; struct way{ int u,v,c; way(int u=0,int v=0,int c=0):u(u),v(v),c(c){} }; vector<way> ways; void build(int u,int v,int c){ ways.push_back(way(u,v,c)); int i=(int)ways.size()-1; g[u].push_back(i); } void rev_bfs(){ memset(vis,0,sizeof(vis)); d[n-1]=0; vis[n-1]=true; queue<int> q; q.push(n-1); while(!q.empty()){ int v=q.front(); q.pop(); for(int i=0;i<g[v].size();++i){ int e=g[v][i]; int u=ways[e].v; if(!vis[u]){ vis[u]=true; d[u]=d[v]+1; q.push(u); } } } } void bfs(){ memset(vis,0,sizeof(vis)); ans.clear(); vis[0]=true; vector<int> next; next.push_back(0); for(int i=0;i<d[0];++i){ int min_color=inf; for(int j=0;j<next.size();++j){ int u=next[j]; for(int k=0;k<g[u].size();++k){ int e=g[u][k]; int v=ways[e].v; if(d[u]==d[v]+1) min_color=min(min_color,ways[e].c); } } ans.push_back(min_color); vector<int> next2; for(int j=0;j<next.size();++j){ int u=next[j]; for(int k=0;k<g[u].size();++k){ int e=g[u][k]; int v=ways[e].v; if(d[u]==d[v]+1&&!vis[v]&&ways[e].c==min_color){ vis[v]=true; next2.push_back(v); } } } next=next2; } printf("%d\n",(int)ans.size()); for(int i=0;i<ans.size();++i){ if(i) printf(" "); printf("%d",ans[i]); } printf("\n"); } int main(){ int u,v,c,m; while(~scanf("%d%d",&n,&m)){ ways.size(); for(int i=0;i<n;++i) g[i].clear(); while(m--){ scanf("%d%d%d",&u,&v,&c); build(u-1,v-1,c); build(v-1,u-1,c); } rev_bfs(); bfs(); } return 0; }
|