K短路裸题。

#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
struct Edge{
int too, nxt, val;
}edge[100005];
struct Odge{
int too, nxt, val;
}odge[100005];
struct Node{
int idd, hfc, gfc;
bool operator<(const Node &x)const{
return hfc+gfc>x.hfc+x.gfc;
}
}d[1000005];//n*k
int n, m, s, t, k, uu, vv, ww, hea[1005], oea[1005], cnt, ont, dis[1005];
int din, tms[1005], ans;
bool vis[1005];
void add_edge(int fro, int too, int val){
edge[++cnt].nxt = hea[fro];
edge[cnt].too = too;
edge[cnt].val = val;
hea[fro] = cnt;
}
void add_odge(int fro, int too, int val){
odge[++ont].nxt = oea[fro];
odge[ont].too = too;
odge[ont].val = val;
oea[fro] = ont;
}
void dijkstra(){
memset(dis, 0x3f, sizeof(dis));
dis[t] = 0;
for(int i=1; i<=n; i++){
int mini=0;
for(int j=1; j<=n; j++)
if(!vis[j] && dis[mini]>dis[j])
mini = j;
vis[mini] = true;
if(!mini) return ;
for(int i=oea[mini]; i; i=odge[i].nxt){
int v=odge[i].too;
dis[v] = min(dis[v], odge[i].val+dis[mini]);
}
}
}
void aStar(){
d[++din] = (Node){s, 0, 0};
while(din){
Node j=d[1];
pop_heap(d+1, d+1+din);
din--;
tms[j.idd]++;
if(j.idd==t && tms[t]==k){
printf("%d\n", j.hfc+j.gfc);
return ;
}
if(tms[j.idd]>k) continue;
for(int i=hea[j.idd]; i; i=edge[i].nxt)
if(tms[edge[i].too]<=k){
d[++din] = (Node){edge[i].too, j.hfc+edge[i].val, dis[edge[i].too]};
push_heap(d+1, d+1+din);
}
}
cout<<"-1\n";
}
int main(){
cin>>n>>m;
for(int i=1; i<=m; i++){
scanf("%d %d %d", &uu, &vv, &ww);
add_edge(uu, vv, ww);
add_odge(vv, uu, ww);
}
cin>>s>>t>>k;
if(s==t) k++;
dijkstra();
aStar();
return 0;
}

poj2449 Remmarguts' Date K短路 A*的更多相关文章

  1. [poj2449]Remmarguts' Date(K短路模板题,A*算法)

    解题关键:k短路模板题,A*算法解决. #include<cstdio> #include<cstring> #include<algorithm> #includ ...

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

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

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

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

  4. POJ 2449 Remmarguts' Date --K短路

    题意就是要求第K短的路的长度(S->T). 对于K短路,朴素想法是bfs,使用优先队列从源点s进行bfs,当第K次遍历到T的时候,就是K短路的长度. 但是这种方法效率太低,会扩展出很多状态,所以 ...

  5. POJ2449 Remmarguts' Date 第K短路

    POJ2449 比较裸的K短路问题 K短路听起来高大上 实际思路并不复杂 首先对终点t到其他所有点求最短路 即为dist[] 然后由起点s 根据当前走过的距离+dist[]进行A*搜索 第k次到达t即 ...

  6. [poj2449]Remmarguts' Date(spfa+A*)

    转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud Remmarguts' Date Time Limit: 4000MS   Mem ...

  7. POJ2449 Remmarguts' Date

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

  8. POJ 2449Remmarguts' Date K短路模板 SPFA+A*

    K短路模板,A*+SPFA求K短路.A*中h的求法为在反图中做SPFA,求出到T点的最短路,极为估价函数h(这里不再是估价,而是准确值),然后跑A*,从S点开始(此时为最短路),然后把与S点能达到的点 ...

  9. poj2449 Remmarguts' Date【A*算法】

    转载请注明出处,谢谢:http://www.cnblogs.com/KirisameMarisa/p/4303855.html   ---by 墨染之樱花 [题目链接]:http://poj.org/ ...

随机推荐

  1. Vue部分知识

    一.本尊建议的学习顺序:https://zhuanlan.zhihu.com/p/23134551(侵删) 二.安装: 1.安装 Node.js,可以去Node.js的官网上下载: 2.(非必选)如果 ...

  2. 前后端分离 vue+springboot 跨域 session+cookie失效问题

    环境: 前端 vue   ip地址:192.168.1.205 后端 springboot2.0  ip地址:192.168.1.217 主要开发后端. 问题: 首先登陆成功时将用户存在session ...

  3. javascript中两种基本常用排序算法分析

    备注:内容大部分从网上复制,代码为自己手写.仅做知识的温故知新,并非原创. 1.冒泡排序(Bubble Sort) (1)算法描述 冒泡排序是一种简单的排序算法.它重复地走访过要排序的数列,一次比较两 ...

  4. Burpsuite Professional安装及使用教程

    转自:https://www.jianshu.com/p/edbd68d7c341 1.先从吾爱破解论坛下载工具:https://down.52pojie.cn/Tools/Network_Analy ...

  5. Java transient关键字使用

    1. transient的作用及其使用方法 我们都知道一个对象只要实现了Serilizable接口,这个对象就可以被序列化,java的这种序列化模式为开发者提供了很多便利,我们可以不必关系具体序列化的 ...

  6. ssh连接github连不上

    连接github报端口22连接不上: 输入命令展示出ssh_config内容后: vim /etc/ssh/ssh_config 或者使用open /etc/ssh/ssh_config命令在文本编辑 ...

  7. Vue风格指南总结及对应ESLint规则配置

    全手打原创,转载请标明出处:https://www.cnblogs.com/dreamsqin/p/10906951.html,多谢,=.=~ 必要的:规避错误: 强烈推荐:改善可读性和开发体验: 推 ...

  8. 用配置文件方式启动mongodb集群

  9. 修改字体篇:css3@font-face@字体转换@浏览器字体支持

    @font-face { font-family: 'MyWebFont'; src: url('webfont.eot'); /* IE9 Compat Modes */ src: url('web ...

  10. 读书笔记2013-2 Linux内核设计与实现A

    读书笔记2013-2 Linux内核设计与实现A <Linux内核设计与实现> 简介 这本书不是想Linux源码剖析那样一行行分析Linux源代码的书,而是从Linux历史,Linux哲学 ...