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. redis自启动配置详解

    一.概述 1.1原理 redis自启动的工作原理是怎么样的呢?Linux系统启动后,会有一个程序去特定目录下面扫描文件,然后执行这些文件,这些文件可称之为脚本.所以,你可以把你的工作写成一个脚本,放到 ...

  2. Python+Selenium之摘取网页上全部邮箱

    本文转载:http://blog.csdn.net/u011541946/article/details/68485981 练习场景:在某一个网页上有些字段是我们感兴趣的,我们希望摘取出来,进行其他操 ...

  3. UVA 10905 Children's Game (贪心)

    贪心,假如任意给出一个序列,如果两两交换了以后会变大,那么就交换,直到不能交换为止. #include<bits/stdc++.h> using namespace std; ; stri ...

  4. UVALive 4329 Ping pong (BIT)

    枚举中间的人,只要知道在这个人前面的技能值比他小的人数和后面技能值比他小的人数就能计算方案数了,技能值大的可有小的推出. 因此可以利用树状数组,从左到右往树上插点,每个点询问sum(a[i]-1)就是 ...

  5. codeforces Gym 100286H Hell on the Markets

    紫书上面的题,队友做的,WA了freopen..爆了int... UVA 1614 - Hell on the Markets 奇怪的股市(贪心,结论)

  6. Array - Container With Most Water

    /** * 此为暴力解法 * Find two lines, which together with x-axis forms a container, such that the container ...

  7. prometheus-简介及安装

    监控是整个产品周期中最重要的一环,及时预警减少故障影响免扩大,而且能根据历史数据追溯问题. 对系统不间断实时监控 实时反馈系统当前状态 保证业务持续性运行 监控系统 监控方案 告警 特点 适用 Zab ...

  8. linux - 权限解析

    当你在linux下用命令ll 或者ls -la的时候会看到这些字眼,这些字眼表示为不同用户组的权限:r:read就是读权限 --数字4表示w:write就是写权限 --数字2表示 x:excute就是 ...

  9. java基础—java对象的序列化和反序列化

    一.序列化和反序列化的概念 把对象转换为字节序列的过程称为对象的序列化. 把字节序列恢复为对象的过程称为对象的反序列化. 对象的序列化主要有两种用途: 1) 把对象的字节序列永久地保存到硬盘上,通常存 ...

  10. C# 用qq邮箱发邮件

    一.在企业的QQ邮箱中开启POP3/SMTP服务 开启服务时,授权密码保存好. 二.示例 public static string UserName = ""; // 企业邮箱 p ...