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)
题目大意:
求次短路
题解:
通过Dijkstra算法我们求得最短路, 到某个顶点v的次短路要么时到其他某个顶点u的最短路再加上u->v的边, 要么是到u的次短率再加上u->v的边, 因此所需要求的就是到所有顶点的最短路和次短路. 因此, 对于每个顶点,我么记录的不仅仅是最短距离, 还有次短的距离. 接下去只要用与Dijkstra算法相同的做法, 不断更新这两个距离就可以求出次短路了.
设d[v]为s到v的最短距离, dd[v]为s到v的次短距离
d[v] = min(d[v], d[u] + w[u, v])
dd[v] = d[u] + w[u, v] 或者 dd[v] = dd[u] + w[u, v]
每次做松弛操作的时候,如果能更新d[v],则dd[v] = d[v] ,然后更新d[v], 如果只能更新dd[v],则只更新dd[v]
代码:
#include <iostream>
#include <vector>
#include <set>
#include <queue>
#define INF 0x3f3f3f3f
using namespace std;
typedef pair<int, int> Pair;
class Graph {
int V;
vector<Pair> *adj;
vector<int> *dist;
vector<int> *dist2;
public:
Graph(int V);
void addEdge(int u, int v, int w);
int secondShortestPath(int src);
};
Graph::Graph(int V) {
this->V = V;
adj = new vector<Pair>[V];
}
void Graph::addEdge(int u, int v, int w) {
adj[u].push_back(make_pair(v, w));
}
template<class Type> inline void Swap(Type &a, Type &b) {
Type tmp = a; a = b; b = tmp;
}
int Graph::secondShortestPath(int src) {
dist = new vector<int>(V, INF);
dist2 = new vector<int>(V, INF);
dist->at(src) = 0;
priority_queue<Pair, vector<Pair>, greater<Pair> > p;
p.push(make_pair(0, src));
while (!p.empty()) {
Pair tmp = p.top();
p.pop();
int u = tmp.second;
int d1 = tmp.first; //这里需要用队列中的值来松弛,才能保证用到dist2[u]的值
vector<Pair>::iterator i;
for (i = adj[u].begin(); i != adj[u].end(); i++) {
int v = i->first;
int w = i->second;
int d2 = d1 + w;
//如果dist dist2都要更新
if (dist->at(v) > d2) {
Swap(dist->at(v), d2);
p.push(make_pair(dist->at(v), v));
}
//只需要更新 dist2
if (dist2->at(v) > d2 && dist->at(v) < d2) {
dist2->at(v) = d2;
p.push(make_pair(dist2->at(v), v));
}
}
}
return dist2->at(V-1);
}
int main() {
int N, R;
int u, v, w;
cin >> N >> R;
Graph G(N);
for (int i = 0; i < R; i++) {
cin >> u >> v >> w;
u--; v--;
G.addEdge(u, v, w);
G.addEdge(v, u, w);
}
cout << G.secondShortestPath(0) << endl;
return 0;
}