题目链接: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. asp.net导出excel示例代码

    asp.net导出excel的简单方法. excel的操作,最常用的就是导出和导入. 本例使用NPOI实现. 代码:/// <summary> );             ;       ...

  2. Incorrect column name 'productid '

    #1166 - Incorrect column name 'productid ' 解决方法:字段是复制的吧,复制的里面应该是有空格吧?检查一下,去掉就可以了哟,呵呵.  

  3. MYSQL查询某字段中以逗号分隔的字符串的方法

    首先我们建立一张带有逗号分隔的字符串. CREATE TABLE test(id int(6) NOT NULL AUTO_INCREMENT,PRIMARY KEY (id),pname VARCH ...

  4. ASP.NET MVC5学习笔记之Action参数模型绑定值提供体系

    这一节我们关注模型绑定的值提供体系,先来介绍几个重要的接口 一. IValueProvider,接口定义如下: public interface IValueProvider { bool Conta ...

  5. 基于Memcached的Session共享问题

    把Memcached的key(Guid)写入浏览器的cookie(类比SessionId) 存值: string sessionId = Guid.NewGuid().ToString(); Comm ...

  6. python 中 input 和 raw_input 的区别

    input会假设输入的信息是合法的python表达式,例如,输入一个人名,Diesel,input会认为这是一个变量,必须加上引号,比如“Diesel”: 而raw_input会把所有的输入当作原始数 ...

  7. PagerAdapter的notifyDataSetChanged无效解决方法

    在Adapter中复写该方法: @Override public int getItemPosition(Object object) { return POSITION_NONE; } 即可~~

  8. C 网页压力测试器

    引言 <<独白>> 席慕蓉 节选一 把向你借来的笔还给你吧. 一切都发生在回首的刹那. 我的彻悟如果是缘自一种迷乱,那么,我的种种迷乱不也就只是因为一种彻悟? 在一回首间,才忽 ...

  9. Sqlserver中char,nchar,varchar与Nvarchar的区别分析

    1. char类型: 对英文(ASCII)字符占用1个字节,对一个汉字占用2个字节,CHAR存储定长数据很方便,CHAR字段上的索引效率极高,比如定义char(10),那么不论你存储的数据是否达 到了 ...

  10. Android JNI如何调用第三方库

    http://www.2cto.com/kf/201504/388764.html Android JNI找不到第三方库的解决方案 cannot load library 最近做一个jni项目,拿到的 ...