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

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


题说得挺迷的,反正就是要求 S –> T 的第k短路 有向图。
那么可以用A*算法,统计T进队的次数,第k次那么就是答案了。
h函数可以从T求一遍最短路,那么h(x) = h’(x) 精确求解。
不过要注意如果一开始S==T,不算一条路。
另外注意特判S,T 不联通且S集合存在环的情况!!不然会死循环的。。。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<vector>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<string>
#include<iomanip>
#include<ctime>
#include<climits>
#include<cctype>
#include<algorithm>
#ifdef WIN32
#define AUTO "%I64d"
#else
#define AUTO "%lld"
#endif
using namespace std;
#define smax(x,tmp) x=max((x),(tmp))
#define smin(x,tmp) x=min((x),(tmp))
#define maxx(x1,x2,x3) max(max(x1,x2),x3)
#define minn(x1,x2,x3) min(min(x1,x2),x3)
const int INF=0x3f3f3f3f;
const int maxn = 1005;
const int maxm = 100005;
struct Edge
{int to,next;int val;
}edge1[maxm],edge2[maxm];
int head1[maxn],head2[maxn];
int maxedge1,maxedge2;
inline void addedge1(int u,int v,int w)
{edge1[++maxedge1] = (Edge) { v,head1[u],w };head1[u] = maxedge1;
}
inline void addedge2(int u,int v,int w)
{edge2[++maxedge2] = (Edge) { v,head2[u],w };head2[u] = maxedge2;
}
int n,m,S,T,k;
int h[maxn];
bool inque[maxn];
void spfa(int S)
{queue <int> que;h[S]=0; inque[S]=true;que.push(S);while(!que.empty()){int u = que.front(); que.pop(); inque[u]=false;for(int i=head2[u];~i;i=edge2[i].next){int v = edge2[i].to;if(h[v] > h[u] + edge2[i].val){h[v] = h[u] + edge2[i].val;if(inque[v]) continue;inque[v] = true;que.push(v);}}}
}
struct Node
{int f,g;int id;bool operator < (const Node t) const // greater<int>{if(f ^ t.f) return f > t.f;return g > t.g;}
};
int A_star(int S,int T)
{if(h[S]==INF) return -1; // avoid the case of a circle!!int cnt = 0;priority_queue <Node> que;Node now = (Node) { h[S],0,S }; // g = 0 here!!! not h[S] too!!!!que.push(now);while(!que.empty()){Node u = que.top(); que.pop();if(u.id==T)if(++cnt == k) return u.g;for(int i=head1[u.id];~i;i=edge1[i].next){Node v;v.id = edge1[i].to;v.g = u.g + edge1[i].val;v.f = v.g + h[v.id];que.push(v);}}return -1;
}
inline void init()
{memset(head1,-1,sizeof(head1));memset(head2,-1,sizeof(head2));maxedge1 = maxedge2 = -1;memset(h,0x3f,sizeof(h));
}
int main()
{freopen("k.in","r",stdin);freopen("k.out","w",stdout);init();scanf("%d%d",&n,&m);for(int i=1;i<=m;i++){int u,v,c;scanf("%d%d%d",&u,&v,&c);addedge1(u,v,c);addedge2(v,u,c);}scanf("%d%d%d",&S,&T,&k);if(S==T) k++; // special judge!! The cost 0 isn't a path!!spfa(T);int ans = A_star(S,T);printf("%d",ans);return 0;
}

POJ 2449 Remmarguts' Date [第k短路]相关推荐

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

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

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

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

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

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

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

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

  5. POJ 2449 Remmarguts' Date

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

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

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

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

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

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

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

  9. 【POJ】【2449】Remmarguts' Date

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

最新文章

  1. python安装教程win8-python 2.7在win8.1上安装的方法
  2. Elasticsearch学习记录
  3. 计算机网络 实验六 静态路由配置,实验六-静态路由配置.doc
  4. menisa mysql_实例详细说明linux下去除重复行命令uniq
  5. 当一个事情过度的艳丽的时候就是一个衰败的开始
  6. LINQ访问DataTable
  7. 71道经典Android面试题,涵盖了所有android知识点,值得学习和思考
  8. 小心了!一大波存储厂商术语正在靠近
  9. MongoDB学习(黑马教程)-5-数据库MongoDB的验证
  10. 谷歌浏览器安装插件教程步骤,开发用这2个插件工作效率倍增
  11. 计算机应用专业配置标准,计算机应用专业技能抽查考试标准.doc
  12. UPnP和DLNA协议
  13. 利用DataEase的关联数据集制作宽表
  14. 9.5.4英语词典。设计字典记录小张新学的英文单词和中文翻译,并能根据英文来查找中文翻译,当用户输入1,按提示添加新的单词和中文;用户输入2可查找英文单词的对应中文翻译,输入3,退出程序。
  15. Python京东抢购
  16. WIN10安装与升级的方法
  17. 手机怎么压缩证件照大小?这样压缩太简单
  18. CF1637E. Best Pair
  19. 金蝶eas怎么引出凭证_金蝶EAS如何设置凭证模版
  20. 黄铮,张一鸣,宿华退居二线的背后,暗藏玄机,没那么简单

热门文章

  1. 2022中国新能源汽车客户体验价值排名:理想、小鹏、几何、极氪位居前列 | 美通社头条...
  2. 人脸识别数据集-Glint360K
  3. httpCanary app抓包
  4. idea自定义过滤器
  5. oracle模糊批量查询,Oracle 模糊查询方法
  6. 近视眼学计算机好吗6,怎么会加重眼睛的近视,近视加重与6个原因有关
  7. TS实现原生数组方法之pop()、push()、shift()、unshift()
  8. 蓝牙 linux开发板,开源双模蓝牙协议栈 - 蓝牙模组以及开发板使用介绍
  9. cmd命令查看已连接的WiFi密码
  10. exercises of nginx and images,more efforts, more happiness