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 ...
随机推荐
- jquery : eval() 解析json的注意
jquery eval解析JSON中的注意点介绍 来在:http://www.jb51.net/article/40842.htm 在JS中将JSON的字符串解析成JSON数据格式,一般有两种方式: ...
- 基于Jenkins自动构建系统开发
1 绪论 1.1 课题的研究背景 随着IT行业的不断发展,软件开发的复杂度也随着不断提高.与此同时,软件的开发团队也越来越庞大,而如何更好地协同整个团队进行高效准确的工作,从而确保软件开发的质量已经 ...
- E20180512-hm
travesal n. 横越,横断物,(横向)往返移动;
- VS2010在WIN7下安装报错“下列组件安装失败”如何解决
VS2010在WIN7下安装报错“下列组件安装失败”如何解决 http://www.111cn.net/net/42/75914.htm
- HDU1829【种类并查集】
题意: 检验给出条件是否有同性恋. 思路: 条件并查集. 还是一个类似的前缀和,sum[x]是x到根这段路径上的和,根一定是坐标越小的, 那么如果说对于同类(同一个集合)的判断就sum[a]是否等于s ...
- lightoj1026【tarjan】
题意: 据说就是找桥: 思路: 无敌RE......是cmp写挫了...现在数组开太大了 模板题: #include <bits/stdc++.h> using namespace std ...
- Codeforces714C【映射】
题意: T次操作: +的话就是往 multiset 塞进一个: -的话就是往 multiset 去除一个: ?操作 思路: +和-操作就是处理字符串直接实现一个原字符串改成"01" ...
- opencv学习资料
搜集一些基础书 数字图像处理 信号与系统 计算机视觉中的多视图几何 图像处理.分析与机器视觉 基于序列图像的视觉检测理论与方法 官网(各版本api) http://opencv.org/ opencv ...
- Unity亚洲开发者大会会议简录之技术篇,Profiler的一些参数的意思
2014Unity亚洲开发者大会会议简录之技术篇 拖公司的福,有幸去了一趟北京参加了一场Unity3D的交流盛宴,在为期两天的时间内,不仅有着技术上收获,也有心灵上的震撼.现在先来说说技术方面的一些比 ...
- PJzhang:python基础入门的7个疗程-one
猫宁!!! 参考链接:易灵微课-21天轻松掌握零基础python入门必修课-售价29元人民币 https://www.liaoxuefeng.com/wiki/1016959663602400 安全从 ...