POJ——T 2449 Remmarguts' Date

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了POJ——T 2449 Remmarguts' Date相关的知识,希望对你有一定的参考价值。

http://poj.org/problem?id=2449

Time Limit: 4000MS   Memory Limit: 65536K
Total Submissions: 30754   Accepted: 8394

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

POJ Monthly,Zeyuan Zhu
 
K短路模板
 1 #include <algorithm>
 2 #include <iostream>
 3 #include <cstring>
 4 #include <cstdio>
 5 #include <queue>
 6 
 7 using namespace std;
 8 
 9 const int INF(0x3f3f3f3f);
10 const int M(100005);
11 const int N(1005);
12 int n,m,s,t,k;
13 int sumedge,had[N],hed[N];
14 struct Edge
15 {
16     int v,next,w;
17 }edge1[M<<1],edge2[M<<1];
18 inline void ins(int u,int v,int w)
19 {
20     edge1[++sumedge].v=v;
21     edge1[sumedge].next=hed[u];
22     edge1[sumedge].w=w;
23     hed[u]=sumedge;
24     edge2[sumedge].v=u;
25     edge2[sumedge].next=had[v];
26     edge2[sumedge].w=w;
27     had[v]=sumedge;
28     
29 }
30 
31 int dis[N];
32 bool inq[N];
33 void  SPFA(int s)
34 {
35     for(int i=1;i<=n;i++) dis[i]=INF;
36     queue<int>que; que.push(s);
37     inq[s]=true; dis[s]=0;
38     for(;!que.empty();)
39     {
40         int u=que.front();
41         que.pop(); inq[u]=0;
42         for(int v,i=had[u];i;i=edge2[i].next)
43         {
44             v=edge2[i].v;
45             if(dis[v]>dis[u]+edge2[i].w)
46             {
47                 dis[v]=dis[u]+edge2[i].w;
48                 if(!inq[v]) que.push(v),inq[v]=1;
49             }
50         }
51     }
52 }
53 
54 struct Node
55 {
56     int g,f,to;
57     bool operator < (const Node x) const
58     {
59         if(f==x.f) return g>x.g;
60         return f>x.f;
61     }
62 };
63 int Astar()
64 {
65     priority_queue<Node>que;
66     if(dis[s]==INF) return -1;
67     int cnt=0; Node now,v;
68     now.f=dis[s];now.g=0;now.to=s;
69     que.push(now);
70     for(;!que.empty();)
71     {
72         now=que.top(); que.pop();
73         if(now.to==t) cnt++;
74         if(cnt==k) return now.g;
75         for(int i=hed[now.to];i;i=edge1[i].next)
76         {
77              v.to=edge1[i].v;
78             v.g=now.g+edge1[i].w;
79             v.f=v.g+dis[edge1[i].v];
80             que.push(v);
81         }
82     }
83     return -1;
84 }
85 
86 int main()
87 {
88     scanf("%d%d",&n,&m);
89     for(int u,v,w;m--;)
90     {
91         scanf("%d%d%d",&u,&v,&w);
92         ins(u,v,w);
93     }
94     scanf("%d%d%d",&s,&t,&k);
95     if(s==t) k++; SPFA(t);
96     printf("%d\n",Astar());
97     return 0;
98 }

 

以上是关于POJ——T 2449 Remmarguts' Date的主要内容,如果未能解决你的问题,请参考以下文章

POJ 2449 Remmarguts' Date(k短路模板)

poj 2449 Remmarguts' Date k短路

POJ 2449 Remmarguts' Date

POJ2449 Remmarguts' Date 第K短路

Remmarguts’ Date(poj 2449)

(最短路+A*搜索)POJ 2449 - Remmarguts' Date