poj 2449 Remmarguts' Date (A*,k短路)
2017-10-01 22:01
330 查看
Description
给定一个有向图,求第k短路Solution
考虑A*,f(x)=g(x)+h(x)f(x)=g(x)+h(x)中,g(x)表示到达当前点的代价,h(x)表示到达终点的最小可能的花费(小于等于实际花费),其中h(x)可以将边反向后SPFA得到,g(x)由搜索时递推得到Code
#include<cstdio> #include<cstdlib> #include<cmath> #include<cstring> #include<iostream> #include<algorithm> #include<queue> #include<vector> #include<set> #define For(i , j , k) for (int i = (j) , _##end_ = (k) ; i <= _##end_ ; ++ i) #define Fordown(i , j , k) for (int i = (j) , _##end_ = (k) ; i >= _##end_ ; -- i) #define Set(a , b) memset(a , b , sizeof(a)) #define pb push_back #define mp make_pair #define x first #define y second #define INF (0x3f3f3f3f) #define Mod (1000000007) using namespace std; typedef long long LL; typedef pair<int , int> PII; template <typename T> inline bool chkmax(T &a , T b) { return a < b ? (a = b , 1) : 0; } template <typename T> inline bool chkmin(T &a , T b) { return b < a ? (a = b , 1) : 0; } int _ , __; char c_; inline int read() { for (_ = 0 , __ = 1 , c_ = getchar() ; !isdigit(c_) ; c_ = getchar()) if (c_ == '-') __ = -1; for ( ; isdigit(c_) ; c_ = getchar()) _ = (_ << 1) + (_ << 3) + (c_ ^ 48); return _ * __; } inline void file() { #ifdef hany01 freopen("poj2449.in" , "r" , stdin); freopen("poj2449.out" , "w" , stdout); #endif } const int maxn = 1010 , maxm = 100010; int n , m , s , t , k , beg[maxn] , nex[maxm] , v[maxm] , w[maxm] , dis[maxn] , beg1[maxn] , nex1[maxm] , v1[maxm] , w1[maxm] , e , e1 , times[maxn] , Ans; bool vis[maxn]; queue<int> q; inline void add(int uu , int vv , int ww) { v[++ e] = vv; w[e] = ww; nex[e] = beg[uu]; beg[uu] = e; } inline void add1(int uu , int vv , int ww)//反向边 { v1[++ e1] = vv; w1[e1] = ww; nex1[e1] = beg1[uu]; beg1[uu] = e1; } inline void Init() { int uu , vv , ww; n = read(); m = read(); For(i , 1 , m) uu = read(), vv = read(), ww = read(), add(uu , vv , ww), add1(vv , uu , ww); s = read(); t = read(); k = read(); if (s == t)//坑:当起点等于终点时,最短路径长度为0,但不能计入 ++ k; } inline void SPFA()//预处理g(x) { For(i , 1 , n) dis[i] = INF; dis[t] = 0; vis[t] = true; q.push(t); while (!q.empty()) { int u = q.front(); q.pop(); vis[u] = false; for (int i = beg1[u] ; i ; i = nex1[i]) if (chkmin(dis[v1[i]] , dis[u] + w1[i])) if (!vis[v1[i]]) { vis[v1[i]] = true; q.push(v1[i]); } } } struct Item { int u , g , h; bool operator < (const Item &item) const { return g + h > item.g + item.h; } }; priority_queue<Item> qi;//优先队列保存最优解 inline void Astar() { Item u; u.u = s; u.g = 0; u.h = dis[s]; qi.push(u); while (!qi.empty()) { u = qi.top(); qi.pop(); ++ times[u.u];//记录这是第几短路 if (times[u.u] > k)//若超过k次,显然毫无意义 continue; if (times[u.u] == k && u.u == t)//找到解,退出 { Ans = u.h + u.g; return ; } for (int i = beg[u.u] ; i ; i = nex[i]) qi.push((Item){v[i] , u.g + w[i] , dis[v[i]]}); } } int main() { file(); Init(); SPFA(); Ans = -1; Astar(); printf("%d\n" , Ans); return 0; }
相关文章推荐
- POJ 题目2449 Remmarguts' Date(K短路,A*+spfa)
- POJ 2449 Remmarguts' Date(k短路)
- POJ 2449 Remmarguts' Date(K短路)
- POJ 2449 Remmarguts' Date(k短路)
- Remmarguts' Date POJ - 2449 第k短路 SPFA+A*
- poj 2449 Remmarguts' Date (k短路)
- poj 2449Remmarguts' Date uvaoj 10740 not the best dijkstra或spfa或bellman-ford k短路 A*
- poj 2449 Remmarguts' Date(K短路)
- POJ 2449 Remmarguts' Date -K短路
- poj 2449 Remmarguts' Date 求第k短路(SPFA+A*)
- 搜索 ( 第K短路, A*搜索)——Remmarguts' Date ( POJ 2449 )
- POJ 2449 Remmarguts' Date ( Dijkstra + A* 求解第K短路 )
- POJ 2449 Remmarguts' Date 第K短路 A*
- 第k短路(POJ 2449: Remmarguts' Date)
- POJ:2449-Remmarguts' Date(单源第K短路)
- poj2449 Remmarguts' Date,第K短路
- poj--2449 Remmarguts' Date(k短路)
- POJ 2449 Remmarguts' Date (求第K短路,A* + Dijkstra)
- POJ--2449--Remmarguts' Date【dijkstra_heap+A*】第K短路
- poj 2449 Remmarguts' Date(第K短路 A*)