D.Made In Heaven

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 theseK1 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 E. 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 50 test cases.

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

The second line contains four numbers S, E, K and T (1K10000, 1T100000000 ).

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

It is guaranteed that for any two spots there will be only one directed road from spot to spot B (1A,BN,AB), but it is possible that both directed road <A,B> and directed road <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

题目来源

ACM-ICPC 2018 沈阳赛区网络预赛

题目思路:求个第k短路就好了

#include <bits/stdc++.h>

using namespace std;
const int INF = 0x7FFFFFFF;
const int MAXN = 1010;
const int MAXM = 105000;

int S, E, K, T;

struct Edge {
    int u, v, c, next, next1;
    Edge() {}
    Edge(int u, int v,int c):u(u), v(v), c(c) {}
} edge[MAXM * 4];

int first[MAXN], first1[MAXN], sign;
int n, m;
int dis[MAXN];
bool vis[MAXN];

struct Node {
    int v, c;
    Node() {}
    Node(int v, int c):v(v), c(c) {}
    bool operator < (const Node& a) const {
        return c + dis[v] > a.c + dis[a.v];
    }
};

void init() {
    memset(first, -1, sizeof(first));
    memset(first1, -1, sizeof(first1));
    sign = 0;
}
void addEdge(int u, int v, int c) {
    edge[sign] = Edge(u, v, c);
    edge[sign].next1 = first1[v];
    first1[v] = sign;
    edge[sign].next = first[u];
    first[u] = sign++;
}

priority_queue<Node>que;

void dijkstra(int s) {
    memset(vis, 0, sizeof(vis));
    for(int i = 1; i <= n; i++) {
        dis[i] = INF;
    }
    while(!que.empty()) { que.pop(); }
    dis[s] = 0;
    que.push(Node(s, 0));
    while(!que.empty()) {
        Node cur = que.top();
        que.pop();
        if(!vis[cur.v]) {
            vis[cur.v] = 1;
            dis[cur.v] = cur.c;
            for(int i = first1[cur.v]; ~i; i = edge[i].next1) {
                if(dis[edge[i].u] > dis[cur.v] + edge[i].c) {
                    que.push(Node(edge[i].u, edge[i].c + cur.c));
                }
            }
        }
    }
}

int a_star(int src) {
//    priority_queue<Node>que;
    while(!que.empty()) { que.pop(); }
    que.push(Node(src, 0));
    while(!que.empty()) {
        Node cur = que.top();
        que.pop();
        if(cur.v == E) {
            if(K > 1) {
                K--;
            }
            else {
                return cur.c;
            }
        }
        for(int i = first[cur.v]; ~i; i = edge[i].next) {
            que.push(Node(edge[i].v, cur.c + edge[i].c));
        }
    }
    return -1;
}

int main() {
    int u, v, c;
    while(~scanf("%d%d",&n, &m)) {
        init();
        scanf("%d %d %d %d", &S, &E, &K, &T);
        while(m--) {
            scanf("%d %d %d",&u, &v, &c);
            addEdge(u, v, c);
        }
        dijkstra(E);
        if(dis[S] == INF) {
            puts("Whitesnake!");
            continue;
        }
        if(S == E) {
            K++;
        }
        int res = a_star(S);
        if(res == -1) {
            puts("Whitesnake!");
            continue;
        }
        if(res <= T) {
            puts("yareyaredawa");
        } else {
            puts("Whitesnake!");
        }
    }
    return 0;
}
View Code

相关文章:

  • 2022-12-23
  • 2021-06-13
  • 2021-06-28
  • 2022-03-01
  • 2021-09-07
  • 2022-01-13
  • 2022-12-23
猜你喜欢
  • 2021-10-09
  • 2022-12-23
  • 2022-02-10
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案