• 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;
    }
  • 相关阅读:
    python读文件指定行的数据
    在linux系统中实现各项监控的关键技术(2)--内核态与用户态进程之间的通信netlink
    在linux系统中实现各项监控的关键技术(1)--cpu使用率的计算
    spring事件驱动模型--观察者模式在spring中的应用
    B2C自营商城的订单设计方案
    RabbitMQ死信队列
    springboot操作rabbitmq
    Rabbitmq--topic
    docker-compose.yml rabbitmq
    运行rabbitmq
  • 原文地址:https://www.cnblogs.com/Jadon97/p/9638912.html
Copyright © 2020-2023  润新知