1#include <iostream>2#include <cstring>3#include <cstdio>4#include <queue>5 #defineMAX 99999996 7 using namespacestd;8 9 structNode {Ten intV,w;//v end point, W weight value One intNest//Next A }; -Node edge[150000];//forward star each element is each edge - inthead[25030];//An array of head pointers, an array of pointers to the adjacency list the intdis[25010];//Save the shortest distance per point - intflag[25010];//the number of times to save a point to join a queue - BOOLvis[25010];//Tag Array - intCnt// Subscript + - + voidAddintUintVintW//Add Edge A { atedge[cnt].v=v; -edge[cnt].w=W; -Edge[cnt].nest=head[u];//equivalent to the chain header insertion method -head[u]=cnt++;// - } - in BOOLSPFA (intSintt) - { to intI,u,v;//u point v found from Q +deque<int> que;//bidirectional Queue - theFill (dis,dis+t+1, MAX); *memset (Flag,0,sizeof(flag)); $memset (Vis,false,sizeof(Vis));Panax Notoginsengdis[s]=0;//s for? That S is the starting point -Que.push_back (s);//join S to queue the while(!que.empty ())//the queue is not empty + { A theU=que.front ();//out of the queue +Que.pop_front ();//Delete -vis[u]=false;//mark as not visited $ for(i=head[u];i!=-1; i=edge[i].nest)//find all edges adjacent to the point $ { -v=edge[i].v; - if(dis[v]>dis[u]+EDGE[I].W) the { -DIS[V]=DIS[U]+EDGE[I].W;//Relaxation SuccessWuyi if(!vis[v])//represents unmarked the { -vis[v]=true;//Mark Wu //flag[v]++;//indicates the number of times the point has entered the queue - //if (flag[v]>=n)//If the point enters the queue more than n times indicates a negative ring About //return true;//returns a negative ring $ //The following is the SLF optimization - if(!que.empty () &&dis[v]<dis[que.front ()])//if the queue is empty && queue first element distance is greater than the current point -Que.push_front (v);//join the team first - Else A Que.push_back (v); + } the } - } $ } the the return true;//no negative ring the } the - intMain () in { the intt,r,p,s,u,v,w,i; the Aboutscanf"%d%d%d%d",&t,&r,&p,&s); thememset (head,-1,sizeof(head)); theCnt=0; the while(r--) + { -scanf"%d%d%d",&u,&v,&W); theAdd (U,V,W); Add (V,U,W);//bidirectional non-directional graphBayi } the while(p--) the { -scanf"%d%d%d",&u,&v,&W); - Add (u,v,w); the } the SPFA (s,t); the for(i=1; i<=t;i++) the { - if(dis[i]>=MAX) theprintf"NO path\n"); the Else theprintf"%d\n", Dis[i]);94 } the return 0; the } the
View Code
Just started to open the array, got 30 points, on the Internet to find a problem, feeling and my same, submitted on the right after passing, then I read the question, careless.
Roads and routes