您的位置:首页 > 其它

POJ 2449 Remmarguts' Date 求k短路 A*+dijkstra

2017-05-23 19:48 337 查看
Description
"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.

Input
The first line contains two integer numbers N and M (1 <= N <= 1000, 0 <= M <= 100000). Stations are numbered from 1 to N. Each of the following M lines contains three integer numbers A, B and T (1
<= A, B <= N, 1 <= T <= 100). It shows that there is a directed sideway from A-th station to B-th station with time T.

The last line consists of three integer numbers S, T and K (1 <= S, T <= N, 1 <= K <= 1000).
Output
A single line consisting of a single integer number: the length (time required) to welcome Princess Uyuw using the K-th shortest path. If K-th shortest path does not exist, you should output "-1" (without
quotes) instead.
Sample Input
2 2
1 2 5
2 1 4
1 2 2

Sample Output
14

Source

题目大意:给出n个点,m条有向边,再给出s,t,k求s~t的第k短路径。

很好的模板题目。

最短路部分其实可以写SPFA等等,,但是这里的话O(N^2)的算法就够了,比较慵懒。。

具体的我在另一篇里详细讲了。

看了一些大神的题解,综合了一下,,觉得我的代码可能会比较好理解吧。

(自信脸)

//#include<bits/stdc++.h>
#include<cstdio>
#include<queue>
#include<cstring>
using namespace std;
inline int read(){
int x=0,f=1;char ch=getchar();
while (ch<'0' || ch>'9'){if (ch=='-') f=-1;ch=getchar();}
while (ch>='0' && ch<='9'){x=x*10+ch-'0';ch=getchar();}
return x*f;
}
const int
Point=1005,
Edges=100005;
int n,m,start,end,kth;
int dist[Point],times[Point];
bool vis[Point];
struct Edge{
int to,next,val;
}E[Edges],Eopp[Edges];      //Eopp means Eopposite
int head[Point],head_opp[Point];
struct A_Star_node{
int p,g,h;
bool operator < (A_Star_node x)const{
return x.g+x.h<g+h;
}
};         //means point  and a_Star:f(x)=g(x)+h(x);
priority_queue<A_Star_node>Q;
inline void add(int Ecnt,int u,int v,int w){
E[Ecnt].next=head[u];
E[Ecnt].to=v;
E[Ecnt].val=w;
head[u]=Ecnt;
}
inline void add_opposite(int EoppCnt,int u,int v,int w){
Eopp[EoppCnt].next=head_opp[u];
Eopp[EoppCnt].to=v;
Eopp[EoppCnt].val=w;
head_opp[u]=EoppCnt;
}
void dijkstra(int s,int e){
memset(vis,0,sizeof(vis));
memset(dist,127,sizeof(dist));
int mini;	dist[e]=0;
for (int i=1;i<=n;i++){
mini=0;
for (int j=1;j<=n;j++)
if (!vis[j] && dist[mini]>dist[j])	mini=j;
vis[mini]=1;
for (int x=head_opp[mini];x;x=Eopp[x].next)
dist[Eopp[x].to]=min(dist[Eopp[x].to],dist[mini]+Eopp[x].val);
}
}
int A_Star(int s,int e){
A_Star_node t1,tmp;
memset(times,0,sizeof(times));
t1.g=t1.h=0; t1.p=s;
Q.push(t1);
while (!Q.empty()){
t1=Q.top();	Q.pop();
times[t1.p]++;
if (times[t1.p]==kth && t1.p==e) return t1.h+t1.g;
if (times[t1.p]>kth) continue;
for (int i=head[t1.p];i;i=E[i].next){
tmp.p=E[i].to;
tmp.g=dist[E[i].to];
tmp.h=E[i].val+t1.h;
Q.push(tmp);
}
}
return -1;
}
int main(){
n=read(),m=read(),kth=read(),start=read(),end=read();
int x,y,z;
memset(head,0,sizeof(head));
memset(head_opp,0,sizeof(head_opp));
for (int i=1;i<=m;i++){
x=read(),y=read(),z=read();
add(i,x,y,z);
add_opposite(i,y,x,z);
}
dijkstra(start,end);
if (start==end) kth++;
int ans=A_Star(start,end);
if (ans==-1) puts("No");
else	printf("%d\n",ans);
return 0;
}
 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: