zoukankan      html  css  js  c++  java
  • 2018 ICPC Asia Shenyang Regional Preliminary D. Made In Heaven

    传送门

    #include <bits/stdc++.h>
    
    using namespace std;
    using ll = long long;
    using p = pair<int, int>;
    const int inf(0x3f3f3f3f);
    const int maxn(1e3 + 10);
    const int maxm(2e5 + 10);
    int ecnt, head[maxn], rhead[maxn];
    bool vis[maxn];
    int g[maxn];
    
    struct edge {
        int to, wt, nxt;
    } edges[maxm];
    
    template<typename T = int>
    inline const T read()
    {
        T x = 0, f = 1;
        char ch = getchar();
        while (ch < '0' or ch > '9') {
            if (ch == '-') f = -1;
            ch = getchar();
        }
        while (ch >= '0' and ch <= '9') {
            x = (x << 3) + (x << 1) + ch - '0';
            ch = getchar();
        }
        return x * f;
    }
    
    template<typename T>
    inline void write(T x, bool ln)
    {
        if (x < 0) {
            putchar('-');
            x = -x;
        }
        if (x > 9) write(x / 10, false);
        putchar(x % 10 + '0');
        if (ln) putchar(10);
    }
    
    inline void addEdge(int u, int v, int w, bool r)
    {
        edges[ecnt].to = v;
        edges[ecnt].wt = w;
        if (r) {
            edges[ecnt].nxt = rhead[u];
            rhead[u] = ecnt++;
        } else {
            edges[ecnt].nxt = head[u];
            head[u] = ecnt++;
        }
    }
    
    void dijkstra(int src)
    {
        memset(g, 0x3f, sizeof g);
        g[src] = 0;
        priority_queue<p, vector<p>, greater<p>> q;
        q.push(p(0, src));
        while (not q.empty()) {
            int u = q.top().second;
            q.pop();
            if (vis[u]) continue;
            vis[u] = true;
            for (int i = rhead[u]; compl i; i = edges[i].nxt) {
                int v = edges[i].to, w = edges[i].wt;
                if (g[v] > g[u] + w) {
                    g[v] = g[u] + w;
                    q.push(p(g[v], v));
                }
            }
        }
    }
    
    int astar(int src, int des, int k)
    {
        if (src == des) ++k;
        if (g[src] == inf) return -1;
        auto cmp = [&](const p& a, const p& b) {
            return a.second + g[a.first] > b.second + g[b.first];
        };
        priority_queue<p, vector<p>, decltype(cmp)> q(cmp);
        q.push(p(src, 0));
        int cnt = 0;
        while (not q.empty()) {
            int u = q.top().first, d = q.top().second;
            q.pop();
            if (u == des and ++cnt == k) {
                return d;
            }
            for (int i = head[u]; compl i; i = edges[i].nxt) {
                int v = edges[i].to, w = edges[i].wt;
                q.push(p(v, d + w));
            }
        }
        return -1;
    }
    
    int main()
    {
    #ifndef ONLINE_JUDGE
        freopen("input.txt", "r", stdin);
    #endif
        int n, m;
        while (compl scanf("%d%d", &n, &m)) {
            ecnt = 0;
            memset(vis, false, sizeof vis);
            memset(head, -1, sizeof head);
            memset(rhead, -1, sizeof rhead);
            int s = read(), t = read(), k = read(), d = read();
            while (m--) {
                int u = read(), v = read(), w = read();
                addEdge(u, v, w, false);
                addEdge(v, u, w, true);
            }
            dijkstra(t);
            int res = astar(s, t, k);
            puts(res <= d and compl res ? "yareyaredawa" : "Whitesnake!");
        }
        return 0;
    }
    
  • 相关阅读:
    同步机制(下)
    同步机制(上)
    处理器调度
    kubernetes源码阅读笔记——Kubelet(之二)
    Kubernetes源码阅读笔记——Controller Manager(之三)
    kubernetes源码阅读笔记——Kubelet(之一)
    kubernetes源码阅读笔记——API Server(之二)
    kubernetes源码阅读笔记——API Server(之一)
    Kubernetes源码阅读笔记——Scheduler(之二)
    Kubernetes源码阅读笔记——Scheduler(之一)
  • 原文地址:https://www.cnblogs.com/singularity2u/p/14087820.html
Copyright © 2011-2022 走看看