题目链接: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. LintCode-211.字符串置换

    字符串置换 给定两个字符串,请设计一个方法来判定其中一个字符串是否为另一个字符串的置换. 置换的意思是,通过改变顺序可以使得两个字符串相等. 样例 "abc" 为 "cb ...

  2. RXSwift --UITableView之初探

    对于RXSwift中的一些基本概念和说明请参看其他文章,接下来我们使用RXSwift一步一步去构建TableView,从简单到复杂.iOS开发过程中tableView的使用率是最高的,他的一些代理方法 ...

  3. Sqoop使用笔记(转载)

    Sqoop是Apache顶级项目,主要用来在Hadoop和关系数据库中传递数据.通过sqoop,可以方便的将数据从关系数据库导入到HDFS,或将数据从HDFS导出到关系数据库. 关于Sqoop 官网S ...

  4. 《Effective C#》快速笔记(二)- .NET 资源托管

    简介 续 <Effective C#>读书笔记(一)- C# 语言习惯. .NET 中,GC 会帮助我们管理内存,我们并不需要去担心内存泄漏,资源分配和指针初始化等问题.不过,它也并非万能 ...

  5. Kafka Strem

    Overview Concepts Topology Time States Window Hopping time windows Tumbling time windows Sliding win ...

  6. Lucene笔记一

    Lucene就是一个全文检索的工具,建立索引用的,类似于新华字典的目录 这里使用的是lucene-4.4.0版本,入门代码所需jar包如下图所示(解压lucene-4.4.0后的目录): 入门代码: ...

  7. hihoCoder#1838 : 鎕鎕鎕 贪心

    ---题面--- 题解: 神奇的贪心题,,,感觉每次做贪心题都无从下手... 我们首先按照a对所有卡片从小到大排序,然后从1开始,从连续的两张牌中取b最大的,最后一张单出来的也取了. 可以证明,这样的 ...

  8. C++中typedef和#define简介

    本文基于<C++ Primer(第5版)>和网上博客,整理而成. 一.类型别名 类型别名是一个名字,它是某种类型的同义词,有两种方法可用于定义类型别名:typedef.using. 1.关 ...

  9. nginx 报invalid pid number

    /opt/ibis/sbin/nginx -c /opt/ibis/conf/nginx.conf 这是make make install之后生成的文件夹和文件. -c c是configure的缩写 ...

  10. su对环境变量做了什么

    服务器是ubuntu12.04 用一个账户app,使用su - app得到的环境变量和直接ssh登录的环境变量不同. 导致su - app,无法执行ifconfig su - app 的环境变量 /u ...