题目链接: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. TCP系列14—重传—4、Karn算法和TSOPT的RTTM

    一.Karn算法 在RTT采样测量过程中,如果一个数据包初传后,RTO超时重传,接着收到这个数据包的ACK报文,那么这个ACK报文是对应初传TCP报文还是对应重传TCP报文呢?这个问题就是retran ...

  2. <Effective C++>读书摘要--Designs and Declarations<一>

    <Item 18> Make interfaces easy to use correctly and hard to use incorrectly 1.That being the c ...

  3. 每天网络半小时(MAC数据包在哪里合并的)

    ip_deliver_local函数中函数中完成合并 听过netfilter框架中也会 因为net_filter框架需要感知到第四层的信息,但是单个数据包是无法感知到这些信息的,所以需要在netfil ...

  4. MyBatis事务管理机制

    MyBatis作为Java语言的数据库框架,对数据库的事务管理是其非常重要的一个方面.   本文将讲述MyBatis的事务管理的实现机制,首先介绍MyBatis的事务Transaction的接口设计以 ...

  5. vs code 自动补全效果不理想的问题

    之前一直用webstorm,最近换换口味,改用了VS Code,发现VS Code 智能提示得到的都不是我想要的 就比如  ! + tab ,HTML结构都出不来.经过一番搜索,发现是 VS Code ...

  6. init只创建一次 只有父类的init创建servletContext的对象

    init只创建一次 只有父类的init创建servletContext的对象  如果重写父类的方法 但不显示调用父类的init 是不会创建servletContext对象的

  7. [AT2164] [agc006_c] Rabbit Exercise

    题目链接 AtCoder:https://agc006.contest.atcoder.jp/tasks/agc006_c 洛谷:https://www.luogu.org/problemnew/sh ...

  8. ocker nginx 配置反向代理和负载均衡

    1. 获取及配置nginx 如果需要全站通过docker部署,那么nginx或许是不可或缺的.通过配置nginx,可以迅速实现负载均衡和反向代理服务.值得一提的是,docker官网恰好也有nginx镜 ...

  9. UOJ117:欧拉回路——题解

    http://uoj.ac/problem/117 (作为一道欧拉回路的板子题,他成功的令我学会了欧拉回路) (然而我不会背……) 就两件事: 1.无向图为欧拉图,当且仅当为连通图且所有顶点的度为偶数 ...

  10. Static全局变量与普通的全局变量有什么区别?static函数与普通函数有什么区别?

    Static全局变量与普通的全局变量有什么区别? 答: 全局变量(外部变量)的说明之前再冠以static就构成了静态的全局变量.全局变量本身就是静态存储方式,静态全局变量当然也是静态存储方式. 这两者 ...