堆优化的dijkstra
1 #include<bits/stdc++.h> 2 #define ll long long 3 using namespace std; 4 const ll N=1e5+2; 5 ll n,h[N],tot,S,m; 6 struct node{ 7 ll v,c,ne; 8 }e[N*8]; 9 void add(ll u,ll v,ll c) 10 { 11 tot++;e[tot]=(node){v,c,h[u]};h[u]=tot; 12 } 13 struct OK{ 14 ll x,d; 15 friend bool operator <(OK A,OK B) 16 { 17 return A.d>B.d; 18 } 19 }ff,rr; 20 priority_queue<OK>q; 21 bool v[N]; 22 ll d1[N]; 23 void dij() 24 { 25 for(ll i=1;i<=n;++i) d1[i]=2147483647,v[i]=0; 26 d1[S]=0;q.push((OK){S,0}); 27 while(!q.empty()) 28 { 29 ff=q.top();q.pop(); 30 if(v[ff.x]) continue; 31 v[ff.x]=1; 32 for(ll i=h[ff.x];i;i=e[i].ne) 33 { 34 if(d1[e[i].v]>d1[ff.x]+e[i].c) 35 { 36 d1[e[i].v]=d1[ff.x]+e[i].c; 37 q.push((OK){e[i].v,d1[e[i].v]}); 38 } 39 } 40 } 41 } 42 int main() 43 { 44 scanf("%lld%lld%lld",&n,&m,&S); 45 for(ll i=1,x,y,z;i<=m;++i) 46 { 47 scanf("%lld%lld%lld",&x,&y,&z); 48 add(x,y,z);//add(y,x,z); 49 } 50 dij(); 51 for(ll i=1;i<=n;++i) cout<<d1[i]<<" "; 52 return 0; 53 }
粪虫至秽,变为蝉而饮露于秋风。
鬼话,不如多喝几口毒鸡汤,效果杠杠的