"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
"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.
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
The last line consists of three integer numbers S, T and K (1 <= S, T <= N, 1 <= K <= 1000).
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 Input2 2
1 2 5
2 1 4
1 2 2
Sample Output
14
A*求k短路
先SPFA求出S到每个点的距离dist
然后从T往前反向A*,now表示当前走的路长,h表示估计到S的估价长度
h=now+dist[v]
然后bfs时维护按h的小根堆,当S第k次经过时,当前now即为答案
1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<algorithm> 5 #include<cmath> 6 #include<queue> 7 using namespace std; 8 typedef long long lol; 9 struct Node 10 { 11 int next,to; 12 lol dis; 13 }edge1[200001],edge2[200001]; 14 int num1,num2,head1[200001],head2[200001]; 15 lol dist[100001],ans,inf; 16 int S,T,k,tot,n,m; 17 bool vis[100001]; 18 struct ZYYS 19 { 20 lol now,h; 21 int id; 22 bool operator <(const ZYYS &b) 23 const 24 { 25 if (h==b.h) return now>b.now; 26 return h>b.h; 27 } 28 }; 29 void add1(int u,int v,lol d) 30 { 31 num1++; 32 edge1[num1].next=head1[u]; 33 head1[u]=num1; 34 edge1[num1].to=v; 35 edge1[num1].dis=d; 36 } 37 void add2(int u,int v,lol d) 38 { 39 num2++; 40 edge2[num2].next=head2[u]; 41 head2[u]=num2; 42 edge2[num2].to=v; 43 edge2[num2].dis=d; 44 } 45 void SPFA() 46 {int i; 47 queue<int>Q; 48 memset(dist,127/2,sizeof(dist)); 49 inf=dist[0]; 50 Q.push(S); 51 dist[S]=0; 52 while (Q.empty()==0) 53 { 54 int u=Q.front(); 55 Q.pop(); 56 vis[u]=0; 57 for (i=head1[u];i;i=edge1[i].next) 58 { 59 int v=edge1[i].to; 60 if (dist[v]>dist[u]+edge1[i].dis) 61 { 62 dist[v]=dist[u]+edge1[i].dis; 63 if (vis[v]==0) 64 { 65 vis[v]=1; 66 Q.push(v); 67 } 68 } 69 } 70 } 71 } 72 void Astar() 73 {int i; 74 if (S==T) k++; 75 priority_queue<ZYYS>Q; 76 ZYYS tmp; 77 if (dist[T]==inf) return; 78 tmp.id=T;tmp.now=0;tmp.h=dist[T]; 79 Q.push(tmp); 80 while (Q.empty()==0) 81 { 82 tmp=Q.top(); 83 Q.pop(); 84 if (tmp.id==S) 85 { 86 ++tot; 87 if (tot==k) 88 { 89 ans=tmp.now; 90 return; 91 } 92 } 93 for (i=head2[tmp.id];i;i=edge2[i].next) 94 { 95 int v=edge2[i].to; 96 ZYYS to; 97 to.id=v; 98 to.now=tmp.now+edge2[i].dis; 99 to.h=to.now+dist[v]; 100 Q.push(to); 101 } 102 } 103 } 104 int main() 105 {int i,u,v; 106 lol d; 107 cin>>n>>m; 108 for (i=1;i<=m;i++) 109 { 110 scanf("%d%d%lld",&u,&v,&d); 111 add1(u,v,d); 112 add2(v,u,d); 113 } 114 cin>>S>>T>>k; 115 SPFA(); 116 Astar(); 117 if (tot!=k) printf("-1"); 118 else 119 cout<<ans; 120 }