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 ...
随机推荐
- c#使用DocX给word添加目录TOC
刚要做目录的时候,我就想DocX应该提供了这个方面的函数.于是我就在讨论区搜索,看看别人是怎么用的. 我搜索了catalog; catalogue; list; contents;,但是都没有和目录有 ...
- Entity Framework Extended Library (EF扩展类库,支持批量更新、删除、合并多个查询等)
E文好的可以直接看https://github.com/loresoft/EntityFramework.Extended 也可以在nuget上直接安装这个包 1.先更新VS的NuGet版本http: ...
- WIN8 下 Hyper-V和Vmware Workstation
1 管理员身份运行命令提示符 cmd bcdedit /copy {current} /d “Windows Without Hyper-V 2 记下 { } 中的代码 bcdedit /set {X ...
- echarts 各种细节问题
1.最大值最小值异常 //如果数组中的数字是字符串的形式的话,echarts计算最大最小值不正确,故将String的数字转化成Number类型 //将包含字符串的数组转化为浮点数数组 function ...
- centos查看设置端口开放状态
centos查看端口是否已开放/etc/init.d/iptables status centos开放端口/sbin/iptables -I INPUT -p tcp --dport 8000 -j ...
- 如何批量转换 WordPress 文章分类
可能建博之初,分类设置过于详细,后来想重新整理并删除一些分类项目,比如删除分类A,并将其中的所有文章划归到分类B中,手动修改文章的分类过于麻烦,有木有什么方法可以批量移动文章到另一个分类中呢? 网上闲 ...
- JDK 环境变量的配置
小编下载的是 jdk1.6 的版本 具体如何如何安装我相信大家肯定都会的了 这里就不做详细的说明了 , 我的jdk 安装的目录是在 D:\javac 这个目录下 下面我们来配置环境变量 在我的电脑- ...
- DevExpress GridControl 使用方法技巧 总结 收录整理
一.如何解决单击记录整行选中的问题 View->OptionsBehavior->EditorShowMode 设置为:Click 二.如何新增一条记录 ().gridView.AddNe ...
- Delphi中TStringList类常用属性方法详解
TStrings是一个抽象类,在实际开发中,是除了基本类型外,应用得最多的. 常规的用法大家都知道,现在来讨论它的一些高级的用法. 先把要讨论的几个属性列出来: 1.CommaText 2.Delim ...
- C#使用SQL存储过程完整流程
存储过程就是固化在SQL数据库系统内部的SQL语句,这样做的好处是可以提高执行效率.提高数据库的安全性.减少网络流量.接下来就讲解如何在数据库中建立一个存储过程. 打开SQL2055数据库,展开“数据 ...