题目链接

题意

给出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. CentOS6.5优盘安装

    从CentOS6.5开始直接把iso文件写入u盘就可实现优盘安装 windows平台:1.用UltraISO打开iso(如:CentOS-6.5-x86_64-bin-DVD1.iso)2.然后点“启 ...

  2. 早期malloc分配时,如果内存耗尽分配不出来,会直接返回NULL。现在分配不出来,直接抛出异常(可使用nothrow关键字)

    今天和同事review代码时,发现这样的一段代码: Manager * pManager = new Manager(); if(NULL == pManager) { //记录日志 return f ...

  3. C#异步委托等待句柄的使用

    using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threa ...

  4. wpf-MVVM界面自适应:界面自适应及字体自适应

    原文:wpf-MVVM界面自适应:界面自适应及字体自适应 1,界面自适应 界面先划分Region,每个填充Region的View不设置Width属性,即可按照Region划分的比例,自适应屏幕分辨率 ...

  5. dotnet core 跨平台编译发布

    vs2017 建立的项目,在项目目录 ,执行 dotnet publish -r ubuntu.15.04-x64 dotnet publish -r linux-x64 dotnet publish ...

  6. C、C++笔记

    2017年6月 阅读书籍<C和指针> #if 0 #endif 比注释掉代码好.(<C和指针>2017.06.07) 全局变量和全局静态变量的区别 1)全局变量是不显式用sta ...

  7. 什么是TOML?

    配置文件的使用由来已久,从.ini.XML.JSON.YAML再到TOML,语言的表达能力越来越强,同时书写便捷性也在不断提升. TOML是前GitHub CEO, Tom Preston-Werne ...

  8. 有未经处理的异常(在 xx.exe 中): 堆栈 Cookie 检测代码检测到基于堆栈的缓冲区溢出。

    一般这个问题是数组越界. 我产生这个异常的代码是这句:memcpy(tmp_cert.byKey, m_row[2], 255); 255的长度超过了char数组tmp_cert.byKey的长度.

  9. Android零基础入门第61节:滚动视图ScrollView

    原文:Android零基础入门第61节:滚动视图ScrollView 前面几期学习了ProgressBar系列组件.ViewAnimator系列组件.Picker系列组件和时间日期系列组件,接下来几期 ...

  10. ACL FAQ

    acl 下载地址:https://sourceforge.net/projects/acl/https://github.com/zhengshuxin/acl/http://git.oschina. ...