zoukankan      html  css  js  c++  java
  • ACM模板~求第k短路 ~~~A*+Dijkstra

    #include <map>
    #include <set>
    #include <cmath>
    #include <ctime>
    #include <stack>
    #include <queue>
    #include <cstdio>
    #include <cctype>
    #include <bitset>
    #include <string>
    #include <vector>
    #include <cstring>
    #include <iostream>
    #include <algorithm>
    #include <functional>
    #define fuck(x) cout<<"["<<x<<"]";
    #define FIN freopen("input.txt","r",stdin);
    #define FOUT freopen("output.txt","w+",stdout);
    #pragma comment(linker, "/STACK:102400000,102400000")
    using namespace std;
    typedef long long LL;
    typedef pair<int, int> PII;
    
    const int maxn = 1000 + 5;
    const int INF = 0x3f3f3f3f;
    int s, t, k;
     
    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];
        }
    };
     
    struct Edge {
        int v, cost;
        Edge (int _v = 0, int _cost = 0) : v(_v), cost(_cost) {}
    };
     
    vector<Edge>E[maxn], revE[maxn];
     
    void Dijkstra(int n, int s) {
        memset(vis, false, sizeof(vis));
        for (int i = 1; i <= n; i++) dist[i] = INF;
        priority_queue<Node>que;
        dist[s] = 0;
        que.push(Node(s, 0));
        while (!que.empty()) {
            Node tep = que.top(); que.pop();
            int u = tep.v;
            if (vis[u]) continue;
            vis[u] = true;
            for (int i = 0; i < (int)E[u].size(); i++) {
                int v = E[u][i].v;
                int cost = E[u][i].cost;
                if (!vis[v] && dist[v] > dist[u] + cost) {
                    dist[v] = dist[u] + cost;
                    que.push(Node(v, dist[v]));
                }
            }
        }
    }
     
    int astar(int s) {
        priority_queue<Node> que;
        que.push(Node(s, 0)); k--;
        while (!que.empty()) {
            Node pre = que.top(); que.pop();
            int u = pre.v;
            if (u == t) {
                if (k) k--;
                else return pre.c;
            }
            for (int i = 0; i < (int)revE[u].size(); i++) {
                int v = revE[u][i].v;
                int c = revE[u][i].cost;
                que.push(Node(v, pre.c + c));
            }
        }
        return -1;
    }
     
    void addedge(int u, int v, int w) {
        revE[u].push_back(Edge(v, w));
        E[v].push_back(Edge(u, w));
    }
     
    int main() {
    #ifndef ONLINE_JUDGE
        FIN
        #endif
        int n, m, u, v, w;
        while (scanf("%d%d", &n, &m) != EOF) {
            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);
            }
            scanf("%d%d%d", &s, &t, &k);
            Dijkstra(n, t);
            if (dist[s] == INF) {
                puts("-1");
                continue;
            }
            if (s == t) k++;
            printf("%d
    ", astar(s));
        }
        return 0;
    }
    View Code
    每一个不曾刷题的日子 都是对生命的辜负 从弱小到强大,需要一段时间的沉淀,就是现在了 ~buerdepepeqi
  • 相关阅读:
    VMwareTools安装笔记
    Oracle常用命令(持续更新)
    window常用命令(持续更新)
    Oracle 中 sys和system帐号的区别
    决策树——排序算法的理论下界
    插入、选择、冒泡、梳排序性能比较
    插入、选择、冒泡排序的综述
    绝知此事要躬行之——插入排序
    Tree 和ls 的使用
    用户目录
  • 原文地址:https://www.cnblogs.com/buerdepepeqi/p/9377647.html
Copyright © 2011-2022 走看看