Roadblocks POJ 3255(次短路)
作者:互联网
原题
题目分析
给无向图,求次短路.相对于第k短路而言次短路还是好求的,只需要在跑dijkstra的过程中顺便记录次短路就行了.
代码
1 #include <cstdio> 2 #include <cstdlib> 3 #include <iostream> 4 #include <algorithm> 5 #include <utility> 6 #include <ctime> 7 #include <cmath> 8 #include <cstring> 9 #include <string> 10 #include <stack> 11 #include <queue> 12 #include <vector> 13 #include <set> 14 #include <map> 15 16 using namespace std; 17 typedef long long LL; 18 const int INF_INT=0x3f3f3f3f; 19 const LL INF_LL=0x3f3f3f3f3f3f3f3f; 20 21 typedef pair<int,int> P; 22 struct edge{int to,cost;}; 23 vector<edge> es[6000]; 24 int dis[6000],dis2[6000]; 25 int n,r; 26 27 void add() 28 { 29 int s,t,c; 30 scanf("%d %d %d",&s,&t,&c); 31 es[s].push_back(edge{t,c}); 32 es[t].push_back(edge{s,c}); 33 } 34 35 void dijkstra() 36 { 37 priority_queue<P,vector<P>,greater<P> > que; 38 for(int i=1;i<=n;i++) dis[i]=dis2[i]=INF_INT; 39 dis[1]=0; 40 que.push(P(0,1)); 41 while(que.size()) 42 { 43 P p=que.top();que.pop(); 44 int x=p.second; 45 if(p.first>dis2[x]) continue; 46 for(int i=0;i<es[x].size();i++) 47 { 48 edge t=es[x][i]; 49 int d=p.first+t.cost; 50 if(dis[t.to]>d) swap(dis[t.to],dis2[t.to]),dis[t.to]=d,que.push(P(d,t.to)); 51 else if(dis2[t.to]>d) dis2[t.to]=d,que.push(P(d,t.to)); 52 } 53 } 54 } 55 56 int main() 57 { 58 // freoGen("black.in","r",stdin); 59 // freopen("black.out","w",stdout); 60 cin>>n>>r; 61 for(int i=0;i<r;i++) add(); 62 dijkstra(); 63 printf("%d\n",dis2[n]); 64 return 0; 65 }
标签:dis2,int,短路,3255,Roadblocks,POJ,push,include,dis 来源: https://www.cnblogs.com/VBEL/p/11419177.html