POJ2449 Remmarguts' Date
"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
<= 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
(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
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<queue>
using namespace std;
typedef long long lol;
struct Node
{
int next,to;
lol dis;
}edge1[],edge2[];
int num1,num2,head1[],head2[];
lol dist[],ans,inf;
int S,T,k,tot,n,m;
bool vis[];
struct ZYYS
{
lol now,h;
int id;
bool operator <(const ZYYS &b)
const
{
if (h==b.h) return now>b.now;
return h>b.h;
}
};
void add1(int u,int v,lol d)
{
num1++;
edge1[num1].next=head1[u];
head1[u]=num1;
edge1[num1].to=v;
edge1[num1].dis=d;
}
void add2(int u,int v,lol d)
{
num2++;
edge2[num2].next=head2[u];
head2[u]=num2;
edge2[num2].to=v;
edge2[num2].dis=d;
}
void SPFA()
{int i;
queue<int>Q;
memset(dist,/,sizeof(dist));
inf=dist[];
Q.push(S);
dist[S]=;
while (Q.empty()==)
{
int u=Q.front();
Q.pop();
vis[u]=;
for (i=head1[u];i;i=edge1[i].next)
{
int v=edge1[i].to;
if (dist[v]>dist[u]+edge1[i].dis)
{
dist[v]=dist[u]+edge1[i].dis;
if (vis[v]==)
{
vis[v]=;
Q.push(v);
}
}
}
}
}
void Astar()
{int i;
if (S==T) k++;
priority_queue<ZYYS>Q;
ZYYS tmp;
if (dist[T]==inf) return;
tmp.id=T;tmp.now=;tmp.h=dist[T];
Q.push(tmp);
while (Q.empty()==)
{
tmp=Q.top();
Q.pop();
if (tmp.id==S)
{
++tot;
if (tot==k)
{
ans=tmp.now;
return;
}
}
for (i=head2[tmp.id];i;i=edge2[i].next)
{
int v=edge2[i].to;
ZYYS to;
to.id=v;
to.now=tmp.now+edge2[i].dis;
to.h=to.now+dist[v];
Q.push(to);
}
}
}
int main()
{int i,u,v;
lol d;
cin>>n>>m;
for (i=;i<=m;i++)
{
scanf("%d%d%lld",&u,&v,&d);
add1(u,v,d);
add2(v,u,d);
}
cin>>S>>T>>k;
SPFA();
Astar();
if (tot!=k) printf("-1");
else
cout<<ans;
}
POJ2449 Remmarguts' Date的更多相关文章
- [poj2449]Remmarguts' Date(spfa+A*)
转载请注明出处: http://www.cnblogs.com/fraud/ ——by fraud Remmarguts' Date Time Limit: 4000MS Mem ...
- POJ2449 Remmarguts' Date 第K短路
POJ2449 比较裸的K短路问题 K短路听起来高大上 实际思路并不复杂 首先对终点t到其他所有点求最短路 即为dist[] 然后由起点s 根据当前走过的距离+dist[]进行A*搜索 第k次到达t即 ...
- poj2449 Remmarguts' Date【A*算法】
转载请注明出处,谢谢:http://www.cnblogs.com/KirisameMarisa/p/4303855.html ---by 墨染之樱花 [题目链接]:http://poj.org/ ...
- POJ2449 Remmarguts' Date A*算法
题意是让求从st的ed第k短路... 考虑A*算法:先把终点到每个点最短路跑出来(注意要建反图),当做估价函数h(u),然后跑A* 每次取出总代价最小的,即g(u)+h(u)最小的进行扩展,注意如果u ...
- [poj2449]Remmarguts' Date(K短路模板题,A*算法)
解题关键:k短路模板题,A*算法解决. #include<cstdio> #include<cstring> #include<algorithm> #includ ...
- poj2449 Remmarguts' Date K短路 A*
K短路裸题. #include <algorithm> #include <iostream> #include <cstring> #include <cs ...
- 【POJ】【2449】Remmarguts' Date
K短路/A* 经(luo)典(ti) K短路题目= = K短路学习:http://www.cnblogs.com/Hilda/p/3226692.html 流程: 先把所有边逆向,做一遍dijkstr ...
- poj 2449 Remmarguts' Date(第K短路问题 Dijkstra+A*)
http://poj.org/problem?id=2449 Remmarguts' Date 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 ...
随机推荐
- C语言程序设计(基础)- 第6周作业
一.PTA作业 完成PTA第六周作业中4个题目的思路列在博客中. 1.7-1 高速公路超速处罚 2.7-2 计算油费 3.7-3 比较大小 4.7-4 两个数的简单计算器 (必须使用switch结构实 ...
- 团队作业7——第二次项目冲刺(Beta版本)
Deadline: 2017-12-10 23:00PM,以博客发表日期为准. 评分基准: 按时交 - 有分,检查的项目包括后文的三个方面 冲刺计划安排(单独1篇博客) 七天的敏捷冲刺(每两天发布 ...
- 结对编程作业——四则运算GUI程序
毛忠庆 201421122088 赵嘉楠 201421122065 源代码存放位置:https://gitee.com/ouwen0819/SiZeYunSuan.git 题目描述 使用 -n 参数控 ...
- C语言——第六周作业
题目 题目一:高速公路超速处罚 1.实验代码 #include <stdio.h> int main() { int speed,maxspeed; double x; scanf(&qu ...
- python中functools.singledispatch的使用
from functools import singledispatch @singledispatch def show(obj): print (obj, type(obj), "obj ...
- Scala 快速入门
 Scalable 编程语言 纯正的的面向对象语言 函数式编程语言 无缝的java互操作 scala之父 Martin Odersky 1. 函数式编程 函数式编程(functional progr ...
- 【原创】Webpack构建中hash的优化
背景: SPA的vue应用,采用webpack2构建,打包入口为main.js 输出:main模块打包成app.js,公共lib打包成vendor.js,公共样式打包成app.css,运行时依赖打包成 ...
- JAVA_SE基础——5.第一个Java程序HelloWorld&注释的应用
配置完JDK&环境变量后,我们就可以开始写程序了,那么程序怎么写呢,用什么工具呢,我建议 为了方便学习,我们最好在一个磁盘下建立一个专门的文件来写java程序,比如就在D盘下建立一个名为&qu ...
- 20170222==(MODBUS读取多个寄存器)
MODBUS读取多个寄存器(功能码04) 为了简单我这里只用4个寄存器,当让你也可以用125个寄存器,但是最多也只能用125个寄存器的.每个寄存器有上面的表知道为一个字的大小即2个字节或者叫16比特位 ...
- python识别验证码——PIL,pytesser,pytesseract的安装
1.使用Python识别验证码需要安装Python的图像处理模块(PIL.pytesser.pytesseract) (安装过程需要pip,在我的Python中已经安装pip了,pip的安装就不在赘述 ...