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. DatabaseMetaData类

    DatabaseMetaData类是java.sql包中的类,利用它可以获取我们连接到的数据库的结构.存储等很多信息.如: 1.数据库与用户,数据库标识符以及函数与存储过程.         2.数据 ...

  2. css3背景与边框相关样式

    background-attachment          背景图像是否固定或者随着页面的其余部分滚动 background-color                    设置元素的背景颜色 b ...

  3. 就来推荐一本2018年研究的Web书《移动Web前端高效开发实战》

    一线互联网公司Web前端团队实战经验总结,涵盖移动Web前端开发各个关键技术环节,包括移动开发核心技术.常用布局方案.MV*类新时代框架.预编译技术.性能优化.开发调试.混合式应用.单元测试.工程化等

  4. struts2 ognl表达式访问值栈

    1:简单的说,值栈是对应每一个请求对象的轻量级的数据存储中心,在这里统一管理着数据,供Action.Result.Interceptor等Struts2的其他部分使用,这样数据被集中管理起来而不凌乱. ...

  5. 从零开始的Lua宅[1]:编译Lua解释器

    Lua是一门神奇的脚本语言,游戏宅必备,懒人必备.Lua差多不是学起来用起来最简单的语言了,以至于简单到自身就是文档,自身就是配置文件.但是Lua的运行效率却是众多脚本中非常高的,据说仅次于V8爹下的 ...

  6. httpHelper请求辅助类

    #import <Foundation/Foundation.h> #import "AFNetworking.h" @interface AFHttpClient : ...

  7. (五)我的JavaScript系列:JavaScript的糟粕

    泪眼问花花不语,乱红飞过秋千去. JavaScript的糟粕 JavaScript语言是一门集精华与糟粕于一体的语言.在JavaScript: the good parts中,便集中讨论了关于精华与糟 ...

  8. HDU 3001 Travelling (状压DP,3进制)

    题意: 给出n<=10个点,有m条边的无向图.问:可以从任意点出发,至多经过同一个点2次,遍历所有点的最小费用? 思路: 本题就是要卡你的内存,由于至多可经过同一个点2次,所以只能用3进制来表示 ...

  9. 小常识:变量的修饰符和DEMO

    public static string ss = "这是全局静态变量";//生命周期:程序结束为止,可以修改 public string s = "这是全局变量&quo ...

  10. tcp、http和socket的区别

    本文原链接:https://www.jianshu.com/p/88d69454bdde tcp.http和socket的区别 一:tcp协议 tcp协议属于传输层协议(UDP也属于传输层协议,但是U ...