poj 2449 Remmarguts' Date K短路+A*
题目链接:http://poj.org/problem?id=2449
"Good man never makes girls wait or breaks an appointment!" said the mandarin duck father. Softly touching his little ducks' head, he told them a story.
"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.
题意描述:王子和喜欢的女孩儿在不同的城堡里,王子为了见女孩儿,必须从自己的城堡走第K条最短的路径到达女孩儿所在的城堡里。求第K条最短路径的长度。
算法分析:K短路的模板题,一般运用A*算法求解。
说明:这道题的K达1000之多,应该是POJ上面这道题的数据不强吧,数据极限的时候估计A*TLE吧。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<algorithm>
#include<queue>
#define inf 0x7fffffff
using namespace std;
const int maxn=+;
const int M = +; int n,m,from,to,K;
struct Edge
{
int to,w;
int next;
}edge[M*],edge2[M*];
int head[maxn],edgenum;
int head2[maxn],edgenum2; void add(int u,int v,int w)
{
edge[edgenum].to=v ;edge[edgenum].w=w;
edge[edgenum].next=head[u] ;head[u]=edgenum++ ;
}
void add2(int u,int v,int w)
{
edge2[edgenum2].to=v ;edge2[edgenum2].w=w;
edge2[edgenum2].next=head2[u] ;head2[u]=edgenum2++ ;
} int dis[maxn],vis[maxn];
void spfa()
{
for (int i= ;i<=n ;i++) {dis[i]=inf ;vis[i]= ; }
queue<int> que;
que.push(to);
vis[to]=;
dis[to]=;
while (!que.empty())
{
int u=que.front() ;que.pop() ;
vis[u]=;
for (int i=head[u] ;i!=- ;i=edge[i].next)
{
int v=edge[i].to;
if (dis[v]>dis[u]+edge[i].w)
{
dis[v]=dis[u]+edge[i].w;
if (!vis[v])
{
vis[v]=;
que.push(v);
}
}
}
}
return ;
} struct node
{
int to,g,f;///评估函数: f=g+h;
friend bool operator < (node a,node b)
{
if (a.f != b.f) return a.f > b.f;
return a.g > b.g;
}
}cur,tail; int A_star()
{
if (from==to) K++;
if (dis[from]==inf) return -;
priority_queue<node> Q;
cur.to=from ;cur.g= ;cur.f=cur.g+dis[from];
Q.push(cur);
int cnt=;
while (!Q.empty())
{
cur=Q.top() ;Q.pop() ;
int u=cur.to;
if (u==to) cnt++;
if (cnt==K) return cur.g;
for (int i=head2[u] ;i!=- ;i=edge2[i].next)
{
tail.to=edge2[i].to;
tail.g=cur.g+edge2[i].w;
tail.f=tail.g+dis[edge2[i].to ];
Q.push(tail);
}
}
return -;
} //int flag[maxn][maxn];
int main()
{
while (scanf("%d%d",&n,&m)!=EOF)
{
memset(head,-,sizeof(head));
memset(head2,-,sizeof(head2));
edgenum=;
edgenum2=;
//memset(flag,0,sizeof(flag));
int a,b,c;
for (int i= ;i<m ;i++)
{
scanf("%d%d%d",&a,&b,&c);
//if (flag[a][b]) continue;
//flag[a][b]=1;
add(b,a,c);
add2(a,b,c);
}
scanf("%d%d%d",&from,&to,&K);
spfa();
int ans=A_star();
printf("%d\n",ans);
}
return ;
}
poj 2449 Remmarguts' Date K短路+A*的更多相关文章
- POJ 2449 Remmarguts' Date (K短路 A*算法)
题目链接 Description "Good man never makes girls wait or breaks an appointment!" said the mand ...
- POJ 2449 Remmarguts' Date --K短路
题意就是要求第K短的路的长度(S->T). 对于K短路,朴素想法是bfs,使用优先队列从源点s进行bfs,当第K次遍历到T的时候,就是K短路的长度. 但是这种方法效率太低,会扩展出很多状态,所以 ...
- 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 ...
- 图论(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短路,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 ...
随机推荐
- 四、MongoDB的查询
一.MongoDB的下载.安装与部署 二.MongoDB的基础知识简介 三.MongoDB的创建.更新和删除 db.blogs.insert([ { "author": " ...
- 如何验证 jemalloc 优化 Nginx 是否生效
Jemalloc 源于 Jason Evans 2006年在 BSDcan conference 发表的论文:<A Scalable Concurrent malloc Implementati ...
- php根据身份证号码计算年龄
代码如下 复制代码 <?php function getAgeByID($id){ //过了这年的生日才算多了1周岁 if(empty($id)) return ...
- input中如何输入逆写的中文句子
<input style="text-align:right" /><input type="text" dir="rtl" ...
- reader,字符流
1. public class Demo1 { public static void main(String[] args) throws IOException { File file = new ...
- ajax向前台输出二维数组 并解析
最近在弄一个售后数据统计的功能,里边需要统计特定时期内各种客户.机型的分布比例,单单table来计算并显示很死板(一点也不酷) 于是决定用jquery插件flot并通过ajax传输数据 :flot的折 ...
- C#高级功能(四)扩展方法和索引
扩展方法使你能够向现有类型“添加”方法,而无需创建新的派生类型.重新编译或以其他方式修改原始类型. 扩展方法是一种特殊的静态方法,但可以像扩展类型上的实例方法一样进行调用.扩展方法被定义为静态方法,但 ...
- 菜鸟学习Spring——初识Spring
一.概念. Spring是一个开源框架,Spring是于2003 年兴起的一个轻量级的Java 开发框架,由Rod Johnson 在其著作Expert One-On-One J2EE Develop ...
- oracle 分析函数(笔记)
分析函数是oracle数据库在9i版本中引入并在以后版本中不断增强的新函数种类.分析函数提供好了跨行.多层次聚合引用值的能力.分析函数所展现的效果使用传统的SQL语句也能实现,但是实现方式比较复杂,效 ...
- Node.js 异步模式浅析
注:此文是node.js实战读后的总结. 在平常的脚本语言中都是同步进行的,比如php,服务器处理多个请求的方法就是并行这些脚本.多任务处理,多线程等等.但是这种处理方式也有一个问题:每一个进程或者线 ...