题意:找出第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. Making the Grade---poj3666(dp)

    题目链接:http://poj.org/problem?id=3666 题意:有一个n个数的序列a,现在要把这些序列变成单调增的或者单调减的序列 b , 其价值为|A1 - B1| + |A2 - B ...

  2. Android(八) HandlerThread

    1.Looper Looper used to run a message loop for a thread. Threads by default do not have a message lo ...

  3. BUG笔记:Win XP IE8下HTML Parsing Error: Unable to modify the parent container element before the child

    [Bug描述]Windows XP IE8的某些版本下页面只显示一部分,其余为空白.IE左下角有惊叹号报错标志,点开后显示字符如下: HTML Parsing Error: Unable to mod ...

  4. iOS入门怎样选择Swift和objective-c

    版权声明:本文为博主原创文章,未经博主同意不得转载.博主微信:lofocus https://blog.csdn.net/cuibo1123/article/details/28261795 学oc吧 ...

  5. java-信息安全(十一)-非对称加密算法ECC以及ECDSA签名

    概述 信息安全基本概念: ECC算法(Elliptic curve cryptography,椭圆曲线密码学) 一.ECC加密解密[暂时无意义] 椭圆加密算法(ECC)是一种公钥加密体制,最初由Kob ...

  6. office 2016 install(office2016组件自定义安装激活程序) v5.9.3中文绿色版

    下载地址  http://www.ddooo.com/softdown/71741.htm#dltab office 2016 install是目前下载office2016和office2016组件最 ...

  7. PHP开启CORS

    CORS 定义 Cross-Origin Resource Sharing(CORS)跨来源资源共享是一份浏览器技术的规范,提供了 Web 服务从不同域传来沙盒脚本的方法,以避开浏览器的同源策略,是 ...

  8. [LeetCode] 200. Number of Islands_ Medium tag: BFS

    Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. An island is surro ...

  9. mysql数据库给别人访问权限

    注:本操作是在WIN命令提示符下,phpMyAdmin同样适用. 用户:phplamp  用户数据库:phplampDB 1.新建用户. //登录MYSQL @>mysql -u root -p ...

  10. potplayer启动慢的各种奇葩原因

    此博文可能会持续更新,因为启动慢的原因各种奇葩啊 1.声卡(螃蟹卡)驱动导致的启动慢.解决方法:potplayer中,"选项"->"声音"->修改一 ...