题目:https://vjudge.net/problem/POJ-3255
Bessie has moved to a small farm and sometimes enjoys returning to visit one of her best friends. She does not want to get to her old home too quickly, because she likes the scenery along the way. She has decided to take the second-shortest rather than the shortest path. She knows there must be some second-shortest path.
The countryside consists of R (1 ≤ R ≤ 100,000) bidirectional roads, each linking two of the N (1 ≤ N ≤ 5000) intersections, conveniently numbered 1..N. Bessie starts at intersection 1, and her friend (the destination) is at intersection N.
The second-shortest path may share roads with any of the shortest paths, and it may backtrack i.e., use the same road or intersection more than once. The second-shortest path is the shortest path whose length is longer than the shortest path(s) (i.e., if two or more shortest paths exist, the second-shortest path is the one whose length is longer than those but no longer than any other path).
Input
Lines 2.. R+1: Each line contains three space-separated integers: A, B, and D that describe a road that connects intersections A and B and has length D (1 ≤ D ≤ 5000)
Output
Sample Input
4 4 1 2 100 2 4 200 2 3 250 3 4 100
Sample Output
450
Hint
#include<iostream> #include<cstdio> #include<cstring> #include<cmath> #include<queue> #include<set> #include<algorithm> #include<map> #define maxn 100005 #define inf 1e9+7 using namespace std; typedef pair<int,int>P;//first 是最短距离 second 顶点编号 struct edge{ int to,cost; }; int n,r; int dis[maxn]; int dis2[maxn]; vector<edge>mp[maxn]; priority_queue<P,vector<P>,greater<P> >que; void dijkstra(int n,int s) { memset(dis,inf,sizeof(dis)); memset(dis2,inf,sizeof(dis2)); dis[0]=0; que.push(P(0,0)); while(!que.empty()) { P s=que.top(); que.pop(); int v=s.second,d=s.first; if(dis2[v]<d)continue; for(int i=0;i<mp[v].size();i++) { edge e=mp[v][i]; int d2=d+e.cost; if(dis[e.to]>d2){swap(dis[e.to],d2); //找最短路 并且把比最短路大一点的dis【e.to】赋值给d2; que.push(P(dis[e.to],e.to)); //用队列存下最短路和那个顶点 } if(dis2[e.to]>d2&&dis[e.to]<d2) { dis2[e.to]=d2; //找次短路 如果dis2【】<dis[]就可以 que.push(P(dis2[e.to],e.to)); } } } } int main() { cin>>n>>r; int x; edge y; for(int i=0;i<r;i++) //建图(无向图)模板,,建立带权图板子(边上有属性的) { cin>>x>>y.to>>y.cost; x--; y.to--; mp[x].push_back(y); swap(y.to,x); mp[x].push_back(y); } dijkstra(n,0); cout<<dis2[n-1]<<endl;//因为是从0开始查的,,但是题目是从1开始 //cout<<dis[n-1]<<endl; //最短路 return 0; } //// 建立不带权板子 // // //for(int i=0;i<r;i++) // { // // cin>>x>>y; // x--;y--; // mp[x].push_back(y); // mp[y].push_back(x); // }
主要是题意难理解,