原题链接
Roadblocks
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 12531 Accepted: 4429
Description
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
Line 1: Two space-separated integers: N and R
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
Line 1: The length of the second shortest path between node 1 and node N
Sample Input
4 4
1 2 100
2 4 200
2 3 250
3 4 100
Sample Output
450
Hint
Two routes: 1 -> 2 -> 4 (length 100+200=300) and 1 -> 2 -> 3 -> 4 (length 100+250+100=450)
Source
USACO 2006 November Gold
使用堆优化的dijkstra算法
#include <algorithm>
#include <iostream>
#include <sstream>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <vector>
#include <queue>
using namespace std;
const int MOD = int(1e9) + 7;
//int MOD = 99990001;
const int INF = 0x3f3f3f3f;
//const LL INFF = 0x3f3f3f3f3f3f3f3fLL;
//const DB EPS = 1e-9;
//const DB OO = 1e20;
//const DB PI = acos(-1.0); //M_PI;
const int fx[] = {-1, 1, 0, 0};
const int fy[] = {0, 0, -1, 1};
const int maxn=5000 + 5;
struct edge{int to,cost;};//构造一种表示边的结构
typedef pair<int,int> p;//first记录最短的距离,second保存对应的编号
int n,r;//n代表点的个数,r代表边的个数
vector<edge> E[maxn];//存储与其相连的所有边
int d[maxn],d2[maxn];//记录最短路与次短路
void dijkstra(){
//首先把距离全部初始化为INF
fill(d,d+n,INF);
fill(d2,d2+n,INF);
d[0]=0;//从0到0的路径长度是0
priority_queue< p,vector<p>,greater<p> > que;//使用堆来存储当前可以更新的那些最短路,这样每次取出的都是最短的路径
que.push(p(0,0));
while(!que.empty()){
p now=que.top();que.pop();
int dist=now.first,v=now.second;//得到当前的代表最短的距离的那个点,更新当前的点所连接的所有的点的最短的距离
if(dist>d2[v]) continue;//如果没有必要更新那么久不更新了
int len=E[v].size();
for(int i=0;i<len;i++){
edge t=E[v][i];
int newdist = t.cost + dist;
if(newdist<d[t.to]){
swap(newdist,d[t.to]);
que.push(p(d[t.to],t.to));
}
if(newdist<d2[t.to] && newdist>d[t.to]){
swap(newdist,d2[t.to]);
que.push(p(d2[t.to],t.to));
}
}
}
return;
}
int main(){
cin >> n >> r;
while(r--){
int x,y,val;
scanf("%d%d%d",&x,&y,&val);
x--;y--;//从点0到点n-1
E[x].push_back( (edge){y,val} );
E[y].push_back( (edge){x,val} );
}
dijkstra();
cout << d2[n-1] << endl;
return 0;
}