题目链接: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. Dynamic view

    Views are a useful feature of SQL databases, letting us create virtual tables based on SQL select st ...

  2. BF算法和KMP算法(javascript版本)

    var str="abcbababcbababcbababcabcbaba";//主串 var ts="bcabcbaba";//子串 function BF( ...

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

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

  4. 关于编程语言(转/收藏)-原文作者:韩天峰(Rango)

    原文在这里:http://rango.swoole.com/archives/405 容易让人记住的文章,要么引起共鸣,要么催人奋进.一句话,你已走过,而我也在路上. 最近群里很多朋友询问我是如何学习 ...

  5. Nginx配置:http重定向,URLRewrite,一个简单框架的配置思路

    一个重定向的应用配置: server { listen       8000; server_name  localhost; root F:/home/projects/test; index   ...

  6. 12.python中的列表

    先看列表是如何创建的: a = ['scolia', 123] b = list('scolia',123) 同样有两种创建方式,但一般用第一种. 列表和元祖最大的不同就是列表是可以修改的. 老规矩, ...

  7. JSTL实现分页

    JSTL(JSP Standard Tag Library ,JSP标准标签库)是一个不断完善的开放源代码的JSP标签库,是由apache的jakarta小组来维护的.JSTL只能运行在支持JSP1. ...

  8. Python学习教程(learning Python)--2.3.1 Python传参函数设计

    本节主要讨论设计传递多个参数子函数的设计方法. 在2.3节里我们讨论了如何自己设计一个带参数的子函数的设计方法,现在我们研究一下如何传递两个及以上参数的设计方法. 函数为何要带参数呢?其实原因很简单, ...

  9. Ruby处理二进制(未完成)

    https://practicingruby.com/articles/binary-file-formats http://stackoverflow.com/questions/16821435/ ...

  10. 【Inno Setup】 Inno Setup 64位安装程序默认安装路径

    在脚本中加入: ArchitecturesInstallIn64BitMode=x64 ArchitecturesAllowed=x64