K短路模板POJ 2449 Remmarguts' Date
| Time Limit: 4000MS | Memory Limit: 65536K | |
| Total Submissions:32863 | Accepted: 8953 |
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<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
#include<algorithm>
#include<cmath>
#define ll long long
#define inf 1047483600
#define mod 317847191
using namespace std;
inline int read()
{
int x=,w=;char ch=getchar();
while(!isdigit(ch)){if(ch=='-') w=-;ch=getchar();}
while(isdigit(ch)) x=(x<<)+(x<<)+(ch-''),ch=getchar();
return x*w;
}
const int N=;
struct node{
int u,v,c,ne;
}e[N],e2[N];
int h[N],h2[N],tot,n,m;
void add(int u,int v,int c)
{
tot++;e[tot]=(node){u,v,c,h[u]};h[u]=tot;
e2[tot]=(node){v,u,c,h2[v]};h2[v]=tot;
}
int S,T,K;
int d[N];
bool v[N];
struct kk{
int id,f,g;
bool operator<(const kk&x)const{
if(f!=x.f) return f>x.f;
else return g>x.g;
}
};
priority_queue<kk>q;
void spfa(int s)
{
queue<int>q;
for(int i=;i<=n;++i) d[i]=inf;
d[s]=;v[s]=;q.push(s);
while(!q.empty())
{
int ff=q.front();q.pop();v[ff]=;
for(int i=h2[ff];i;i=e2[i].ne)
{
int rr=e2[i].v;
if(d[rr]>d[ff]+e2[i].c)
{
d[rr]=d[ff]+e2[i].c;
if(!v[rr]) v[rr]=,q.push(rr);
}
}
}
}
void A_star(int S,int T,int K)
{
if(S==T) K++;
int cnt=;
q.push((kk){S,,});
while(!q.empty())
{
kk ff=q.top();q.pop();
if(ff.id==T)
{
cnt++;
if(cnt==K){printf("%d",ff.f);exit();}
}
for(int i=h[ff.id];i;i=e[i].ne)
{
kk rr;rr.id=e[i].v;
rr.g=ff.g+e[i].c;
rr.f=rr.g+d[rr.id];
q.push(rr);
}
}
cout<<"-1";
}
int main()
{
n=read();m=read();
for(int i=;i<=m;++i)
{
int x,y,z;x=read();y=read();z=read();
add(x,y,z);
}
S=read();T=read();K=read();
spfa(T);
A_star(S,T,K);
return ;
}
败后或反成功,顾拂心处莫便放手。
K短路模板POJ 2449 Remmarguts' Date的更多相关文章
- 图论(A*算法,K短路) :POJ 2449 Remmarguts' Date
Remmarguts' Date Time Limit: 4000MS Memory Limit: 65536K Total Submissions: 25216 Accepted: 6882 ...
- A*算法求K短路模板 POJ 2449
#include<cstdio> #include<queue> #include<cstring> using namespace std; const int ...
- 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 ...
- POJ 2449 - Remmarguts' Date - [第k短路模板题][优先队列BFS]
题目链接:http://poj.org/problem?id=2449 Time Limit: 4000MS Memory Limit: 65536K Description "Good m ...
- 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短路 (最短路变形)
Remmarguts' Date Time Limit: 4000MS Memory Limit: 65536K Total Submissions: 33606 Accepted: 9116 ...
- poj 2449 Remmarguts' Date(K短路,A*算法)
版权声明:本文为博主原创文章.未经博主同意不得转载. https://blog.csdn.net/u013081425/article/details/26729375 http://poj.org/ ...
- POJ 2449 Remmarguts' Date (K短路 A*算法)
题目链接 Description "Good man never makes girls wait or breaks an appointment!" said the mand ...
随机推荐
- HttpClient实现通过url下载文件
其实就是通过浏览器url,点击就会下载文件. 这里是从代码层面上,对文件进行下载. package main.java.com.abp.util; import org.apache.http.*; ...
- delphi 静态3维数组。 严重占用堆栈 切记。 应该用动态数组, 非要用静态数组的话, 要在编译器里 把 堆栈 调大
delphi 代码正确, 但是运行就崩溃. 原因为 定义了 一些 静态3维数组. 应该扩大 软件的 堆栈 设置. 然后正常解决问题 静态3维数组. 严重占用堆栈 切记. 应该用动态 ...
- HttpModule 介绍
引言 Http 请求处理流程 和 Http Handler 介绍 这两篇文章里,我们首先了解了Http请求在服务器端的处理流程,随后我们知道Http请求最终会由实现了IHttpHandler接口的类进 ...
- Python笔记(二十七)_魔法方法_容器
定制容器 容器类型的协议: 定制不可变容器,只需要定义__len__()和__getitem__()方法 定制可变容器,需要定义__len__().__getitem__().__setitem__( ...
- 在layui中,新的页面怎么获取另一个页面传过来的数据,并可以对数据进行判断,layui中的后台分页(table)。
例如:打开一个新页面的同时,传数据. layer.open({ type: 2, title: '新增项目', shadeClose: false, shade: [0.3], maxmin: tru ...
- Far and away the best prize that life has given to us is the chance to work hard at work worth doing
work at:侧重于某个工作场所,或者是工作领域内研究 work on:侧重于思想上的从事于某个工作. marvel:n.漫威.奇迹 means.n.方法 tailor.n.裁缝 brighten. ...
- Hibernate异常:MappingException
异常信息: org.hibernate.MappingException: Unknown entity: com.geore.pojo.customer.Customer 造成原因: Mapping ...
- linux系统中的基础监控(硬盘,内存,系统负载,CPU,网络等)
Linux系统常见日常监控 系统信息 查看 CentOS 版本号:cat /etc/redhat-release 综合监控 nmon 系统负载 命令:w(判断整体瓶颈) 12:04:52 up 1 ...
- [19/05/04-星期六] 正则表示式(Regular Expression)
一.概念 语法: \D :就是不是0-9数字的其它字符: \W:与\w相反: a\d?b:表示在字符a和b之间可以有一个数字或者没有数字都可以:如:ab .a3b a\d+b:表示在字符a和b之间至少 ...
- Spark-Core RDD转换算子-Value型
1. map(func) 作用: 返回一个新的 RDD, 该 RDD 是由原 RDD 的每个元素经过函数转换后的值而组成. 就是对 RDD 中的数据做转换. 创建一个包含1-10的的 RDD,然后将每 ...