题意:找出第k短路,输出长度,没有输出-1

思路:这题可以用A*做。A*的原理是这样,我们用一个函数:f = g + h 来表示当前点的预期步数,f代表当前点的预期步数,g代表从起点走到当前的步数,h代表从当前点走到终点的最短路,显然h可以用最短路解出。那么我们从起点开始找,每次找f最小的点,直到找到第k个这样的点。

代码:

#include<queue>
#include<cstring>
#include<set>
#include<map>
#include<stack>
#include<string>
#include<cmath>
#include<vector>
#include<cstdio>
#include<iostream>
#include<algorithm>
typedef long long ll;
using namespace std;
const int maxn = + ;
const int seed = ;
const ll MOD = 1e9 + ;
const int INF = 0x3f3f3f3f;
int n, m, k, tot;
struct Edge{
int v, w, next;
}edge[maxn * ];
struct As{
int f, g, pos;
bool operator < (const As a) const{
return a.f == f? a.g < g : a.f < f;
}
};
struct que{
int u, v, w;
}q[];
int head[maxn], dis[maxn];
bool vis[maxn];
void init(){
memset(head, -, sizeof(head));
tot = ;
}
void addEdge(int u, int v, int w){
edge[tot].v = v;
edge[tot].w = w;
edge[tot].next = head[u];
head[u] = tot++;
}
void spfa(int st){
for(int i = ; i <= n; i++) dis[i] = INF;
memset(vis, false, sizeof(vis));
vis[st] = true;
dis[st] = ;
queue<int> q;
while(!q.empty()) q.pop();
q.push(st);
while(!q.empty()){
int u = q.front();
q.pop();
vis[u] = false;
for(int i = head[u]; i != -; i = edge[i].next){
int v = edge[i].v;
int w = edge[i].w;
if(dis[v] > dis[u] + w){
dis[v] = dis[u] + w;
if(!vis[v]){
vis[v] = true;
q.push(v);
}
}
}
}
}
int Astar(int st, int end){
int cnt = ;
priority_queue<As> q;
while(!q.empty()) q.pop();
if(st == end) k++;
if(dis[st] == INF) return -;
As a, b;
a.pos = st, a.g = , a.f = a.g + dis[st];
q.push(a);
while(!q.empty()){
a = q.top();
q.pop();
if(a.pos == end){
cnt++;
if(cnt == k) return a.f;
}
for(int i = head[a.pos]; i != -; i = edge[i].next){
b.pos = edge[i].v;
b.g = a.g + edge[i].w;
b.f = b.g + dis[b.pos];
q.push(b);
}
}
return -;
}
int main(){
while(~scanf("%d%d" ,&n, &m)){
init();
for(int i = ; i <= m; i++){
scanf("%d%d%d", &q[i].u, &q[i].v, &q[i].w);
addEdge(q[i].v, q[i].u, q[i].w);
}
int s, t;
scanf("%d%d%d", &s, &t, &k);
spfa(t);
init();
for(int i = ; i <= m; i++){
addEdge(q[i].u, q[i].v, q[i].w);
}
printf("%d\n", Astar(s, t));
}
return ;
}

POJ 2449 Remmarguts' Date(第K短路 + A* + 最短路)题解的更多相关文章

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

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

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

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

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

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

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

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

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

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

  6. poj 2449 Remmarguts' Date(第K短路问题 Dijkstra+A*)

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

  7. 图论(A*算法,K短路) :POJ 2449 Remmarguts' Date

    Remmarguts' Date Time Limit: 4000MS   Memory Limit: 65536K Total Submissions: 25216   Accepted: 6882 ...

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

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

  9. POJ 2449 Remmarguts' Date (第k短路径)

    Remmarguts' Date Time Limit: 4000MS   Memory Limit: 65536K Total Submissions:35025   Accepted: 9467 ...

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

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

随机推荐

  1. 开机自启:bat实现一次性打开win7中的常用软件和文件夹

    需求说明: 我们电脑(windows)办公,经常上班前一开机,就要手动打开很多文件夹和程序. 想节省时间,一键打开常用的文件夹和程序,研究了一下bat命令,于是mystart.bat产生了. myst ...

  2. Pycharm自动换行

    只对当前文件有效的操作是菜单栏->View -> Active Editor -> Use Soft Wraps. 要是想对所有文件都起到效果,就要在setting里面进行操作.Pe ...

  3. redis连接池的标准用法:

    from .conf import HOST, PORT, POOL_NAME import redis redis_pool = redis.ConnectionPool(host=HOST, po ...

  4. [MySQL 5.6] MySQL 5.6 group commit 性能测试及内部实现流程

    [MySQL 5.6] MySQL 5.6 group commit 性能测试及内部实现流程 http://mysqllover.com/?p=581 尽管Mariadb以及Facebook在long ...

  5. Centos7 Zabbix3.2集群安装

    安装环境:服务器10.80.0.191作为zabbix-server,10.80.0.191-195作为zabbix-agent. [zabbix@miyan ~]$ cat /etc/redhat- ...

  6. easyUI的datebox添加清空按钮功能

    需要修改源码: 第一步:按下图修改 第二步:按下两图修改(*zh_CN.js)

  7. opencv之颜色过滤只留下图片中的红色区域

    如图,这次需要在图片中找到卷尺的红色刻度,所以需要对图像做过滤,只留下红色部分. 一开始的想法是分别找到RGB值,然后找到红色区域的部分保留就可以了,不过好像很难确定红色区域的RGB取值范围,所以要把 ...

  8. 跑道标识和那些复杂的灯光系统 and 简介、编号、参数、标志及数量 and 飞机跑道标准与参数

    http://www.360doc.com/content/16/0616/12/32670666_568219786.shtml http://news.carnoc.com/list/365/36 ...

  9. Impala SQL 语言元素(翻译)[转载]

    原 Impala SQL 语言元素(翻译) 本文来源于http://my.oschina.net/weiqingbin/blog/189413#OSC_h2_2 摘要 http://www.cloud ...

  10. [转] Matlab编程规范(MATLAB Programming Style Guidelines)

    转自: Jerry Zitao Liu的博客 主要是参考了下面这篇文章,简洁总结在这里. MATLAB Programming Style Guidelines 简洁总结如下: 表示object的数量 ...