poj2449 Remmarguts' Date K短路 A*
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*的更多相关文章
- [poj2449]Remmarguts' Date(K短路模板题,A*算法)
解题关键:k短路模板题,A*算法解决. #include<cstdio> #include<cstring> #include<algorithm> #includ ...
- poj 2449 Remmarguts' Date K短路+A*
题目链接:http://poj.org/problem?id=2449 "Good man never makes girls wait or breaks an appointment!& ...
- POJ 2449 Remmarguts' Date (K短路 A*算法)
题目链接 Description "Good man never makes girls wait or breaks an appointment!" said the mand ...
- POJ 2449 Remmarguts' Date --K短路
题意就是要求第K短的路的长度(S->T). 对于K短路,朴素想法是bfs,使用优先队列从源点s进行bfs,当第K次遍历到T的时候,就是K短路的长度. 但是这种方法效率太低,会扩展出很多状态,所以 ...
- POJ2449 Remmarguts' Date 第K短路
POJ2449 比较裸的K短路问题 K短路听起来高大上 实际思路并不复杂 首先对终点t到其他所有点求最短路 即为dist[] 然后由起点s 根据当前走过的距离+dist[]进行A*搜索 第k次到达t即 ...
- [poj2449]Remmarguts' Date(spfa+A*)
转载请注明出处: http://www.cnblogs.com/fraud/ ——by fraud Remmarguts' Date Time Limit: 4000MS Mem ...
- POJ2449 Remmarguts' Date
"Good man never makes girls wait or breaks an appointment!" said the mandarin duck father. ...
- POJ 2449Remmarguts' Date K短路模板 SPFA+A*
K短路模板,A*+SPFA求K短路.A*中h的求法为在反图中做SPFA,求出到T点的最短路,极为估价函数h(这里不再是估价,而是准确值),然后跑A*,从S点开始(此时为最短路),然后把与S点能达到的点 ...
- poj2449 Remmarguts' Date【A*算法】
转载请注明出处,谢谢:http://www.cnblogs.com/KirisameMarisa/p/4303855.html ---by 墨染之樱花 [题目链接]:http://poj.org/ ...
随机推荐
- java中循环的不同终止方式
1.break:直接强行跳出当前循环,不再执行剩余代码.但在多重循环的情况下,若break在内层循环中,则仅仅终止了内层循环,外循环照常执行. 2.continue:仅仅终止此次循环. 3.retur ...
- Kendo MVVM 数据绑定(五) Events
Kendo MVVM 数据绑定(五) Events 本篇和 Kendo MVVM 数据绑定(三) Click 类似,为事件绑定的一般形式.Events 绑定支持将 ViewModel 的方法绑定到 D ...
- Python+selenium之测试报告(3)
较测试报告(2),该文章将测试报告和测试截图存放在随机变动的文件夹下面,去除了要存放在指定文件夹下面的限制. 注:遇到问题有: 1.创建由时间自动拼接的多级文件夹 2. import os impor ...
- 【转】NSBundle的使用,注意mainBundle和Custom Bundle的区别
1.[NSBundle mainBundle],文件夹其实是Group,如左侧的树形文件管理器 Build之后,文件直接就复制到了根目录下,于是读取的方法,应该是这样: NSString *earth ...
- PWD简介与妙用(一个免费、随时可用的Docker实验室)
转载自 https://baiyue.one/archives/472.html 本文介绍下 PWD 的历史,并依据本站最近学习心得,经过多次尝试,终于打通了 Docker 与常规宝塔面板搭建,因此, ...
- 使用MaskedTextBox控件实现输入验证
实现效果: 知识运用: MaskedTextBox控件的 Mask属性 BeepOnError属性 MaskInputRejected事件 实现代码: private void Form1_Load( ...
- 在DataGridView控件中显示图片
实现效果: 知识运用: DataGridView控件的DataSource属性 实现代码: private void Form1_Load(object sender, EventArgs e) { ...
- EWS code return Error : Request failed. The remote server returned an error: (403) Forbidden OR (401) Unauthorized
Following is my code. ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2007_SP1 ...
- on() 和 click() 的区别
on() 和 click() 的区别: 二者在绑定静态控件时没有区别,但是如果面对动态产生的控件,只有 on() 能成功的绑定到动态控件中. 以下实例中原先的 HTML 元素点击其身后的 Delete ...
- Where do I belong-freecodecamp算法题目
Where do I belong(数组排序并找出元素索引) 要求 给数组排序 找到指定的值在数组的位置,并返回位置对应的索引. 思路 设定.sort()需要的返回函数 将要搜索的值添加到数组内 用. ...