标签:its 双向 += pop 政策 enter copy 个数 --
6 3 3 4
1 2 5
3 4 5
5 6 10
3 5 -100
4 6 -100
1 3 -10
NO PATH
NO PATH
5
0
-95
-100
if
(!q.empty()&&dis[v]<dis[q.front()]) q.push_front(v);
else
q.push_back(v);
1 #pragma GCC optimize(3,"Ofast","inline") 2 #include <bits/stdc++.h> 3 using namespace std; 4 const int N=500000; 5 int n,r,p,s; 6 struct ss 7 { 8 int u,v,w,next; 9 }edg[N]; 10 int sumedg,head[N]; 11 void addedg(int u,int v,int w) 12 { 13 edg[sumedg]=(ss){u,v,w,head[u]}; 14 head[u]=sumedg++; 15 } 16 int dis[N]; 17 bool vis[N]; 18 void spfa() 19 { 20 deque<int>q; 21 for(int i=0;i<n+10;i++) dis[i]=INT_MAX; 22 memset(vis,0,sizeof(vis)); 23 q.push_back(s); 24 vis[s]=1; 25 dis[s]=0; 26 long long sum1=0,sum2=1; 27 28 while(!q.empty()) 29 { 30 int now=q.front();q.pop_front(); 31 32 sum2--; 33 sum1-=dis[now]; 34 35 while(sum2&&sum2*dis[now]>sum1) 36 { 37 q.push_back(now); 38 sum1+=dis[now]; 39 now=q.front(); 40 q.pop_front(); 41 sum1-=dis[now]; 42 } 43 44 vis[now]=0; 45 for(int i=head[now];i!=-1;i=edg[i].next) 46 { 47 int v=edg[i].v; 48 if(dis[v]>dis[now]+edg[i].w) 49 { 50 dis[v]=dis[now]+edg[i].w; 51 if(!vis[v]) 52 { 53 vis[v]=1; 54 if(!q.empty()&&dis[v]<dis[q.front()]) q.push_front(v); 55 else q.push_back(v); 56 57 sum1+=dis[v]; 58 sum2++; 59 } 60 } 61 } 62 } 63 } 64 int main() 65 { 66 scanf("%d%d%d%d",&n,&r,&p,&s); 67 for(int i=0;i<=n;i++) head[i]=-1; 68 for(int i=0;i<r;i++) 69 { 70 int u,v,w; 71 scanf("%d%d%d",&u,&v,&w); 72 addedg(u,v,w); 73 addedg(v,u,w); 74 } 75 for(int i=0;i<p;i++) 76 { 77 int u,v,w; 78 scanf("%d%d%d",&u,&v,&w); 79 addedg(u,v,w); 80 } 81 spfa(); 82 for(int i=1;i<=n;i++) 83 { 84 if(dis[i]!=INT_MAX) printf("%d\n",dis[i]); 85 else printf("NO PATH\n"); 86 } 87 return 0; 88 }
标签:its 双向 += pop 政策 enter copy 个数 --
原文地址:https://www.cnblogs.com/sylvia1111/p/11871220.html