poj 3255 Roadblocks (次短路 A星演算法)
題目連結:http://poj.org/problem?id=3255
Language:Default
Roadblocks
Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 20256 Accepted: 7107 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 100Sample Output
450Hint
Two routes: 1 -> 2 -> 4 (length 100+200=300) and 1 -> 2 -> 3 -> 4 (length 100+250+100=450)
一道A星演算法的模板題
#include <iostream> #include <cstdio> #include <cstdlib> #include <algorithm> #include <cstring> #include <cmath> #include <stack> #include <vector> #include <queue> #include <map> #include <climits> #include <cassert> #define LL long long using namespace std; const int inf = 0x3f3f3f3f; const int maxn = 5000 + 1; const int maxm = 100000 + 1; int n, m; int st, ed; int tot; int head[maxn]; struct Edge { int to, cost, nxt; } e[maxm << 1]; void init() { memset(head, -1, sizeof(head)); tot = 0; } void addEdge(int fr, int to, int cost) { e[tot].to = to; e[tot].cost = cost; e[tot].nxt = head[fr]; head[fr] = tot++; } int dis[maxn]; bool vis[maxn]; void spfa() { for (int i = 1; i <= n; i++) { dis[i] = inf; vis[i] = false; } priority_queue<int> q; q.push(ed); vis[ed] = true; dis[ed] = 0; while (!q.empty()) { int cur = q.top(); q.pop(); for (int i = head[cur]; i != -1; i = e[i].nxt) { int x = e[i].to; if (dis[cur] + e[i].cost < dis[x]) { dis[x] = dis[cur] + e[i].cost; if (!vis[x]) { vis[x] = true; q.push(x); } } } vis[cur] = false; } } struct Node { int pos; int h, g; bool operator < (const Node a) const { return a.h + a.g < h + g; } }; int Astar(int k) { int cnt = 0; if (st == ed) k++; if (dis[st] == inf) return -1; priority_queue<Node> q; Node now, nxt; now.pos = st, now.g = 0, now.h = dis[st]; q.push(now); while (!q.empty()) { nxt = q.top(); q.pop(); if (nxt.pos == ed) { cnt++; if (cnt == k) return nxt.g; } for (int i = head[nxt.pos]; i != -1; i = e[i].nxt) { now.pos = e[i].to; now.g = nxt.g + e[i].cost; now.h = dis[e[i].to]; q.push(now); } } return 0; } int main() { //freopen("C://input.txt", "r", stdin); while (~scanf("%d%d", &n, &m)) { init(); while (m--) { int fr, to, cost; scanf("%d%d%d", &fr, &to, &cost); addEdge(fr, to, cost); addEdge(to, fr, cost); } st = 1; ed = n; spfa(); printf("%d\n", Astar(2)); } return 0; }