Test Instructions:Link
Method:Spfa
parsing:This inscribed deep search is to die, don't ask me why. In the deep search process you will probably RE8 a point--! complexity similar to O (nm)? In fact, the whole thing is a SPFA, but in this we need to make some changes. This tiger is a DP Tiger, he will occupy your best plan every time. So you can only use sub-optimal solutions to update the best and suboptimal solutions. This is the path of SPFA. But this path is not enough. Let's consider a point when there is only one son. He might sweep his son for the first time to update his answer. At this time there is the shortest circuit and no short circuit. But you're sure to update its son information. At this time the son will come to update this point. Then if we follow the simulation process, then the son will update the short distance and the shortest distance. But obviously, this point is not a short distance. So if the point is updated a short distance, then the point and then go upward will affect the answer. So this situation should be avoided. Specifically how to avoid? is to record the last time it was updated by WHO, if the next time is the same point, then do not update the second short distance. Complexity is the complexity of SPFA, which can be accepted.
Code:
#include <queue>#include <cstdio>#include <cstring>#include <iostream>#include <algorithm>#define N 100100#define M 1000010using namespace STD;intn,m,k,cnt;intVip[n];intHead[n];intV[n];intVal[n];intDis[n];intSec_dis[n];intPre[n];structnode{intFrom,to,val,next;} edge[m<<1];voidInit () {memset(Dis,0x3f,sizeof(dis));memset(Sec_dis,0x3f,sizeof(Sec_dis));memset(head,-1,sizeof(head)); Cnt=1;}voidEdgeadd (intFromintTo,intval) {edge[cnt].from=from,edge[cnt].to=to,edge[cnt].val=val; Edge[cnt].next=head[from]; head[from]=cnt++;}voidBFS () { Queue<int>Q for(intI=1; i<=k;i++) {intXscanf("%d", &x); x + +; dis[x]=sec_dis[x]=0; Q.push (x); v[x]=1; } while(!q.empty ()) {intU=q.front (); Q.pop (); v[u]=0; for(inti=head[u];i!=-1; i=edge[i].next) {intto=edge[i].to;if(Sec_dis[u]+edge[i].val<=dis[to]) {if(Pre[to]!=u) sec_dis[to]=dis[to]; Dis[to]=sec_dis[u]+edge[i].val; Pre[to]=u;if(!v[to]) {Q.push (to); v[to]=1; } }Else if(Sec_dis[u]+edge[i].val<sec_dis[to]) {sec_dis[to]=sec_dis[u]+edge[i].val;if(!v[to]) {Q.push (to); v[to]=1; } } } }}intMain () {init ();scanf("%d%d%d", &n,&m,&k); for(intI=1; i<=m;i++) {intX, Y, Zscanf("%d%d%d", &x,&y,&z); x++,y++; Edgeadd (x, y, z); Edgeadd (Y,X,Z); } BFS ();printf("%d\n", sec_dis[1]);}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Bzoj 2622 [2012 National Training Team Test] lion's Den SPFA