POJ——T 2449 Remmarguts' Date
http://poj.org/problem?id=2449
Time Limit: 4000MS | Memory Limit: 65536K | |
Total Submissions: 30754 | Accepted: 8394 |
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
Source
#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdio>
#include <queue> using namespace std; const int INF(0x3f3f3f3f);
const int M();
const int N();
int n,m,s,t,k;
int sumedge,had[N],hed[N];
struct Edge
{
int v,next,w;
}edge1[M<<],edge2[M<<];
inline void ins(int u,int v,int w)
{
edge1[++sumedge].v=v;
edge1[sumedge].next=hed[u];
edge1[sumedge].w=w;
hed[u]=sumedge;
edge2[sumedge].v=u;
edge2[sumedge].next=had[v];
edge2[sumedge].w=w;
had[v]=sumedge; } int dis[N];
bool inq[N];
void SPFA(int s)
{
for(int i=;i<=n;i++) dis[i]=INF;
queue<int>que; que.push(s);
inq[s]=true; dis[s]=;
for(;!que.empty();)
{
int u=que.front();
que.pop(); inq[u]=;
for(int v,i=had[u];i;i=edge2[i].next)
{
v=edge2[i].v;
if(dis[v]>dis[u]+edge2[i].w)
{
dis[v]=dis[u]+edge2[i].w;
if(!inq[v]) que.push(v),inq[v]=;
}
}
}
} struct Node
{
int g,f,to;
bool operator < (const Node x) const
{
if(f==x.f) return g>x.g;
return f>x.f;
}
};
int Astar()
{
priority_queue<Node>que;
if(dis[s]==INF) return -;
int cnt=; Node now,v;
now.f=dis[s];now.g=;now.to=s;
que.push(now);
for(;!que.empty();)
{
now=que.top(); que.pop();
if(now.to==t) cnt++;
if(cnt==k) return now.g;
for(int i=hed[now.to];i;i=edge1[i].next)
{
v.to=edge1[i].v;
v.g=now.g+edge1[i].w;
v.f=v.g+dis[edge1[i].v];
que.push(v);
}
}
return -;
} int main()
{
scanf("%d%d",&n,&m);
for(int u,v,w;m--;)
{
scanf("%d%d%d",&u,&v,&w);
ins(u,v,w);
}
scanf("%d%d%d",&s,&t,&k);
if(s==t) k++; SPFA(t);
printf("%d\n",Astar());
return ;
}
POJ——T 2449 Remmarguts' Date的更多相关文章
- 【POJ】2449 Remmarguts' Date(k短路)
http://poj.org/problem?id=2449 不会.. 百度学习.. 恩. k短路不难理解的. 结合了a_star的思想.每动一次进行一次估价,然后找最小的(此时的最短路)然后累计到k ...
- 【POJ】2449.Remmarguts' Date(K短路 n log n + k log k + m算法,非A*,论文算法)
题解 (搬运一个原来博客的论文题) 抱着板题的心情去,结果有大坑 就是S == T的时候也一定要走,++K 我发现按照论文写得\(O(n \log n + m + k \ log k)\)算法没有玄学 ...
- poj 2449 Remmarguts' Date(第K短路问题 Dijkstra+A*)
http://poj.org/problem?id=2449 Remmarguts' Date Time Limit: 4000MS Memory Limit: 65536K Total Subm ...
- poj 2449 Remmarguts' Date (k短路模板)
Remmarguts' Date http://poj.org/problem?id=2449 Time Limit: 4000MS Memory Limit: 65536K Total Subm ...
- 图论(A*算法,K短路) :POJ 2449 Remmarguts' Date
Remmarguts' Date Time Limit: 4000MS Memory Limit: 65536K Total Submissions: 25216 Accepted: 6882 ...
- poj 2449 Remmarguts' Date 第k短路 (最短路变形)
Remmarguts' Date Time Limit: 4000MS Memory Limit: 65536K Total Submissions: 33606 Accepted: 9116 ...
- POJ 2449 Remmarguts' Date (第k短路径)
Remmarguts' Date Time Limit: 4000MS Memory Limit: 65536K Total Submissions:35025 Accepted: 9467 ...
- 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短路模板题][优先队列BFS]
题目链接:http://poj.org/problem?id=2449 Time Limit: 4000MS Memory Limit: 65536K Description "Good m ...
随机推荐
- 3ds Max修改桌面快捷方式为中文语言
通过上篇教程的学习,我们已经会切换3ds Max 2018的界面语言了,相关内容可参阅3ds Max如何设置中文界面.但是如果每次都要从开始菜单进行操作,岂不麻烦?简单一点,可以在桌面添加3ds Ma ...
- 优动漫PAINT-紫藤花画法
本教程分享一篇使用优动漫PAINT绘制一树梦幻的紫藤萝花教程,原文转载自优动漫官网. 小清新紫藤萝教程,就到这里啦!有兴趣的可以尝试画一画哦,软件下载:www.dongmansoft.com/xiaz ...
- es-for-Laravel: Composer 包安装, Laravel 最简单的方式操作 Elasticsearch
composer 安装:composer require ethansmart/es-for-laravel github 地址:https://github.com/roancsu/es-for-l ...
- 紫书 习题 11-8 UVa 1663 (最大流求二分图最大基数匹配)
很奇怪, 看到网上用的都是匈牙利算法求最大基数匹配 紫书上压根没讲这个算法, 而是用最大流求的. 难道是因为第一个人用匈牙利算法然后其他所有的博客都是看这个博客的吗? 很有可能-- 回归正题. 题目中 ...
- 素数计数函数$\pi(x)\sim \Theta(\frac{x}{\log{x}})$的一个初等方法——素数定理的估计
$\DeclareMathOperator{\lcm}{lcm}$ 本文的方法来源于GTM 190:"Problems in Algebraic Number Theory",给出 ...
- 【BZOJ 1293】[SCOI2009]生日礼物
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 显然的滑动窗口题. (尺取法 如果l..i这一段已经有k种珍珠了. 那么就尝试把l++; (即把l这个影响尝试去掉一下 如果不足k种 ...
- 【BZOJ 1192】[HNOI2006]鬼谷子的钱袋
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 设k为最大的正整数满足 \(2^0+2^1+...+2^k<=m\) 如果\(m>2^0+2^1+...+2^k\) 那 ...
- POJ——T2446 Chessboard
http://poj.org/problem?id=2446 Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 18560 ...
- unity3D常见问题
总结自己在学习中遇到的问题. 记录问题,帮助他人,有什么不正确的地方欢迎指正 没有发生碰撞 两个物体(Plane和Cube)都加入了collider,当中一个加入了rigidbody,应该会产生碰撞, ...
- 将 Android* Bullet 物理引擎移植至英特尔® 架构
简单介绍 因为眼下的移动设备上可以使用更高的计算性能.移动游戏如今也可以提供震撼的画面和真实物理(realistic physics). 枪战游戏中的手雷爆炸效果和赛车模拟器中的汽车漂移效果等便是由物 ...