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
Two routes: 1 -> 2 -> 4 (length 100+200=300) and 1 -> 2 -> 3 -> 4 (length 100+250+100=450)
题意:求解1到n的第二短路
思路:用dijkstra()来做,dist用来保存最短路,dist2用来保存次短路,选出1到每一个点v的dist,与dist2 ,更新dist[v],与dist2[v]的值
AC代码:
#include <iostream>
#include <map>
#include <vector>
#include <cstdio>
#include <queue>
#include <algorithm>
using namespace std;
const int MAX_N = 5005;
const int INF = 0x7fffffff;
int n,r;
int dist[MAX_N];//最短距离
int dist2[MAX_N];//次短距离
typedef pair<int,int> P;
vector<P>G[MAX_N];
void dijistra() {
for(int i = 1; i <= n; i++) {
dist[i] = INF;
dist2[i] = INF;
}
priority_queue<P,vector<P>,greater<P> >que;
dist[1] = 0;
que.push(P(0,1));
while(!que.empty()) {
P p = que.top();
que.pop();
int index = p.second;
int d = p.first;
if(dist2[index] < d) {
continue;
}
for(int i = 0; i < G[index].size(); i++) {
int v = G[index][i].second;
int d2 = d + G[index][i].first;
if(dist[v] > d2) {
swap(dist[v],d2);
que.push(P(dist[v],v));
}
if(dist2[v] > d2 && d2 > dist[v]) {
dist2[v] = d2;
que.push(P(dist2[v],v));
}
}
}
}
int main() {
scanf("%d%d",&n,&r);
while(r--) {
int a,b,c;
scanf("%d%d%d",&a,&b,&c);
G[a].push_back(pair<int,int>(c,b));
G[b].push_back(pair<int,int>(c,a));
}
dijistra();
printf("%d\n",dist2[n]);
return 0;
}
AC代码2:
#include <iostream>
#include <map>
#include <vector>
#include <cstdio>
#include <queue>
#include <algorithm>
using namespace std;
const int MAX_N = 5005;
const int INF = 0x7fffffff;
typedef pair<int,int> P;//定义到目标节点 first为花费,second为目标节点
struct edge {
int to;
int cost;
};
int n,r;
vector<edge>G[MAX_N];//存图的大小
int dist[MAX_N];//最短距离
int dist2[MAX_N];//次短距离
void dijistra() {
priority_queue<P,vector<P>,greater<P> > que;//优先队列
for(int i = 1; i <= n; i++) {
dist[i] = INF;
dist2[i] = INF;
}//初始化
dist[1] = 0;
que.push(P(0,1));
while(!que.empty()) {
P p = que.top();
que.pop();
int v = p.second;
int d = p.first;
if(dist2[v] < d) {
continue;
}
for(int i = 0; i < G[v].size(); i++) {
edge e = G[v][i];
int d2 = d+e.cost;
if(dist[e.to] > d2) {
swap(dist[e.to],d2);
que.push(P(dist[e.to],e.to));
}
if(dist2[e.to] > d2 && dist[e.to] < d2) {
dist2[e.to] = d2;
que.push(P(dist2[e.to],e.to));
}
}
}
}
int main() {
scanf("%d%d",&n,&r);
while(r--) {
int a,b,c;
scanf("%d%d%d",&a,&b,&c);
edge e;
e.to = b;
e.cost = c;
G[a].push_back(e);
e.to = a;
e.cost = c;
G[b].push_back(e);
}
dijistra();
printf("%d\n",dist2[n]);
return 0;
}