题目

题意:求 点s 到 点t 的 第 k 短 路的距离;

估价函数=当前值+当前位置到终点的距离

f(n)=g(n)+h(n);     g(n)表示g当前从s到p所走的路径的长度,      h(n)‘启发式函数’,表示为终点t到其余一点p的路径长度;

(1)将有向图的所有边反向,以原终点t为源点,求解t到所有点的最短距离; 

(2)新建一个优先队列,将源点s加入到队列中; 

(3)从优先级队列中弹出f(p)最小的点p,如果点p就是t,则计算t出队的次数; 

如果当前为t的第k次出队,则当前路径的长度就是s到t的第k短路的长度,算法结束; 

否则遍历与p相连的所有的边,将扩展出的到p的邻接点信息加入到优先级队列;

#include <iostream>
#include <cstdio>
#include <queue>
#include <vector>
using namespace std; const int Maxn = 10010;
const int INF = 1e9; struct node{
int to,val;
node(){}
node(int a,int b)
{
to = a; val = b;
}
}; vector<node> adj[Maxn],_adj[Maxn]; int n,m,k;
bool vis[Maxn];
int dis[Maxn]; void AddEdge(int x,int y,int val)
{
adj[x].push_back(node(y,val));
_adj[y].push_back(node(x,val));//反向存图
}
void Dijkstra(int s,int t)
{
priority_queue<int, vector<int>,greater<int> > q;
while(!q.empty()) q.pop();
for(int i=1;i<=n;i++)
vis[i]=false,dis[i]=INF;
vis[t]=true;dis[t]=0;q.push(t);
int u,len;
while(!q.empty())
{
u = q.top(); q.pop();
len = _adj[u].size();
for(int i=0;i<len;i++)
{
node v = _adj[u][i];
if(dis[v.to]>dis[u]+v.val)
{
dis[v.to]=dis[u]+v.val;
if(!vis[v.to])
{
q.push(v.to);
vis[v.to]=true;
}
}
}
vis[u]= false;
}
}
struct Anode{
int h,g,id;
Anode(int a,int b,int c){h=a;g=b;id=c;}
bool operator < (Anode a) const{
return h+g > a.h + a.g;
}
};
priority_queue<Anode> Q;
int Astar(int s,int t) //A*算法
{
while(!Q.empty()) Q.pop();
Q.push(Anode(0,dis[s],s));
int len,num;
num=0;
while(!Q.empty())
{
Anode u = Q.top();
Q.pop();
if(u.id==t) ++num;
if(num>=k) return u.h;
len = adj[u.id].size();
for(int i=0;i<len;i++)
{
node v = adj[u.id][i];
Q.push(Anode(u.h+v.val,dis[v.to],v.to));
}
}
return -1;
}
int main()
{
while(scanf("%d%d",&n,&m)!=-1)
{
for(int i=0;i<Maxn;i++)
adj[i].clear(),_adj[i].clear();
int x,y,v,s,t;
for(int i=1;i<=m;i++)
{
scanf("%d%d%d",&x,&y,&v);
AddEdge(x,y,v);
}
scanf("%d%d%d",&s,&t,&k);
if(s==t) k++;
Dijkstra(s,t);
printf("%d\n",Astar(s,t));
}
return 0;
}
/*
2 2
1 2 5
2 1 4
1 2 2
*/

poj 2449 Remmarguts' Date【第K短路】的更多相关文章

  1. poj 2449 Remmarguts' Date (k短路模板)

    Remmarguts' Date http://poj.org/problem?id=2449 Time Limit: 4000MS   Memory Limit: 65536K Total Subm ...

  2. POJ 2449 - Remmarguts' Date - [第k短路模板题][优先队列BFS]

    题目链接:http://poj.org/problem?id=2449 Time Limit: 4000MS Memory Limit: 65536K Description "Good m ...

  3. poj 2449 Remmarguts' Date 第k短路 (最短路变形)

    Remmarguts' Date Time Limit: 4000MS   Memory Limit: 65536K Total Submissions: 33606   Accepted: 9116 ...

  4. poj 2449 Remmarguts' Date(K短路,A*算法)

    版权声明:本文为博主原创文章.未经博主同意不得转载. https://blog.csdn.net/u013081425/article/details/26729375 http://poj.org/ ...

  5. POJ 2449 Remmarguts' Date ( 第 k 短路 && A*算法 )

    题意 : 给出一个有向图.求起点 s 到终点 t 的第 k 短路.不存在则输出 -1 #include<stdio.h> #include<string.h> #include ...

  6. 【POJ】2449 Remmarguts' Date(k短路)

    http://poj.org/problem?id=2449 不会.. 百度学习.. 恩. k短路不难理解的. 结合了a_star的思想.每动一次进行一次估价,然后找最小的(此时的最短路)然后累计到k ...

  7. 【POJ】2449.Remmarguts' Date(K短路 n log n + k log k + m算法,非A*,论文算法)

    题解 (搬运一个原来博客的论文题) 抱着板题的心情去,结果有大坑 就是S == T的时候也一定要走,++K 我发现按照论文写得\(O(n \log n + m + k \ log k)\)算法没有玄学 ...

  8. poj 2449 Remmarguts' Date(第K短路问题 Dijkstra+A*)

    http://poj.org/problem?id=2449 Remmarguts' Date 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!& ...

  10. 图论(A*算法,K短路) :POJ 2449 Remmarguts' Date

    Remmarguts' Date Time Limit: 4000MS   Memory Limit: 65536K Total Submissions: 25216   Accepted: 6882 ...

随机推荐

  1. 使用vim编程步骤

    先用vim 名字.cpp //创建一个.cpp文件进行代码编写 可以调用g++ 名字.cpp的形式进行编译,更好的方法是采用CMakeLists.txt touch CMakeLists.txt // ...

  2. spring boot (一): Hello World

    什么是spring boot Spring Boot是由Pivotal团队提供的全新框架,其设计目的是用来简化新Spring应用的初始搭建以及开发过程.该框架使用了特定的方式来进行配置,从而使开发人员 ...

  3. javascript 高级程序设计 七

    引言:好几天没有写随笔了,项目有点紧,恰好今天项目遇到了比较大阻塞,就只好来写一篇随笔来压压惊. 1.Date类型 创建一个新的Date:(除了new Date()外) var someDate = ...

  4. UEFI、BIOS、Secure Boot的关系和知识介绍

      从Windows 8操作系统时代开始,安装操作系统的方法也有了很大的改变,Windows 8采用了Secure Boot引导启动的方式,而不是过去Win XP和Win 7的Legacy启动方式,从 ...

  5. UIButton 设置图片文字垂直居中排列

    #pragma mark 按钮图片文字垂直居中排列 -(void)setButtonContentCenter:(UIButton *)button { CGSize imgViewSize,titl ...

  6. JSON中的{}与[]的区别

    []:索引数组 {}:关联数组(js中,即对象)

  7. [Hbase]Hbase容灾方案

    介绍两种HBase的数据备份或者容灾方案:Snapshot,Replication: 一.Snapshot 开启快照功能,在hbase-site.xml文件中添加如下配置项: <property ...

  8. Partition Array into Disjoint Intervals LT915

    Given an array A, partition it into two (contiguous) subarrays left and right so that: Every element ...

  9. ubuntu系统ftp连接 以及ssh连接

    tfp连接 ssh连接 ubuntu下ssh使用 与 SCP 使用 1 ssh远程登录服务器 ssh username@remote_ip #将username换成自己的用户名,将remote_ip换 ...

  10. Activiti5 添加/查询审批批注(审批意见)

    Activiti5 添加/查询审批批注 Activiti 工作流开发,23张表中,act_hi_commit 中,用于保存流程审核的批注信息:  调用:   taskServer.addComment ...