zoukankan      html  css  js  c++  java
  • k短路模板

    https://acm.taifua.com/archives/jsk31445.html

    链接:

    https://nanti.jisuanke.com/t/31445

    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    #include <queue>
    #include <vector>
    using namespace std;
    const int maxn = 1010;
    const int inf = 0x3f3f3f3f;
    int n, m, s, t, k, Time;
    bool vis[maxn];
    int dist[maxn];
    
    struct Node
    {
        int v, c;
        Node (int _v = 0, int _c = 0): v(_v), c(_c) {}
        bool operator < (const Node &rhs) const
        {
            return c + dist[v] > rhs.c + dist[rhs.v]; // 估价函数 fx = gx + hx 路径短先出队
        }
    };
    
    struct Edge
    {
        int to, cost;
        Edge (int _to = 0, int _cost = 0): to(_to), cost(_cost) {}
    };
    
    vector <Edge> E[maxn], revE[maxn];
    
    void addedge(int u, int v, int w)
    {
        E[v].push_back(Edge(u, w)); // 反向加边
        revE[u].push_back(Edge(v, w)); // 正向加边
    }
    
    void dijkstra(int s, int n) // 最短路
    {
        for (int i = 0; i <= n; ++i)
        {
            vis[i] = false;
            dist[i] = inf;
        }
        dist[s] = 0;
        priority_queue <Node> Q;
        Q.push(Node(s, dist[s]));
        while (!Q.empty())
        {
            Node tmp = Q.top();
            Q.pop();
            int u = tmp.v;
            if (vis[u])
                continue;
            vis[u] = true;
            for (int i = 0; i < E[u].size(); ++i)
            {
                int v = E[u][i].to, cost = E[u][i].cost;
                if (!vis[v] && dist[v] > dist[u] + cost)
                {
                    dist[v] = dist[u] + cost;
                    Q.push(Node(v, dist[v]));
                }
            }
        }
    }
    
    int astar(int s)
    {
        if (dist[s] == inf) // 不能到达
            return -1;
        priority_queue <Node> Q;
        Q.push(Node(s, 0));
        k--;
        while (!Q.empty())
        {
            Node tmp = Q.top();
            Q.pop();
            int u = tmp.v;
            if (tmp.c + dist[u] > Time) // 超时直接返回-1,可不要
                return  -1;
            if (u == t)
            {
                if (k)
                    --k;
                else  // 第k次到达目标节点t
                    return tmp.c;
            }
            for (int i = 0; i < revE[u].size(); ++i)
            {
                int v = revE[u][i].to, cost = revE[u][i].cost;
                Q.push(Node(v, tmp.c + cost));
            }
        }
        return -1;
    }
    
    int main()
    {
        int u, v, w;
        while (scanf("%d%d", &n, &m) != EOF)
        {
            scanf("%d%d%d%d", &s, &t, &k, &Time);
            for (int i = 0; i <= n; ++i)
            {
                E[i].clear();
                revE[i].clear();
            }
            for (int i = 0; i < m; ++i)
            {
                scanf("%d%d%d", &u, &v, &w);
                addedge(u, v, w);
            }
            dijkstra(t, n); // t点到所有点的最短路
            int ans = astar(s);
            if (ans == -1 || ans > Time) // 无法到达或者超过时间
                printf("Whitesnake!
    ");
            else
                printf("yareyaredawa
    ");
        }
        return 0;
    }
  • 相关阅读:
    [leetcode] Delete Operation for Two Strings
    [leetcode] Minimum ASCII Delete Sum for Two Strings
    [leetcode] Palindromic Substrings
    [leetcode] Student Attendance Record I
    [leetcode] Reverse String II
    [leetcode] Diameter of Binary Tree
    [leetcode] Climbing Stairs
    [leetcode] Range Sum Query
    Codeforces 1294A Collecting Coins
    团体程序设计天梯赛 L2-021 点赞狂魔 (25分)
  • 原文地址:https://www.cnblogs.com/Jadon97/p/9638912.html
Copyright © 2011-2022 走看看