=.=好菜

#include <iostream>
#include <cstdio>
#include <string.h>
#include <cstring>
#include <queue> using namespace std;
const int N = 1e3+;
const int M = +;
typedef long long ll;
const ll INF = 1e15; int n,m,head[N],rehead[N],tot;
struct node {
int v,w,next;
}E[M],reE[M]; void init() {
tot=;
memset(head,,sizeof(head));
memset(rehead,,sizeof(rehead));
}
void addEdge(int u,int v,int w) {
++tot;
E[tot].next = head[u];
head[u]=tot;
E[tot].v = v;
E[tot].w = w; reE[tot].next = rehead[v];
rehead[v]=tot;
reE[tot].v = u;
reE[tot].w = w;
} bool inq[N]; ll d[N];
queue<int>que;
void spfa(int src) {
while(!que.empty())
que.pop();
for(int i=;i<N;i++) d[i]=INF;
memset(inq,,sizeof(inq));
d[src]=;
inq[src] = ;
que.push(src);
while(!que.empty()) {
int now = que.front();
que.pop();
//if(vis[now]) continue;
//vis[now]=1;
inq[now] = ;
for(int i=rehead[now]; i; i=reE[i].next) {
int v = reE[i].v;
int w = reE[i].w;
if(d[v] > d[now] + w) {
d[v] = d[now] + w;
if(!inq[v])
que.push(v);
}
}
}
} struct A {
int v;
ll f,g;
///v是current点 f(v)=g(v)+h(v) g(v):st到v的估值, h(v):v到ed的估值
bool operator<(const A other) const {
if(other.f == f) return other.g < g;
return other.f < f;
}
}; int Astar(int st,int ed,int k) {
priority_queue<A> Q;
if(st==ed) k++;
if(d[st]==INF) return -;
int cnt = ;
A t,tt;
t.v=st,t.g=,t.f=t.g+d[st];
Q.push(t);
while (!Q.empty()) {
tt = Q.top(); Q.pop();
int u=tt.v;
if(u == ed) {
cnt++;
if(cnt==k) return tt.g;
}
for(int i=head[u]; i; i=E[i].next) {
t.v = E[i].v;
t.g = tt.g + E[i].w;
t.f = t.g + d[t.v];
Q.push(t); }
}
return -;
} int main () {
//freopen("in.txt","r",stdin);
while (scanf("%d %d", &n, &m)==) {
init();
for(int i=;i<=m;i++) {
int x,y,z; scanf("%d%d%d",&x,&y,&z);
addEdge(x,y,z);
}
int s,t,k; scanf("%d%d%d",&s, &t, &k);
spfa(t);
//for(int i=1;i<=n;i++) cout << d[i]<<endl;
cout<<Astar(s,t,k)<<endl;
}
return ;
}

poj 2449 Remmarguts' Date 求第k短路 Astar算法的更多相关文章

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

    Description "Good man never makes girls wait or breaks an appointment!" said the mandarin ...

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

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

  3. poj 2449 Remmarguts' Date【第K短路】

    题目 题意:求 点s 到 点t 的 第 k 短 路的距离: 估价函数=当前值+当前位置到终点的距离 f(n)=g(n)+h(n);     g(n)表示g当前从s到p所走的路径的长度,      h( ...

  4. POJ 2449 Remmarguts' Date(第K短路 + A* + 最短路)题解

    题意:找出第k短路,输出长度,没有输出-1 思路:这题可以用A*做.A*的原理是这样,我们用一个函数:f = g + h 来表示当前点的预期步数,f代表当前点的预期步数,g代表从起点走到当前的步数,h ...

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

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

  6. POJ 2449 Remmarguts' Date (K短路 A*算法)

    题目链接 Description "Good man never makes girls wait or breaks an appointment!" said the mand ...

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

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

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

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

  9. poj 2449 Remmarguts' Date K短路+A*

    题目链接:http://poj.org/problem?id=2449 "Good man never makes girls wait or breaks an appointment!& ...

随机推荐

  1. mysql 数据操作 多表查询 子查询 带IN关键字的子查询

    1 带IN关键字的子查询 #查询平均年龄在25岁以上的部门名关键点部门名 以查询员工表的dep_id的结果 当作另外一条sql语句查询条件使用 in (sql语句) mysql ; +-------- ...

  2. Jedis 对 Redis 的操作详解

    1. JedisUtil2. 键操作3. 字符串操作4. 字节串4. 整数和浮点数5. 列表6. 集合(Set)7. 散列8. 排序sort 本篇主要阐述Jedis对redis的五大类型的操作:字符串 ...

  3. javascript-js模拟form页面提交跳转

    window.location.href跳转到另外一个界面.但直接传递get方法会暴露数据 下面可以实现跳转的效果,却又能够通过post传递方法隐藏数据. 有一个不足就是,在跳转到新页面后,点击“返回 ...

  4. SQLyog恢复数据库报错解决方法【Error Code: 2006 - MySQL server has gone away】

    https://blog.csdn.net/niqinwen/article/details/8693044 导入数据库的时候 SQLyog 报错了 Error Code: 2006 – MySQL ...

  5. Improving the quality of the output

    There are a variety of reasons you might not get good quality output from Tesseract. It's important ...

  6. Java读取Excel数据

    Java读取Excel数据,解析文本并格式化输出 Java读取Excel数据,解析文本并格式化输出 Java读取Excel数据,解析文本并格式化输出 下图是excel文件的路径和文件名 下图是exce ...

  7. Python常用函数及说明

    原文地址:博客园  CSDN 基本定制型C.__init__(self[, arg1, ...]) 构造器(带一些可选的参数)C.__new__(self[, arg1, ...]) 构造器(带一些可 ...

  8. Lintcode: First Position of Target (Binary Search)

    Binary search is a famous question in algorithm. For a given sorted array (ascending order) and a ta ...

  9. 更高效的MergeSort--稍微优化

    0. 简介 本文简要介绍一下比传统MergeSort更高效的算法,在原来的算法Merge基础上,少发生一半拷贝.欢迎探讨,感谢阅读. 原文链接如下:http://loverszhaokai.com/p ...

  10. ftp命令行敲不了

    最先安装了vsftpd,但是命令行敲ftp老是不行 解决方案:ftp命令是ftp客户端,vsftp是ftp服务器,两者不是一个概念.你需要安装ftp客户端 yum install ftp 可以自动安装 ...