[ACM-ICPC 2018 沈阳网络赛] D. Made In Heaven (k短路模板)

One day in the jail, F·F invites Jolyne Kujo (JOJO in brief) to play tennis with her. However, Pucci the father somehow knows it and wants to stop her. There are NN spots in the jail and MM roads connecting some of the spots. JOJO finds that Pucci knows the route of the former (K-1)(K1)-th shortest path. If Pucci spots JOJO in one of these K-1K1 routes, Pucci will use his stand Whitesnake and put the disk into JOJO's body, which means JOJO won't be able to make it to the destination. So, JOJO needs to take the KK-th quickest path to get to the destination. What's more, JOJO only has TT units of time, so she needs to hurry.

JOJO starts from spot SS, and the destination is numbered EE. It is possible that JOJO's path contains any spot more than one time. Please tell JOJO whether she can make arrive at the destination using no more than TT units of time.

Input

There are at most 5050 test cases.

The first line contains two integers NN and M(1 leq N leq 1000, 0 leq M leq 10000)(1N1000,0M10000). Stations are numbered from 11 to NN.

The second line contains four numbers S, E, KS,E,K and TT ( 1 leq S,E leq N1S,EN, S eq ES=E, 1 leq K leq 100001K10000, 1 leq T leq 1000000001T100000000 ).

Then MM lines follows, each line containing three numbers U, VU,V and W(1 leq U,V leq N, 1 leq W leq 1000)(1U,VN,1W1000) . It shows that there is a directed road from UU-th spot to VV-th spot with time WW.

It is guaranteed that for any two spots there will be only one directed road from spot AA to spot B(1 leq A,B leq N, A eq B)(1A,BN,A=B), but it is possible that both directed road <A,B><A,B> and directed road <B,A><B,A> exist.

All the test cases are generated randomly.

Output

One line containing a sentence. If it is possible for JOJO to arrive at the destination in time, output "yareyaredawa" (without quote), else output "Whitesnake!" (without quote).

样例输入

2 2
1 2 2 14
1 2 5
2 1 4

样例输出

yareyaredawa

SOLUTION:

模板题,A*求k短路
CDOE:
  1 #include <cstring>
  2 #include <cstdio>
  3 #include <queue>
  4  
  5 using namespace std;
  6  
  7 const int MAXN = 1005;
  8 const int INF = 0x3f3f3f3f;
  9  
 10 int N,M,S,K,End,T;
 11  
 12 struct Edge{
 13     int to,w,next;
 14 }E[100*MAXN],RE[100*MAXN];//正向边,Astar用。反向边,Spfa用。 
 15  
 16 int head[MAXN],rhead[MAXN];
 17 int tot;
 18  
 19 struct A{
 20     int f,g,id;//id是点的号。 
 21     //Astar核心式子:f=g+h,这里h(i)=dis[i]。 
 22     bool operator < (const A &b)const {
 23         if(f == b.f)return  g < b.g;
 24         return f > b.f;
 25     }
 26 };
 27  
 28 inline void Add(int from,int to,int w){
 29     E[++tot].to = to;
 30     E[tot].w = w;
 31     E[tot].next = head[from];
 32     head[from] = tot;
 33     RE[tot].to = from;
 34     RE[tot].w = w;
 35     RE[tot].next = rhead[to];
 36     rhead[to] = tot;
 37 }
 38  
 39 int dis[MAXN];
 40 bool used[MAXN];
 41  
 42 /*求出终点到各点的最短长度dis[i]
 43 然后用这个长度来作为后面A*算法的启发式信息h(i)。*/
 44 void Spfa(int from){
 45     memset(dis,INF,sizeof dis);
 46     //memset(used,false,sizeof used);
 47     dis[from] = 0;
 48     queue<int> Q;
 49     Q.push(from);
 50     used[from] = true;
 51     while(!Q.empty()){
 52         int t = Q.front();
 53         Q.pop();
 54         used[t] = false;
 55         for(int i=rhead[t] ; i ; i=RE[i].next){
 56             if(dis[RE[i].to] > dis[t] + RE[i].w){
 57                 dis[RE[i].to] = dis[t] + RE[i].w;
 58                 if(!used[RE[i].to]){
 59                     used[RE[i].to] = true;
 60                     Q.push(RE[i].to);
 61                 }
 62             }
 63         }
 64     }
 65 }
 66  
 67 int Astar(int from,int to){
 68     int cnt = 0;
 69     priority_queue<A> Q;
 70     if(from == to)++K;//坑点,注意判断题意中相同点算不算最短路 
 71     if(dis[from] == INF)return -1;//起点与终点不连通。
 72     A t,tt;
 73     t.id = from,t.g = 0,t.f = t.g + dis[from];
 74     Q.push(t);
 75     while(!Q.empty()){
 76         tt = Q.top();
 77         Q.pop();
 78         if(tt.id == to){
 79             ++cnt;
 80             if(cnt == K)return tt.g;//第K次到达时就是结果。 
 81         }
 82         for(int i=head[tt.id] ; i ; i=E[i].next){
 83             t.id = E[i].to;
 84             t.g = tt.g + E[i].w;
 85             t.f = t.g + dis[t.id];
 86             Q.push(t);
 87         }
 88     }
 89     return -1;//没有第K短路 
 90 }
 91  
 92 inline void init(){
 93     tot = 0;
 94     memset(head,0,sizeof head);
 95     memset(rhead,0,sizeof rhead);
 96 }
 97  
 98 int main(){
 99     
100     while(scanf("%d %d",&N,&M) == 2){
101         init();
102         scanf("%d %d %d %d",&S,&End,&K,&T);
103         int x,y,w;
104         for(int i=1 ; i<=M ; ++i){
105             scanf("%d %d %d",&x,&y,&w);
106             Add(x,y,w);
107         }
108         Spfa(End);
109         int re = Astar(S,End);
110         if(re <= T && re != -1)printf("yareyaredawa
");
111         else printf("Whitesnake!
");
112     }
113     
114     return 0;
115 }





原文地址:https://www.cnblogs.com/zhangbuang/p/11169809.html