题目链接

题意

给出n个点m条有向边,源点s,汇点t,k。问s到t的第k短路的路径长度是多少,不存在输出-1.

思路

A*算法是启发式搜索,通过一个估价函数 f(p) = g(p) + h(p) ,其中源点到p的距离是g(p),从p到汇点的距离是h(p),从源点经过p点到达汇点的长度f(p),来决定搜索的方向。

因此反向建图,从汇点出发处理出h数组,然后就可以用A*来做了。用优先队列,每次出队的点为t的话,就cnt++,当cntk的时候,就说明是第k短路了。

注意一开始st的情况,因为要跑出路径,所以要k++。

#include <cstdio>
#include <cstdlib>
#include <queue>
#include <cstring>
using namespace std;
const int N = 1011;
const int M = 100011;
const int INF = 0x3f3f3f3f;
struct Edge {
int v, nxt, w;
} edge[M];
struct Node {
int u, g, h;
friend bool operator < (const Node &a, const Node &b) {
return a.g + a.h > b.h + b.g;
}
};
int n, m, h[N], vis[N], s, t, k, U[M], V[M], W[M], head[N], tot; void Add(int u, int v, int w) {
edge[tot] = (Edge) { v, head[u], w }; head[u] = tot++;
} void Spfa() {
memset(h, INF, sizeof(h));
memset(vis, 0, sizeof(vis));
queue<int> que; que.push(t);
vis[t] = 1; h[t] = 0;
while(!que.empty()) {
int u = que.front(); que.pop();
vis[u] = 0;
for(int i = head[u]; ~i; i = edge[i].nxt) {
int v = edge[i].v, w = edge[i].w;
if(h[v] > h[u] + w) {
h[v] = h[u] + w;
if(!vis[v]) vis[v] = 1, que.push(v);
}
}
}
} int Astar() {
if(h[s] == INF) return -1;
if(s == t) k++;
Node now = (Node) { s, 0, h[s] };
priority_queue<Node> que; que.push(now);
int cnt = 0;
while(!que.empty()) {
now = que.top(); que.pop();
int u = now.u, gg = now.g, hh = now.h;
// printf("%d : %d - %d\n", u, gg, hh);
if(u == t) cnt++;
if(cnt == k) return gg + hh;
for(int i = head[u]; ~i; i = edge[i].nxt) {
int v = edge[i].v, w = edge[i].w;
now = (Node) { v, gg + w, h[v] };
que.push(now);
}
}
return -1;
} int main() {
while(~scanf("%d%d", &n, &m)) {
memset(head, -1, sizeof(head)); tot = 0;
for(int i = 1; i <= m; i++) {
scanf("%d%d%d", &U[i], &V[i], &W[i]);
Add(V[i], U[i], W[i]);
}
scanf("%d%d%d", &s, &t, &k);
Spfa();
memset(head, -1, sizeof(head)); tot = 0;
for(int i = 1; i <= m; i++)
Add(U[i], V[i], W[i]);
printf("%d\n", Astar());
}
return 0;
}

POJ 2449:Remmarguts' Date(A* + SPFA)的更多相关文章

  1. POJ——2449Remmarguts' Date(A*+SPFA)

    Remmarguts' Date Time Limit: 4000MS   Memory Limit: 65536K Total Submissions: 26504   Accepted: 7203 ...

  2. HDU 6181:Two Paths(A* + SPFA)

    题目链接 题意 给出n个点m条边的无向图,求次短路. 思路 和 POJ 2449 类似,只不过大小要开成long long. #include <bits/stdc++.h> using ...

  3. POJ 2796:Feel Good(单调栈)

    http://poj.org/problem?id=2796 题意:给出n个数,问一个区间里面最小的元素*这个区间元素的和的最大值是多少. 思路:只想到了O(n^2)的做法. 参考了http://ww ...

  4. POJ 3318:Matrix Multiplication(随机算法)

    http://poj.org/problem?id=3318 题意:问A和B两个矩阵相乘能否等于C. 思路:题目明确说出(n^3)的算法不能过,但是通过各种常数优化还是能过的. 这里的随机算法指的是随 ...

  5. POJ 1200:Crazy Search(哈希)

    Crazy Search Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 32483   Accepted: 8947 Des ...

  6. 【POJ】2449 Remmarguts' Date(k短路)

    http://poj.org/problem?id=2449 不会.. 百度学习.. 恩. k短路不难理解的. 结合了a_star的思想.每动一次进行一次估价,然后找最小的(此时的最短路)然后累计到k ...

  7. 【POJ 2449】 Remmarguts' Date

    [题目链接] http://poj.org/problem?id=2449 [算法] A*(启发式搜索) 首先,求第k短路可以用优先队列BFS实现,当T第k次入队时,就求得了第k短路,但是,这种做法的 ...

  8. POJ 3026 : Borg Maze(BFS + Prim)

    http://poj.org/problem?id=3026 Borg Maze Time Limit: 1000MS   Memory Limit: 65536K Total Submissions ...

  9. POJ 3301:Texas Trip(计算几何+三分)

    http://poj.org/problem?id=3301 题意:在二维平面上有n个点,每个点有一个坐标,问需要的正方形最小面积是多少可以覆盖所有的点. 思路:从第二个样例可以看出,将正方形旋转45 ...

随机推荐

  1. Gradle 1.12 翻译——第九章 Groovy高速入口

    由于时间.没办法,做笔记和翻译的同时,大约Gradle用户指南.本博客不再做相关的注意事项.而仅仅翻译和本出版物中未翻译章节. 有关其他章节翻译请注意Github该项目:https://github. ...

  2. 系统引导文件之 boot.ini(有很多参数)

    Windows NT类的操作系统,也就是Windows NT/2000/XP中,有一个特殊文件,也就是“BOOT.INI”文件,这个文件会很轻松地按照我们的需求设置好多重启动系统. “BOOT.INI ...

  3. 简化连接Buffer对象的过程

    上述一大段代码仅只完成了一件事情,就是连接多个Buffer对象,而这种场景需求将会在多个地方发生,所以,采用一种更优雅的方式来完成该过程是必要的.笔者基于以上的代码封装出一个bufferhelper模 ...

  4. ItemsPanelTemplate

    用以定义集合控件的容器外观,如ListBox,Combox 等等使用一个自定义的ListBox用以说明,其默认外观是上下排列,这里修改成横向排列 <Window.Resources> &l ...

  5. ControlTemplate

    ControlTemplate:外观定制 <Window.Resources> <ControlTemplate x:Key="CheckBoxControlTemplat ...

  6. WPF中的文字修饰——上划线,中划线,基线与下划线

    原文:WPF中的文字修饰——上划线,中划线,基线与下划线 我们知道,文字的修饰包括:空心字.立体字.划线字.阴影字.加粗.倾斜等.这里只说划线字的修饰方式,按划线的位置,我们可将之分为:上划线.中划线 ...

  7. wpf 禁用window的systemmenu

    private IntPtr WidProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled) { if (m ...

  8. .NET与Java互通AES算法加密解密

    /// <summary>AES加密</summary> /// <param name="text">明文</param> /// ...

  9. centos7安装 lamp

    1.安装apache yum install httpd #根据提示,输入Y安装即可成功安装 systemctl start httpd.service #启动apache systemctl sto ...

  10. SQL Server 游标运用:查看所有数据库所有表大小信息(Sizes of All Tables in All Database)

    原文:SQL Server 游标运用:查看所有数据库所有表大小信息(Sizes of All Tables in All Database) 一.本文所涉及的内容(Contents) 本文所涉及的内容 ...