• poj2449 Remmarguts' Date K短路 A*


    K短路裸题。

    #include <algorithm>
    #include <iostream>
    #include <cstring>
    #include <cstdio>
    using namespace std;
    struct Edge{
        int too, nxt, val;
    }edge[100005];
    struct Odge{
        int too, nxt, val;
    }odge[100005];
    struct Node{
        int idd, hfc, gfc;
        bool operator<(const Node &x)const{
            return hfc+gfc>x.hfc+x.gfc;
        }
    }d[1000005];//n*k
    int n, m, s, t, k, uu, vv, ww, hea[1005], oea[1005], cnt, ont, dis[1005];
    int din, tms[1005], ans;
    bool vis[1005];
    void add_edge(int fro, int too, int val){
        edge[++cnt].nxt = hea[fro];
        edge[cnt].too = too;
        edge[cnt].val = val;
        hea[fro] = cnt;
    }
    void add_odge(int fro, int too, int val){
        odge[++ont].nxt = oea[fro];
        odge[ont].too = too;
        odge[ont].val = val;
        oea[fro] = ont;
    }
    void dijkstra(){
        memset(dis, 0x3f, sizeof(dis));
        dis[t] = 0;
        for(int i=1; i<=n; i++){
            int mini=0;
            for(int j=1; j<=n; j++)
                if(!vis[j] && dis[mini]>dis[j])
                    mini = j;
            vis[mini] = true;
            if(!mini)   return ;
            for(int i=oea[mini]; i; i=odge[i].nxt){
                int v=odge[i].too;
                dis[v] = min(dis[v], odge[i].val+dis[mini]);
            }
        }
    }
    void aStar(){
        d[++din] = (Node){s, 0, 0};
        while(din){
            Node j=d[1];
            pop_heap(d+1, d+1+din);
            din--;
            tms[j.idd]++;
            if(j.idd==t && tms[t]==k){
                printf("%d
    ", j.hfc+j.gfc);
    			return ;
            }
            if(tms[j.idd]>k)    continue;
            for(int i=hea[j.idd]; i; i=edge[i].nxt)
    			if(tms[edge[i].too]<=k){
        	        d[++din] = (Node){edge[i].too, j.hfc+edge[i].val, dis[edge[i].too]};
            	    push_heap(d+1, d+1+din);
            	}
        }
    	cout<<"-1
    ";
    }
    int main(){
        cin>>n>>m;
        for(int i=1; i<=m; i++){
            scanf("%d %d %d", &uu, &vv, &ww);
            add_edge(uu, vv, ww);
            add_odge(vv, uu, ww);
        }
    	cin>>s>>t>>k;
    	if(s==t)	k++;
        dijkstra();
        aStar();
        return 0;
    }
    
  • 相关阅读:
    CentOS 6 安装 python and pip
    Qt for Windows:Qt 5.4.0 MinGW 静态编译版本制作 (转)
    Vim 扩展工具 vim-ide (转)
    centos yum 完全卸载依赖
    Linux修改Shell命令提示符及颜色
    tmux 命令
    网络分析shell脚本(实时流量+连接统计)
    ICMP:Internet控制报文协议
    读《分布式一致性原理》zookeeper运维
    同网段和不同网段设备通信原理详解
  • 原文地址:https://www.cnblogs.com/poorpool/p/7967148.html
Copyright © 2020-2023  润新知