题目链接: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*的更多相关文章

  1. POJ 2449 Remmarguts' Date (K短路 A*算法)

    题目链接 Description "Good man never makes girls wait or breaks an appointment!" said the mand ...

  2. POJ 2449 Remmarguts' Date --K短路

    题意就是要求第K短的路的长度(S->T). 对于K短路,朴素想法是bfs,使用优先队列从源点s进行bfs,当第K次遍历到T的时候,就是K短路的长度. 但是这种方法效率太低,会扩展出很多状态,所以 ...

  3. poj 2449 Remmarguts' Date(第K短路问题 Dijkstra+A*)

    http://poj.org/problem?id=2449 Remmarguts' Date Time Limit: 4000MS   Memory Limit: 65536K Total Subm ...

  4. poj 2449 Remmarguts' Date (k短路模板)

    Remmarguts' Date http://poj.org/problem?id=2449 Time Limit: 4000MS   Memory Limit: 65536K Total Subm ...

  5. POJ 2449 - Remmarguts' Date - [第k短路模板题][优先队列BFS]

    题目链接:http://poj.org/problem?id=2449 Time Limit: 4000MS Memory Limit: 65536K Description "Good m ...

  6. 图论(A*算法,K短路) :POJ 2449 Remmarguts' Date

    Remmarguts' Date Time Limit: 4000MS   Memory Limit: 65536K Total Submissions: 25216   Accepted: 6882 ...

  7. poj 2449 Remmarguts' Date 第k短路 (最短路变形)

    Remmarguts' Date Time Limit: 4000MS   Memory Limit: 65536K Total Submissions: 33606   Accepted: 9116 ...

  8. poj 2449 Remmarguts' Date(K短路,A*算法)

    版权声明:本文为博主原创文章.未经博主同意不得转载. https://blog.csdn.net/u013081425/article/details/26729375 http://poj.org/ ...

  9. K短路模板POJ 2449 Remmarguts' Date

      Time Limit: 4000MS   Memory Limit: 65536K Total Submissions:32863   Accepted: 8953 Description &qu ...

随机推荐

  1. Linux基础知识-文件管理

    Linux目录与路径 cd:切换目录 例如:cd ~willhua,则回到用户willhua的主文件夹  cd ~或者cd,则表示回到自己的的主文件夹  cd -,则表示回到上个目录 pwd:显示目前 ...

  2. 黑白棋游戏 (codevs 2743)题解

    [问题描述] 黑白棋游戏的棋盘由4×4方格阵列构成.棋盘的每一方格中放有1枚棋子,共有8枚白棋子和8枚黑棋子.这16枚棋子的每一种放置方案都构成一个游戏状态.在棋盘上拥有1条公共边的2个方格称为相邻方 ...

  3. c#反射机制学习和利用反射获取类型信息

    反射(Reflection)是.NET中的重要机制,通过放射,可以在运行时获得.NET中每一个类型(包括类.结构.委托.接口和枚举等)的成员,包括方法.属性.事件,以及构造函数等.还可以获得每个成员的 ...

  4. 机器学习简易入门(四)- logistic回归

    摘要:使用logistic回归来预测某个人的入学申请是否会被接受 声明:(本文的内容非原创,但经过本人翻译和总结而来,转载请注明出处) 本文内容来源:https://www.dataquest.io/ ...

  5. r

    http://vgoulet.act.ulaval.ca/en/emacs/mac/ mean() 均值 sd()标准差 http://www.walware.de/it/downloads/stat ...

  6. char与 int 类型转化问题汇总

    1.char变为int时高位符号扩展问题 int main() { char a = 0x9a; int util; util = (int)a; if(util > 0) printf(&qu ...

  7. .NET开源工作流RoadFlow-流程设计-流程步骤设置-数据设置

    数据设置是控制在流程处理过程中,当前步骤的数据显示与编辑状态,控制当前步骤哪些字段为只读,隐藏或可编辑.需要配合表单设计器使用.

  8. Linux内核学习笔记——VFS

    概念: ①硬链接:若一个 inode 号对应多个文件名,则称这些文件为硬链接.即硬链接就是同一个文件使用了多个别名.硬链接可由命令 link 或 ln 创建. 其特性: 文件有相同的 inode 及 ...

  9. 调用微信退款接口时出现System.Security.Cryptography.CryptographicException: 出现了内部错误 解决办法

    我总结了一下出现证书无法加载的原因有以下三个 1.证书密码不正确,微信证书密码就是商户号 解决办法:请检查证书密码是不是和商户号一致 2.IIS设置错误,未加载用户配置文件 解决办法:找到网站使用的应 ...

  10. db2查询锁表

    --查询锁表情况,可以获取哪个表被锁,其中agent_id为哪个DB2进程锁了表(db2inst1用户下) select * from sysibmadm.LOCKS_HELD with ur; -- ...