POJ 2449 Remmarguts' Date
Time Limit: 4000MS | Memory Limit: 65536K | |
Total Submissions: 30725 | Accepted: 8389 |
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 <cstring>
#include <cstdio>
#include <queue>
#define M 100005
#define N 1005
using namespace std;
struct node
{
int to,f,g;
bool operator<(node a)const
{
if(f==a.f) return g>a.g;
return f>a.f;
}
};
struct Edge
{
int next,to,val;
};
Edge edge1[M*],edge2[M*];
int n,m,dist[N],head1[N],head2[N],cnt;
bool vis[N];
void spfa(int s)
{
queue<int>Q;
for(int i=;i<=n;++i) dist[i]=M,vis[i]=;
dist[s]=;
Q.push(s);
vis[s]=;
while(!Q.empty())
{
int now=Q.front();Q.pop();
vis[now]=;
for(int i=head2[now];i;i=edge2[i].next)
{
int v=edge2[i].to;
if(dist[v]>dist[now]+edge2[i].val)
{
dist[v]=dist[now]+edge2[i].val;
if(!vis[v])
{
vis[v]=;
Q.push(v);
}
}
}
}
}
inline void ins(int u,int v,int w)
{
edge1[++cnt].next=head1[u];
edge1[cnt].to=v;
edge1[cnt].val=w;
head1[u]=cnt;
edge2[cnt].next=head2[v];
edge2[cnt].to=u;
edge2[cnt].val=w;
head2[v]=cnt;
}
int Astar(int s,int t,int k)
{
priority_queue<node>Q;
if(s==t) k++;
int cnt=;
if(dist[s]==M) return -;
node a,tmp;
a.to=s;
a.g=;
a.f=a.g+dist[a.to];
Q.push(a);
while(!Q.empty())
{
node now=Q.top();Q.pop();
if(now.to==t) cnt++;
if(cnt==k) return now.g;
for(int i=head1[now.to];i;i=edge1[i].next)
{
int v=edge1[i].to;
tmp.to=v;
tmp.g=now.g+edge1[i].val ;
tmp.f=tmp.g+dist[tmp.to];
Q.push(tmp);
}
}
return -;
}
inline void init()
{
cnt=;
memset(head1,,sizeof(head1));
memset(head2,,sizeof(head2));
}
int main()
{
for(int s,t,k;cin>>n>>m;)
{
init();
for(int x,y,z;m--;)
{
cin>>x>>y>>z;
ins(x,y,z);
}
cin>>s>>t>>k;
spfa(t);
int ans=Astar(s,t,k);
cout<<ans<<endl;
}
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/ ...
- K短路模板POJ 2449 Remmarguts' Date
Time Limit: 4000MS Memory Limit: 65536K Total Submissions:32863 Accepted: 8953 Description &qu ...
随机推荐
- java调用POI读取Excel
HSSFWorkbook:是操作Excel2003以前(包括2003)的版本,扩展名是.xls:XSSFWorkbook:是操作Excel2007的版本,扩展名是.xlsx: 1.注意点 getPhy ...
- Understand中的Graphical Views使用
Graphical Views 用于浏览代码结构. 下面以dso为例 1.Butterfly 显示include关系.例: 2.Declaration 文件中的类.例: 3.UML Class Dia ...
- now code——小a和黄金街道(欧拉函数和快速幂模板)
小a和小b来到了一条布满了黄金的街道上.它们想要带几块黄金回去,然而这里的城管担心他们拿走的太多,于是要求小a和小b通过做一个游戏来决定最后得到的黄金的数量.游戏规则是这样的:假设道路长度为米(左端点 ...
- [Xcode 实际操作]九、实用进阶-(14)使用富文本CoreText框架创建丰富多彩的文本
目录:[Swift]Xcode实际操作 本文将演示如何使用富文本CoreText框架创建丰富多彩的文本图形. 在项目导航区,打开视图控制器的代码文件[ViewController.swift] imp ...
- go系列(5)- beego自己写controller
前边的系列文章已经讲述了如何安装环境, beego的处理逻辑都是在Controller里面完成的,下面就写一个最简单的Controller. 我们在写自己的controller的时候,一定要继承bee ...
- [題解](并查集)luogu_P2391 白雪皚皚
今天被老師留的作業搞死了,全是裸的水題,難題就那麼兩道我還沒寫......,狗屎 1.倒序處理,每個點至多會被更新一次 2.所以要做的就是快速找到下一個不同顏色的點, 3.然而不知道怎麼就 想到用并查 ...
- Python入门小练习
Python入门小练习 001 备份文件 Python入门小练习 002 批量下载网页链接中的图片 Python入门小练习 003 利用cookielib模拟登录获取账户信息
- 并发编程协程(Coroutine)之Gevent
并发编程协程之Gevent Gevent官网文档地址:http://www.gevent.org/contents.html 基本概念 我们通常所说的协程Coroutine其实是corporate r ...
- Metasploits之ms10_018
漏洞详情:https://technet.microsoft.com/library/security/ms10-018 一准备: 1:kali Linux系统 192.168.195.129 2:W ...
- 【aspnetcore】使用TagHelper制作分页组件
自定义TageHelper并不难,只要记住几个点: 继承TagHelper 定义需要在TagHelper中传入的参数,如果不需要参数,可忽略 重写Process方法 在Process中拼接要输出的HT ...