POJ——2449 Remmarguts' Date
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
#include<iostream>
#include<queue>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<cstdlib>
#include<vector>
#include<cstdio>
#define il inline
#define ll long long
#define For(i,a,b) for(int (i)=(a);(i)<=(b);(i)++)
#define Bor(i,a,b) for(int (i)=(b);(i)>=(a);(i)--)
#define Max(a,b) ((a)>(b)?(a):(b))
#define Min(a,b) ((a)>(b)?(b):(a))
using namespace std;
const int N=,inf=;
int s,t,n,m,k,w[N],W[N],dis[N],ans[N],tot;
int to[N],net[N],h[N],cnt1,To[N],Net[N],H[N],cnt2;
struct node{
int f,g,id;
bool operator<(const node a)const{return f>a.f;}
};
bool vis[N];
priority_queue<node>Q; il int gi(){
int a=;char x=getchar();bool f=;
while((x<''||x>'')&&x!='-')x=getchar();
if(x=='-')x=getchar(),f=;
while(x>=''&&x<='')a=(a<<)+(a<<)+x-,x=getchar();
return f?-a:a;
} il void add(int u,int v,int c){
to[++cnt1]=v,net[cnt1]=h[u],h[u]=cnt1,w[cnt1]=c;
To[++cnt2]=u,Net[cnt2]=H[v],H[v]=cnt2,W[cnt2]=c;
} il void spfa(){
queue<int>q;
For(i,,n) dis[i]=inf;
dis[s]=,vis[s]=,q.push(s);
while(!q.empty()){
int u=q.front();vis[u]=;q.pop();
for(int i=h[u];i;i=net[i])
if(dis[to[i]]>dis[u]+w[i]){
dis[to[i]]=dis[u]+w[i];
if(!vis[to[i]])q.push(to[i]),vis[to[i]]=;
}
}
} il void Astar(){
if(dis[t]==inf) return;
node tmp;
tmp.g=,tmp.f=dis[t],tmp.id=t;
Q.push(tmp);
while(!Q.empty()){
tmp=Q.top();Q.pop();
if(tmp.id==s) {ans[++tot]=tmp.g;if(tot>=k)return;}
for(int i=H[tmp.id];i;i=Net[i]){
node tp;
tp.g=tmp.g+W[i];
tp.f=tp.g+dis[To[i]];
tp.id=To[i];
Q.push(tp);
}
}
} int main(){
while(scanf("%d%d",&n,&m)==){
memset(h,,sizeof(h));cnt1=;
memset(H,,sizeof(H));cnt2=;
int u,v,c;
For(i,,m) u=gi(),v=gi(),c=gi(),add(u,v,c);
s=gi(),t=gi(),k=gi();
if(s==t)k++;
spfa();
Astar();
if(tot<k)cout<<-;
else cout<<ans[k];
}
return ;
}
POJ——2449 Remmarguts' Date的更多相关文章
- 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 ... 
- poj 2449 Remmarguts' Date(K短路,A*算法)
		版权声明:本文为博主原创文章.未经博主同意不得转载. https://blog.csdn.net/u013081425/article/details/26729375 http://poj.org/ ... 
- POJ 2449 Remmarguts' Date
		Time Limit: 4000MS Memory Limit: 65536K Total Submissions: 30725 Accepted: 8389 Description &quo ... 
- K短路模板POJ  2449   Remmarguts' Date
		Time Limit: 4000MS Memory Limit: 65536K Total Submissions:32863 Accepted: 8953 Description &qu ... 
随机推荐
- python核心编程2 第十三章 练习
			13-3.对类进行定制.写一个类,用来将浮点型值转换为金额. class MoneyFmt(object): def __init__(self, value=0.0, flag='-'): self ... 
- 【linux基于Postfix和Dovecot邮件系统的搭建】
			一:PostFixe和Dovecot的简单介绍 Postfix postfix是Wietse Venema在IBM的GPL协议之下开发的MTA(邮件传输代理)软件.postfix是Wietse Ven ... 
- Spring常见面试题
			本文是通过收集网上各种面试指南题目及答案然后经过整理归纳而来,仅仅是为了方便以后回顾,无意冒犯各位原创作者. Spring框架 1. 什么是Spring? Spring 是个java企业级应用的开源开 ... 
- python3笔记
			python3 Python3 基本数据类型 Python 中有六个标准的数据类型: Numbers(数字) Python可以同时为多个变量赋值,如a, b = 1, 2. 一个变量可以通过赋值指向不 ... 
- sorted() ,filter() ,map()的用法
			sorted() 排序函数. 语法: sorted(Iterable, key=None, reverse=False) Iterable: 可迭代对象 key: 排序规则(排序函数), 在sorte ... 
- 2.从print到自省
			print是一个函数 为什么print是一个函数呢?可以在交互式解释器下 输入: >>> type(print) 输出: <class 'builtin_function_ ... 
- 初识python 字符串 列表 字典相关操作
			python基础(一): 运算符: 算术运算: 除了基本的+ - * / 以外,还需要知道 : // 为取整除 返回的市商的整数部分 例如: 9 // 2 ---> 4 , 9.0 // ... 
- mac制作U盘启动器
			Infi-chu: http://www.cnblogs.com/Infi-chu/ 一.所需工具及必要条件: 1. 首先需要一个大于16GB U盘. 2.电脑系统版本应该大于10.11.X(因为之前 ... 
- HBase配置和使用
			参考官方文档 整体实现框架 图1 以下几个为组成部件 21892 HMaster 22028 HRegionServer 21553 QuorumPeerMain 2366 NameNode 2539 ... 
- 初步学习pg_control文件之八
			接前文 初步学习pg_control文件之七 继续 看:catalog_version_no 代码如下: static void WriteControlFile(void) { ... /* * ... 
