• POJ 2449


    #include<queue>
    #include<cstdio>
    #include<string>
    #include<cstring>
    #include<iostream>
    #include<algorithm>
    using namespace std;
    const int MAXN = 1010;
    const int MAXM = 100010;
    const int INF = 0x7fffffff;
    typedef struct{
        int to, next, w;
    }Edge;
    typedef struct Node{
        int p, g, h;
        Node(int p, int g, int h){
            this->p = p;
            this->g = g;
            this->h = h;
        }
        bool operator < (const Node &node) const{
            return node.g+node.h < g+h;
        }
    }Node;
    Edge edge[MAXM << 1];
    int n, m;
    int head[MAXN], tail[MAXN], dist[MAXN];
    int cnt[MAXN], vis[MAXN];
    void addEdge(int u, int v, int w, int k){
        edge[k].to = v;
        edge[k].w = w;
        edge[k].next = head[u];
        head[u] = k++;
        edge[k].to = u;
        edge[k].w = w;
        edge[k].next = tail[v];
        tail[v] = k;
    }
    void init(int s){
        memset(vis, 0, sizeof(vis));
        memset(cnt, 0, sizeof(cnt));
        for(int i = 1;i <= n;i ++) dist[i] = INF;
        dist[s] = 0;
        vis[s] = 1;
    }
    void spfa(int s){
        init(s);
        queue<int>q;
        q.push(s);
        while(!q.empty()){
            int p = q.front();
            vis[p] = 0;
            q.pop();
            for(int i = tail[p];~i;i = edge[i].next){
                int u = edge[i].to;
                if(dist[u] > dist[p] + edge[i].w){
                    dist[u] = dist[p] + edge[i].w;
                    if(!vis[u]){
                        vis[u] = 1;
                        q.push(u);
                    }
                }
            }
        }
    }
    int aStar(int s, int t, int k){
        priority_queue<Node>Q;
        if(dist[s] == INF) return -1;
        Q.push(Node(s, 0, dist[s]));
        while(!Q.empty()){
            Node tt = Q.top();
            cnt[tt.p] ++ ;
            if(cnt[tt.p] > k) continue;
            if(cnt[tt.p] == k && tt.p == t) return tt.g+tt.h;
            Q.pop();
            for(int i = head[tt.p];~i;i = edge[i].next){
                int u = edge[i].to;
                Q.push(Node(u, tt.g+edge[i].w, dist[u]));
            }
        }
        return -1;
    }
    int main(){
        int s, t, k;
        int u, v, w;
        //freopen("in.c", "r", stdin);
        while(~scanf("%d%d", &n, &m)){
            memset(head, -1, sizeof(head));
            memset(tail, -1, sizeof(tail));
            k = 1;
            for(int i = 1;i <= m;i ++){
                scanf("%d%d%d", &u, &v, &w);
                addEdge(u, v, w, k);
                k += 2;
            }
            scanf("%d%d%d", &s, &t, &k);
            if(s == t) k++;
            spfa(t);
            int ans = aStar(s, t, k);
            printf("%d
    ", ans);
        }
        return 0;
    }
    

  • 相关阅读:
    SpringBoot项目部署与服务配置
    JDBC链接oracle已经mysql的测试
    Jedis工具类
    jsp&Sevelet基础详解
    Mysql和Oracle数据库concat()函数
    如何在maven工程中加载oracle驱动
    获取日期的相关方法
    SpringBoot配置Email发送功能
    spring中schedule注解的使用
    新建一个新的spring boot项目
  • 原文地址:https://www.cnblogs.com/anhuizhiye/p/3933147.html
Copyright © 2020-2023  润新知