【题目链接】
http://poj.org/problem?id=2449
【算法】
A*(启发式搜索)
首先,求第k短路可以用优先队列BFS实现,当T第k次入队时,就求得了第k短路,但是,这种做法的复杂度太高
考虑使用A*算法,每个点的估价函数就是这个点到T的最短路,不妨将所有的边反过来求最短路,这样就得到了所有点的估价函数
这种算法的复杂度是优秀的
【代码】
#include <algorithm> #include <bitset> #include <cctype> #include <cerrno> #include <clocale> #include <cmath> #include <complex> #include <cstdio> #include <cstdlib> #include <cstring> #include <ctime> #include <deque> #include <exception> #include <fstream> #include <functional> #include <limits> #include <list> #include <map> #include <iomanip> #include <ios> #include <iosfwd> #include <iostream> #include <istream> #include <ostream> #include <queue> #include <set> #include <sstream> #include <stdexcept> #include <streambuf> #include <string> #include <utility> #include <vector> #include <cwchar> #include <cwctype> #include <stack> #include <limits.h> using namespace std; #define MAXN 1010 #define MAXM 100010 const int INF = 2e9; int i,n,m,tot,rtot,u,v,w,S,T,K; int head[MAXN],rhead[MAXN],dist[MAXN]; struct Edge { int to,w,nxt; } e[MAXM<<1]; struct info { int s,d; friend bool operator < (info a,info b) { return a.d + dist[a.s] > b.d + dist[b.s]; } } ; inline void add(int u,int v,int w) { tot++; e[tot] = (Edge){v,w,head[u]}; head[u] = tot; tot++; e[tot] = (Edge){u,w,rhead[v]}; rhead[v] = tot; } inline void dijkstra(int s) { int i,v,w; priority_queue< pair<int,int> > q; pair<int,int> cur; static bool visited[MAXN]; while (!q.empty()) q.pop(); for (i = 1; i <= n; i++) { dist[i] = INF; visited[i] = false; } dist[s] = 0; q.push(make_pair(0,s)); while (!q.empty()) { cur = q.top(); q.pop(); if (visited[cur.second]) continue; visited[cur.second] = true; for (i = rhead[cur.second]; i; i = e[i].nxt) { v = e[i].to; w = e[i].w; if (dist[cur.second] + w < dist[v]) { dist[v] = dist[cur.second] + w; q.push(make_pair(-dist[v],v)); } } } } inline int Astar(int s,int t,int k) { int i,cnt = 0,v,w; priority_queue< info > q; info cur; while (!q.empty()) q.pop(); q.push((info){s,0}); while (!q.empty()) { cur = q.top(); q.pop(); if (cur.s == t) { cnt++; if (cnt == k) return cur.d; } for (i = head[cur.s]; i; i = e[i].nxt) { v = e[i].to; w = e[i].w; q.push((info){v,cur.d+w}); } } return -1; } int main() { while (scanf("%d%d",&n,&m) != EOF) { tot = rtot = 0; for (i = 1; i <= n; i++) head[i] = rhead[i] = 0; for (i = 1; i <= m; i++) { scanf("%d%d%d",&u,&v,&w); add(u,v,w); } scanf("%d%d%d",&S,&T,&K); if (S == T) K++; dijkstra(T); printf("%d ",Astar(S,T,K)); } return 0; }