poj 2449 Remmarguts’ Date

Description

“Good man never makes girls wait or breaks an appointment!” said the mandarin duck father. Softly touching his little ducks’ head, he told them a story.

“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 first line contains two integer numbers N and M (1 <= N <= 1000, 0 <= M <= 100000). Stations are numbered from 1 to N. Each of the following M lines contains three integer numbers A, B and T (1 <= A, B <= N, 1 <= T <= 100). It shows that there is a directed sideway from A-th station to B-th station with time T.

The last line consists of three integer numbers S, T and K (1 <= S, T <= N, 1 <= K <= 1000).
Output

A single line consisting of a single integer number: the length (time required) to welcome Princess Uyuw using the K-th shortest path. If K-th shortest path does not exist, you should output “-1” (without quotes) instead.
Sample Input

2 2
1 2 5
2 1 4
1 2 2
Sample Output

14

题目大意:

n个点,m条边,起点s,终点t,问第k短路长度。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;
const int maxn=2000000;
struct cc{int from,to,cost;
}es1[maxn],es2[maxn];
int first1[maxn],first2[maxn];
int nxt1[maxn],nxt2[maxn];
int s,t,k;
int tot1=0;
void build1(int ff,int tt,int pp)
{es1[++tot1]=(cc){ff,tt,pp};nxt1[tot1]=first1[ff];first1[ff]=tot1;
}
int tot2=0;
void build2(int ff,int tt,int pp)
{es2[++tot2]=(cc){ff,tt,pp};nxt2[tot2]=first2[ff];first2[ff]=tot2;
}
struct heap{int u,v;
};
priority_queue<heap> q;
bool operator < (heap a,heap b)
{return a.v>b.v;
}
int vis[maxn],dis[maxn];
void dij()
{memset(dis,63,sizeof(dis));memset(vis,0,sizeof(vis));dis[t]=0;q.push((heap){t,0});while(!q.empty()){heap x=q.top(); q.pop();int u=x.u;if(!vis[u]){vis[u]=1;for(int i=first2[u];i;i=nxt2[i]){int v=es2[i].to;if(dis[v]>dis[u]+es2[i].cost){dis[v]=dis[u]+es2[i].cost;q.push((heap){v,dis[v]});}}}}
}
struct edge{long long g,h,u;
};
bool operator < (edge a,edge b)
{return (a.g+a.h)>(b.g+b.h);
};
priority_queue<edge> w ;
int cnt=0;
void astar()
{dij();w.push((edge){0,dis[s],s});while(!w.empty()){edge x=w.top();w.pop();int u=x.u;if(u==t){cnt++;if(cnt==k){printf("%d\n",x.g);return;}}for(int i=first1[u];i;i=nxt1[i]){int v=es1[i].to;edge now;now.h=dis[v];now.g=x.g+es1[i].cost;now.u=v;w.push(now);}}printf("-1\n");
}
int main()
{int n,m;scanf("%d%d",&n,&m);for(int i=1;i<=m;i++){int x,y,z;scanf("%d%d%d",&x,&y,&z);build1(x,y,z);build2(y,x,z);}scanf("%d%d%d",&s,&t,&k);if(s==t){k++;}astar();return 0;
}

poj 2449 Remmarguts' Date(第K短路)相关推荐

  1. POJ 2449 Remmarguts' Date [第k短路]

    Remmarguts' Date Time Limit: 4000MS Memory Limit: 65536KB 64bit IO Format: %lld & %llu Descripti ...

  2. POJ - 2449 Remmarguts' Date(第k短路:spfa+A*)

    题目链接:点击查看 题目大意:给出一个有向图,求第k短路 题目分析:偷学了一波A*,本来以为是多难的算法,其实就是bfs+优先队列的升级版,之前看的那些博客写的都太深奥了,以至于看了一半啥都没看懂然后 ...

  3. POJ 2449 Remmarguts' Date(第K短路 + A* + 最短路)题解

    题意:找出第k短路,输出长度,没有输出-1 思路:这题可以用A*做.A*的原理是这样,我们用一个函数:f = g + h 来表示当前点的预期步数,f代表当前点的预期步数,g代表从起点走到当前的步数,h ...

  4. POJ 2449 Remmarguts' Date(k短路模板)

    link:https://vjudge.net/problem/POJ-2449 前面输入与大多最短路题相同 最后一行输入s,t,k 求从s到t的第K短路 wiki link: https://en. ...

  5. poj 2449 Remmarguts' Date 启发式搜索 A*算法

    做这个题算是学了学spfa算法,一开始感觉spfa和dij好像:dij找最小点松弛,spfa就是一个一个的松弛,松到不能松. 求S到T的第K短路 思路:这个算法的思路是从源点S优雅的暴力跑bfs,用优 ...

  6. POJ 2449 Remmarguts' Date

    POJ_2449 一开始我的思路就是把图上每个点搞一个容量不小于K的最大堆和最小堆,最小堆用于取当前该节点的第某短路值,最大堆用来保存前K小的最短路. 最后为了每次能查询全局最小值,再把N个点放到一个 ...

  7. poj2449 Remmarguts' Date(第k短路问题)(A*+spfa/dijkstra)

    思路来源 https://blog.csdn.net/berrykanry/article/details/78345894(通俗易懂解释好评) https://www.cnblogs.com/yyf ...

  8. A*算法的认识与求第K短路模板

    现在来了解A*算法是什么 现在来解决A*求K短路问题 在一个有权图中,从起点到终点最短的路径成为最短路,第2短的路成为次短路,第3短的路成为第3短路,依此类推,第k短的路成为第k短路.那么,第k短路怎 ...

  9. Poj2449 Remmarguts' Date 【A*搜索】K短路

    http://poj.org/problem?id=2449 A*搜索求K短路. #include <cstdio> #include <cstring> #include & ...

  10. 【POJ】【2449】Remmarguts' Date

    K短路/A* 经(luo)典(ti) K短路题目= = K短路学习:http://www.cnblogs.com/Hilda/p/3226692.html 流程: 先把所有边逆向,做一遍dijkstr ...

最新文章

  1. 回调函数在MFC中的使用
  2. Java中创建对象的5种方式 -[转] http://www.codeceo.com/article/5-ways-java-create-object.html...
  3. 8.Spring Cloud Alibaba教程:整合Seata分布式事务
  4. [LUOGU] P4342 [IOI1998]Polygon
  5. android textwatcher 延时,Java-防止两次在android的textwatcher中运行
  6. “烘焙”ImageNet:自蒸馏下的知识整合
  7. java 子类中this,请问子类中的构造方法中 this(name,beijing,school);是啥意思
  8. Centos7 yum安装Python3.6环境,超简单
  9. 对待棘手bug,新手与大牛的差距在哪里?
  10. 第三次学JAVA再学不好就吃翔(part30)--继承
  11. php实现服务器文件同步,PHPstorm配置同步服务器文件
  12. css布局难,运用 CSS布局到底有多难?[多图]
  13. shell处理curl返回数据_linux shell中curl 发送post请求json格式问题的处理方法
  14. php获取用户 地区 、ip地址
  15. Request header field storeid is not allowed by Access-Control-Allow-Headers in preflight response.
  16. yytextview多种格式_YYText之图文混排
  17. 南昌航空大学计算机去年初试分数,南昌航空大学是几本?去年录取分数线是多少?...
  18. [含lw+源码等]微信小程序在线考试管理系统+后台管理系统[包运行成功]
  19. 【麦课】1~OEL的下载
  20. 用mg格式制作产品动画有什么优势?

热门文章

  1. linux grub设置cpu频率,Grub2相对Grub的一些改进和注意事项(CentOS7)
  2. java中编写多种动物叫声程序_编写Java程序,使用面向接口编程模拟不同动物的吼叫声...
  3. Go语言中的数组、切片和字符串
  4. 江西理工大学matlab考试,江西理工大学matlab测试编程题汇总.pdf
  5. 泡沫退去 看中国大数据四大如何锁定商业模式
  6. vue中产品详情图展示,左右点击切换以及图片放大缩小
  7. SAP MAM - SAP Mobile App Protection by Mocana 免费试用
  8. 【敬业福bug】支付宝五福卡敬业福太难求 被炒至200元
  9. 京东集团的5家独角兽
  10. 触控陈昊芝:“好声音”标王对移动游戏未来的预测