poj 2449 Remmarguts' Date (A*,k短路)
来源:互联网 发布:08最新电影源码 编辑:程序博客网 时间:2024/05/18 18:53
Description
给定一个有向图,求第k短路
Solution
考虑A*,
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;}
阅读全文
0 0
- POJ 2449 Remmarguts' Date (A*+K短路)
- poj 2449 Remmarguts' Date(第K短路 A*)
- poj 2449 Remmarguts' Date(A*求第K短路)
- POJ 2449 Remmarguts' Date(A*+SPFA)K短路问题
- poj 2449 Remmarguts' Date(K短路,A*算法)
- POJ 2449 Remmarguts' Date(A* - 第K短路)
- POJ 题目2449 Remmarguts' Date(K短路,A*+spfa)
- poj 2449 Remmarguts' Date(第K短路 A*)
- (poj 2449 Remmarguts' Date)<A*-K短路模板>
- poj 2449 Remmarguts' Date (A*,k短路)
- POJ 2449 Remmarguts' Date(k短路)
- poj 2449 Remmarguts' Date(K短路)
- poj 2449 Remmarguts' Date (k短路)
- POJ 2449 Remmarguts' Date(K短路)
- 【K短路】【A星】Remmarguts' Date POJ 2449 A-Star
- POJ 2449 Remmarguts' Date 第K短路 A* + SPFA
- poj 2449 Remmarguts' Date--k短路--spfa+A*
- poj 2449 Remmarguts' Date(K短路 Spfa+A*)
- kali ss代理
- 二叉树创建函数中一定要用指针的引用void createtree(BiTree *&T)或者用指针的指针void createtree(BiTree **T)
- 【数据库】NoSQL崛起原因
- 求二叉树两节点的最低公共节点
- OpenCV学习之六: 使用方向梯度直方图估计图像旋转角度
- poj 2449 Remmarguts' Date (A*,k短路)
- 软件需求工程 课堂笔记5
- C++11多线程原子类型
- Python小程序 -- 人民币小写转大写辅助工具
- HDU4596Yet another end of the world(扩展欧几里得)
- 1030. 完美数列(25)--PAT乙级
- 在windows10上安装tensorflow(GPU)
- java 实现排列组合Permutation and Combination和链式不相邻排列组合和环形不相邻排列
- HDU