=.=好菜

#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. 代码这样写更优雅(Python版)

    要写出 Pythonic(优雅的.地道的.整洁的)代码,还要平时多观察那些大牛代码,Github 上有很多非常优秀的源代码值得阅读,比如:requests.flask.tornado,笔者列举一些常见 ...

  2. 解决VMware虚拟机的CentOS无法上网

    1)点击 VM->Settings Hardware选项卡下面 2)点击Network Adapter 设置如下图所示,首先我们在虚拟机中将网络配置设置成NAT 在服务中开启: VMware D ...

  3. 【Python】Pycharm2018激活方式【亲测好用】

    2.激活码激活 优点:Window.Mac.Ubantu都稳定有效,关键是这种激活方式不会产生其他影响 缺点:需要修改hosts文件 修改hosts文件将0.0.0.0 account.jetbrai ...

  4. sap gui 使用方法, sap logon

    1:打断点:在程序保存并激活之后,可以打内部或外部断点. 如图示. 2:  display 展示程序,不能修改. 3: 查看创建的类的结构,使用more>display object list  ...

  5. spark的ML和MLLib两个包区别和联系?

    原文链接:https://www.zhihu.com/question/35225203/answer/123986969 1. 技术角度上,面向的数据集类型不一样:ML的API是面向Dataset的 ...

  6. 实习培训——Java基础(4)

    实习培训——Java基础(4) 1 多态 多态是同一个行为具有多个不同表现形式或形态的能力. 多态就是同一个接口,使用不同的实例而执行不同操作,多态性是对象多种表现形式的体现. 现实中,比如我们按下 ...

  7. DNS解析原理和流程

    DNS解析原理和流程   DNS解析其实就是将IP地址(202.96.134.133)变成域名(www.xxxxx.com)   网络通讯大部分是基于TCP/IP的,而TCP/IP是基于IP地址的,所 ...

  8. Apache+php+mysql环境配置

    Apache+PHP+MySQL环境搭建 标题格式 正文格式 阶段性完成格式 正文中强调格式 ————————————————————————————— 前语:本文是从我写过的doc文档迁移过来的,由 ...

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

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

  10. bootstrap 带有确定取消按钮的modal

    </div><div class="modal fade" id="confirmModal" tabindex="-1" ...