题目链接:http://poj.org/problem?id=2449

题目:

题意:求有向图两点间的k短路。

思路:最短路+A*算法

代码实现如下:

 #include <set>
#include <map>
#include <queue>
#include <stack>
#include <cmath>
#include <bitset>
#include <cstdio>
#include <string>
#include <vector>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std; typedef long long ll;
typedef unsigned long long ull; #define lson i<<1
#define rson i<<1|1
#define bug printf("*********\n");
#define FIN freopen("D://code//in.txt", "r", stdin);
#define debug(x) cout<<"["<<x<<"]" <<endl;
#define IO ios::sync_with_stdio(false),cin.tie(0); const double eps = 1e-;
const int mod = ;
const int maxn = 1e5 + ;
const double pi = acos(-);
const int inf = 0x3f3f3f3f;
const ll INF = 0x3f3f3f3f3f3f3f; inline int read() {//读入挂
int ret = , c, f = ;
for(c = getchar(); !(isdigit(c) || c == '-'); c = getchar());
if(c == '-') f = -, c = getchar();
for(; isdigit(c); c = getchar()) ret = ret * + c - '';
if(f < ) ret = -ret;
return ret;
} int n, m, s, t, u, v, w, k, tot;
int d[maxn<<], cnt[maxn<<], vis[maxn<<];
int head[maxn<<], head1[maxn<<];
pair<int, int> P; struct b {
int v, w, next;
}biao[maxn<<]; struct node {
int g, h;
int to;
bool operator < (node a) const {
return (a.h + a.g) < h + g;
}
}; void add(int u, int v, int w) {
biao[tot].v = v;
biao[tot].w = w;
biao[tot].next = head[u];
head[u] = tot++; biao[tot].v = u;
biao[tot].w = w;
biao[tot].next = head1[v];
head1[v] = tot++;
} void init() {
tot = ;
memset(head, -, sizeof(head));
memset(head1, -, sizeof(head1));
} void spfa() {
memset(vis, , sizeof(vis));
memset(d, inf, sizeof(d));
d[t] = ;
vis[t] = ;
queue<int> q;
q.push(t);
while(!q.empty()) {
int u = q.front();
q.pop();
vis[u] = ;
for(int i = head1[u]; i != -; i = biao[i].next) {
int v = biao[i].v;
if(d[v] > d[u] + biao[i].w) {
d[v] = d[u] + biao[i].w;
if(!vis[v]) {
q.push(v);
vis[v] = ;
}
}
}
}
} int AA() {
memset(cnt, , sizeof(cnt));
priority_queue<node> Q;
node p, q;
p.g = ;
p.h = d[s];
p.to = s;
Q.push(p);
while(!Q.empty()) {
p = Q.top(), Q.pop();
cnt[p.to]++;
if(cnt[p.to] > k) continue;
if(cnt[t] == k) return p.g;
int u = p.to;
for(int i = head[u]; i != -; i = biao[i].next) {
int v = biao[i].v;
q.to = v;
q.g = p.g + biao[i].w;
q.h = d[v];
Q.push(q);
}
}
return -;
} int main() {
//FIN;
scanf("%d%d", &n, &m);
init();
while(m--) {
scanf("%d%d%d", &u, &v, &w);
add(u, v, w);
}
scanf("%d%d%d", &s, &t, &k);
spfa();
if(s == t) k++;
int ans = AA();
printf("%d\n", ans);
return ;
}

Remmarguts' Date(POJ2449+最短路+A*算法)的更多相关文章

  1. POJ 2449 Remmarguts' Date ( 第 k 短路 && A*算法 )

    题意 : 给出一个有向图.求起点 s 到终点 t 的第 k 短路.不存在则输出 -1 #include<stdio.h> #include<string.h> #include ...

  2. poj 2449 Remmarguts' Date(K短路,A*算法)

    版权声明:本文为博主原创文章.未经博主同意不得转载. https://blog.csdn.net/u013081425/article/details/26729375 http://poj.org/ ...

  3. poj 2449 Remmarguts' Date 第k短路 (最短路变形)

    Remmarguts' Date Time Limit: 4000MS   Memory Limit: 65536K Total Submissions: 33606   Accepted: 9116 ...

  4. poj 2449 Remmarguts' Date (k短路模板)

    Remmarguts' Date http://poj.org/problem?id=2449 Time Limit: 4000MS   Memory Limit: 65536K Total Subm ...

  5. 【POJ】2449.Remmarguts' Date(K短路 n log n + k log k + m算法,非A*,论文算法)

    题解 (搬运一个原来博客的论文题) 抱着板题的心情去,结果有大坑 就是S == T的时候也一定要走,++K 我发现按照论文写得\(O(n \log n + m + k \ log k)\)算法没有玄学 ...

  6. POJ2449 Remmarguts' Date 第K短路

    POJ2449 比较裸的K短路问题 K短路听起来高大上 实际思路并不复杂 首先对终点t到其他所有点求最短路 即为dist[] 然后由起点s 根据当前走过的距离+dist[]进行A*搜索 第k次到达t即 ...

  7. POJ 2449 - Remmarguts' Date - [第k短路模板题][优先队列BFS]

    题目链接:http://poj.org/problem?id=2449 Time Limit: 4000MS Memory Limit: 65536K Description "Good m ...

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

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

  9. POJ 2449 Remmarguts' Date (SPFA + A星算法) - from lanshui_Yang

    题目大意:给你一个有向图,并给你三个数s.t 和 k ,让你求从点 s 到 点 t 的第 k 短的路径.如果第 k 短路不存在,则输出“-1” ,否则,输出第 k 短路的长度. 解题思路:这道题是一道 ...

随机推荐

  1. Cstring, TCHAR*, char*的转换

    最近老用到Cstring, TCHAR*, char*的转换. 找到一篇写得蛮详细的. 引用过来, 方便自己以后查阅. char是类型TCHAR也是!不过他可以通过是否定义了UNICODE宏来判断到底 ...

  2. Spring Boot(二)配置分析

    回顾一下采用SSM开发项目时,项目中会存在多个配置文件,比如web.xml,配置Spring相关的applicationContext-springmvc.xml, applicationContex ...

  3. 《Effective C#》快速笔记(三)- 使用 C# 表达设计

    目录 二十一.限制类型的可见性 二十二.通过定义并实现接口替代继承 二十三.理解接口方法和虚方法的区别 二十四.用委托实现回调 二十五.用事件模式实现通知 二十六.避免返回对内部类对象的引用 二十七. ...

  4. Tiny4412 LED 程序

    package cn.hyc.led; import android.os.Bundle; import android.app.Activity; import android.view.Menu; ...

  5. c++内存分类

    1. 代码段:放置代码 2. 静态数据段:放置全局变量和static的局部变量,字符串常量 3. 动态数据段:栈,放置局部作用域的变量,离开函数返回后就会被释放:堆,必须手动的分配和释放. 关于字符串 ...

  6. BZOJ 1015 星球大战(并查集)

    正着不好搞,考虑倒着搞.倒着搞就是一个并查集. # include <cstdio> # include <cstring> # include <cstdlib> ...

  7. echart模块化单文件引入

    echart模块化单文件引入百度上面是推荐这样使用.今天看了一下,做了个Demo. 文件结构如下:

  8. 用select模拟一个socket server成型版2

    1.字典队列测试 import queue msg_dic={} msg_dic[1]=queue.Queue() msg_dic[1].put('hello') msg_dic[1].put('bo ...

  9. 源码安装和yum安装的区别。

    yum是将yum源中别人已经编译好的rpm包下载到本地,然后安装,不需要考虑依赖,主要是方便.源码安装没法人为的控制,安装的版本也很低. 源码安装需要自己编译,安装,编译过程中可以设置参数.可安装的版 ...

  10. ZOJ1081:Points Within——题解

    http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1081 题目大意:给定一个点数为 n 的多边形,点按照顺序给出,再给出 m ...