POJ 3255 Roadblocks (dijkstra最短路)

Roadblocks
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 8382   Accepted: 3030

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:  AB, 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

dijkstra求次短路 先更新最短路 然后看次短路能否更新

AC代码如下:

//
//  POJ 3255 Roadblocks
//
//  Created by TaoSama on 2015-03-20
//  Copyright (c) 2015 TaoSama. All rights reserved.
//
#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <set>
#include <vector>
#define CLR(x,y) memset(x, y, sizeof(x))

using namespace std;
const int INF = 0x3f3f3f3f;
const int MOD = 1e9 + 7;
const int N = 1e5 + 10;

int n, r, dp[5005], dp2[5005];
struct Edge {
	int to, cost;
};
vector<Edge> G[N];
typedef pair<int, int> Sta; //距离, 节点标号

int dijkstra() {
	priority_queue<Sta, vector<Sta>, greater<Sta> > pq;
	memset(dp, 0x3f, sizeof dp); memset(dp2, 0x3f, sizeof dp2);
	dp[1] = 0; pq.push(Sta(0, 1));
	while(!pq.empty()) {
		Sta p = pq.top(); pq.pop();
		int u = p.second, d = p.first;
		if(d > dp2[u]) continue;
		for(int i = 0; i < G[u].size(); ++i) {
			Edge &e = G[u][i];
			int d2 = d + e.cost;
			if(dp[e.to] > d2) {
				swap(dp[e.to], d2);
				pq.push(Sta(dp[e.to], e.to));
			}
			if(dp2[e.to] > d2 && dp[e.to] < d2) {
				dp2[e.to] = d2;
				pq.push(Sta(dp2[e.to], e.to));
			}
		}
	}
	return dp2[n];
}

int main() {
#ifdef LOCAL
	freopen("in.txt", "r", stdin);
//	freopen("out.txt","w",stdout);
#endif
	//ios_base::sync_with_stdio(0);

	cin >> n >> r;
	for(int i = 1; i <= r; ++i) {
		int x, y, v; cin >> x >> y >> v;
		G[x].push_back((Edge) {y, v});
		G[y].push_back((Edge) {x, v});
	}
	cout << dijkstra() << endl;
	return 0;
}


你可能感兴趣的:(poj,dijkstra)