The general idea is to do one step at a time from the end of the BFS to get a level diagram, and then start from the beginning to the smaller layer run, run when the dictionary order is the smallest, because there may be a number of points to meet the conditions, so the layer satisfies the conditions of the point to save, in the next layer of running. After running a layer, you will get the lowest color of this layer.
Introspection: This problem is due to the existence of self-loops and heavy edges, so a point satisfying the condition may be added to the queue multiple times, so that the complexity will be exponentially a number of degrees. Did not notice that this tle a few hair ... If the shortest path of one point to another point is only one, it is not necessary to judge the repetition. That's because of the heavy side, so you need to pay attention.
:
#include <cstdio>#include<cstring>#include<vector>#include<queue>//#define LOCALusing namespacestd;Const intINF =1e9;Const intMAXM = 2e5 +Ten;Const intMAXN = 1e5 +5;intN;structedge{intV,C,NXT;} E[MAXM<<1];intD[MAXN];intcnt, HEAD[MAXN];//Init head-1InlinevoidAddedge (intUintVintc) { //e[cnt].u = u;E[CNT].V =v; E[CNT].C=C; E[CNT].NXT=Head[u]; Head[u]= cnt++;}voidBFs () {Queue<int>Q; memset (d,-1,sizeof(d)); Q.push (n); D[n]=0; intU,v,i; while(!Q.empty ()) {u=Q.front (); Q.pop (); if(U = =1) {printf ("%d\n", D[u]);return ;} for(i = head[u]; ~i; i =e[i].nxt) {v=e[i].v; if(~d[v])Continue; D[V]= D[u] +1; Q.push (v); } }}BOOLVIS[MAXN];voidBFS2 () {Queue<int> q;///The complexity is written high, and the node is not judged by the repetition of the order. intU =1, V, I; Q.push (U); intc = INF;//Minimum Colorvector<int> VEC;//Save the next level of pointsmemset (Vis,false,sizeof(VIS)); while(!q.empty () | |!Vec.empty ()) { if(Q.empty ()) {//ensure that there is only one level of points in the queue, if the queue is empty, indicating that the last layer of the point is running out, this time C must be the smallest for(i =0; i < vec.size (); i++) { intk = Vec[i], V =e[k].v; if(e[k].c = = c &&!vis[v]) {//Vis[v] The heavy side. if(e[k].v = = N) {printf ("%d\n", c);return ;} Q.push (E[VEC[I]].V); VIS[V]=true; }} vec.clear (); printf ("%d", c); c =INF; } u=Q.front (); Q.pop (); for(i = head[u]; ~i; i =e[i].nxt) {v=e[i].v; if(D[u]-d[v] = =1&& e[i].c <=c) {vec.push_back (i); C=e[i].c; } } }}intMain () {#ifdef local freopen ("In.txt","R", stdin);#endif //Local intm; intU,v,c; while(~SCANF ("%d%d",&n,&m)) {memset (head,-1,sizeof(head)); CNT =0; while(m--) {scanf ("%d%d%d",&u,&v,&c); if(U = = v)Continue;//Ignore self-loopAddedge (U,V,C); Addedge (V,U,C); } BFS (); BFS2 (); } return 0;}
"uva1599" "Poj 3092" "Level BFS" Ideal Path