ACWing1133 次短路算法
ACWing 1133. 第二短路
1 #include<bits/stdc++.h> 2 using namespace std; 3 typedef pair<int,int> pii; 4 const int N=1e5+5; 5 int dis1[N],dis2[N],ans=INT_MAX; 6 vector<pii>G[N]; 7 struct cmp 8 { 9 bool operator ()(pii &a,pii &b) 10 { 11 return a.second>b.second; 12 } 13 }; 14 void dijkstra() 15 { 16 priority_queue<pii,vector<pii>,cmp>q; 17 q.push({1,0}); 18 dis1[1]=0; 19 while(q.size()) 20 { 21 auto p=q.top(); 22 int u=p.first,d=p.second; 23 q.pop(); 24 for(int i=0;i<G[u].size();i++) 25 { 26 int vv=G[u][i].first,w=G[u][i].second; 27 if(d+w<dis1[vv]) 28 { 29 dis2[vv]=dis1[vv]; 30 dis1[vv]=d+w; 31 q.push({vv,d+w}); 32 } 33 else if(d+w>dis1[vv]&&d+w<dis2[vv]) 34 { 35 dis2[vv]=d+w; 36 q.push({vv,d+w}); 37 } 38 } 39 40 } 41 42 } 43 int main() 44 { 45 int n,m; 46 scanf("%d%d",&n,&m); 47 memset(dis1,0x3f,sizeof(dis1)); 48 memset(dis2,0x3f,sizeof(dis2)); 49 for(int i=1;i<=m;i++) 50 { 51 int u,v,w; 52 scanf("%d%d%d",&u,&v,&w); 53 G[u].push_back({v,w}); 54 G[v].push_back({u,w}); 55 } 56 dijkstra(); 57 cout<<dis2[n]<<endl; 58 return 0; 59 }