poj2449第K小路径问题
Time Limit: 4000MS | Memory Limit: 65536K | |
Total Submissions: 30017 | Accepted: 8159 |
Description
"Prince Remmarguts lives in his kingdom UDF – United Delta of Freedom. One day their neighboring country sent them Princess Uyuw on a diplomatic mission."
"Erenow, the princess sent Remmarguts a letter, informing him that she would come to the hall and hold commercial talks with UDF if and only if the prince go and meet her via the K-th shortest path. (in fact, Uyuw does not want to come at all)"
Being interested in the trade development and such a lovely girl, Prince Remmarguts really became enamored. He needs you - the prime minister's help!
DETAILS: UDF's capital consists of N stations. The hall is numbered S, while the station numbered T denotes prince' current place. M muddy directed sideways connect some of the stations. Remmarguts' path to welcome the princess might include the same station twice or more than twice, even it is the station with number S or T. Different paths with same length will be considered disparate.
Input
The last line consists of three integer numbers S, T and K (1 <= S, T <= N, 1 <= K <= 1000).
Output
Sample Input
2 2
1 2 5
2 1 4
1 2 2
Sample Output
14 A* + 最短路
http://blog.csdn.net/b2b160/article/details/4057781 贴个网站 A*算法详解
估价函数 = 当前值+当前位置到终点的距离
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
using namespace std;
const int N= ;
const int M= ;
const int INF=0x3f3f3f3f;
int tot,head[N],re[N],d[N],st,ed,k;
bool vis[N];
struct nnn{
int to,next,w;
}e[M],e1[M];
void add(int u,int v,int w){
e[tot].to=v;
e[tot].next=head[u];
e[tot].w=w;
head[u]=tot;
e1[tot].to=u;
e1[tot].next=re[v];
e1[tot].w=w;
re[v]=tot++;
}
struct node{
int f,u,g;
bool operator <(const node &A)const{
return A.f<f;
}
};
void pre(){
d[ed]=;
memset(vis,,sizeof(vis));
memset(d,INF,sizeof(d));
queue<int>Q;
Q.push(ed);
d[ed]=;
while(!Q.empty()){
int u=Q.front();
Q.pop();
vis[u]=;
for(int i=re[u];i+;i=e1[i].next){
int v=e1[i].to;
if(d[v]>d[u]+e1[i].w){
d[v]=d[u]+e1[i].w;
if(!vis[v]) {
Q.push(v);
vis[v]=;
}
}
}
}
}
int slove(){
int cont=;
memset(vis,,sizeof(vis));
priority_queue<node>Q;
node p,q;
p.f=d[st],p.u=st,p.g=;
Q.push(p);
if(p.f==INF) return -;
while(!Q.empty()){
p=Q.top();
Q.pop();
if(p.u==ed){
++cont;
if(cont==k) return p.g;
}
for(int i=head[p.u];i+;i=e[i].next){
int v=e[i].to;
q.u=v;q.g=p.g+e[i].w;q.f=q.g+d[v];
Q.push(q);
}
}
return -;
}
int main(){
int n,m,u,v,w;
while(scanf("%d%d",&n,&m)!=EOF){
tot=;
memset(head,-,sizeof(head));
memset(re,-,sizeof(re));
while(m--){
scanf("%d%d%d",&u,&v,&w);
add(u,v,w);
}
scanf("%d%d%d",&st,&ed,&k);
pre();
if(st==ed) ++k;
printf("%d\n",slove());
}
}
poj2449第K小路径问题的更多相关文章
- SPOJ-COT-Count on a tree(树上路径第K小,可持久化线段树)
题意: 求树上A,B两点路径上第K小的数 分析: 同样是可持久化线段树,只是这一次我们用它来维护树上的信息. 我们之前已经知道,可持久化线段树实际上是维护的一个前缀和,而前缀和不一定要出现在一个线性表 ...
- Count on a tree(树上路径第K小)
题目链接:https://www.spoj.com/problems/COT/en/ 题意:求树上A,B两点路径上第K小的数 思路:主席树实际上是维护的一个前缀和,而前缀和不一定要出现在一个线性表上. ...
- SPOJ 10628 求树上的某条路径上第k小的点
第k小,很容易会想到用主席树来解决 这里简单想一下树的转移过程 因为本身无向图形成一棵树,那么我们总以1为根,那么之后连下去的边对应的点建立的线段树总是在父亲节点对应的树上加上一个当前点对应位置出现的 ...
- 树上第k小,可持久化线段树+倍增lca
给定一颗树,树的每个结点都有权值, 有q个询问,每个询问是 u v k ,表示u到v路径上第k小的权值是多少. 每个结点所表示的线段树,是父亲结点的线段树添加该结点的权值之后形成的新的线段树 c[ro ...
- 主席树总结(经典区间第k小问题)(主席树,线段树)
接着上一篇总结--可持久化线段树来整理吧.点击进入 这两种数据结构确实有异曲同工之妙.结构是很相似的,但维护的主要内容并不相同,主席树的离散化.前缀和等思想也要更难理解一些. 闲话 话说刚学习主席树的 ...
- [BZ4923][Lydsy1706月赛]K小值查询
K小值查询 题面 维护一个长度为n的正整数序列a_1,a_2,...,a_n,支持以下两种操作: 1 k,将序列a从小到大排序,输出a_k的值. 2 k,将所有严格大于k的数a_i减去k. Input ...
- SPOJ COT Count on a tree(树上主席树 + LCA 求点第k小)题解
题意:n个点的树,每个点有权值,问你u~v路径第k小的点的权值是? 思路: 树上主席树就是每个点建一棵权值线段树,具体看JQ博客,LCA用倍增logn求出,具体原理看这里 树上主席树我每个点的存的是点 ...
- POJ 2449 Remmarguts' Date (第k短路径)
Remmarguts' Date Time Limit: 4000MS Memory Limit: 65536K Total Submissions:35025 Accepted: 9467 ...
- spoj COT - Count on a tree (树上第K小 LCA+主席树)
链接: https://www.spoj.com/problems/COT/en/ 思路: 首先看到求两点之前的第k小很容易想到用主席树去写,但是主席树处理的是线性结构,而这道题要求的是树形结构,我们 ...
随机推荐
- Scala教程之:Option-Some-None
文章目录 Option和Some Option和None Option和模式匹配 在java 8中,为了避免NullPointerException,引入了Option,在Scala中也有同样的用法. ...
- 【三剑客】awk函数
1. 内置函数 awk的内置函数有算术.字符串.时间.位操作和其它杂项的函数. 1.1 算术函数 atan2(y,x) 返回弧度的反正切(y/x) cos(expr) 返回expr的余弦(以弧度形 ...
- CF思维联系– CodeForces - 991C Candies(二分)
ACM思维题训练集合 After passing a test, Vasya got himself a box of n candies. He decided to eat an equal am ...
- 题目分享V
题意:现在两个人做游戏,每个人刚开始都是数字1,谁赢了就能乘以k^2,输的乘以k(k可以是任意整数,每次不一定相同)现在给你最终这两个人的得分,让你判断是否有这个可能,有可能的话Yes,否则No. 分 ...
- uniapp中引入less文件
uniapp入门遇到的问题记录 在uniapp中从外部import less文件的话,首先需要在 工具>插件安装 中安装支持less语法的插件,然后在.vue文件中引入 @import url ...
- 解决Vue中文本输入框v-model双向绑定后数据不显示的问题
前言 项目中遇到一个问题就是在Vue中双向绑定对象属性时,手动赋值属性后输入框的数据不实时更新的问题. <FormItem label="地址" prop="eve ...
- 《Docker从入门到跑路》之网络模型介绍
Bridge模式 当我们安装完docker后,启动Docker daemon,就会在主机上看到一个docker0的网桥,默认在此主机上启动的容器都会连接到这个网桥上.虚拟网桥的工作方式和物理交换机的工 ...
- 面试被问为什么使用Spring Boot?答案好像没那么简单
面试官:项目中有使用Spring Boot吗? 小小白:用过. 面试官:说一下为什么要使用Spring Boot? 小小白:在使用Spring框架进行开发的过程中,需要配置很多Spring框架包的依赖 ...
- vim基础快捷键
vim快捷键 1. 移动光标 快捷键 作用 h 光标向左移动一个字符 j 光标向下移动一个字符 k 光标向上移动一个字符 l 光标向右移动一个字符 6j 向下移动6个字符 Ctrl+f 屏幕向下移动一 ...
- Web前端基础第一天
Web标准的构成 结构:结构对于网页元素进行整理和分类,现阶段主要学的是html 表现:表现用于设置元素的板式.颜色.大小等外观样式,主要指的是CSS 行为:行为是指网页模型的定义及交互的编写,现阶段 ...