zoukankan      html  css  js  c++  java
  • luogu2483 【模板】k短路([SDOI2010]魔法猪学院)

    模板题

    #include <iostream>
    #include <cstring>
    #include <cstdio>
    #include <queue>
    using namespace std;
    int n, m, hea[5005], cnt, uu, vv, ans;
    double e, ww, dis[5005];
    const double eps=1e-7;
    bool vis[5005];
    struct Edge{
        int too, nxt;
        double val;
    }edge[400005];
    struct Node{
        int idx;
        double hfc, gfc;
        bool operator<(const Node &x)const{
            return hfc+gfc>x.hfc+x.gfc;
        }
    }cr, cc;
    queue<int> d;
    priority_queue<Node> q;
    void add_edge(int fro, int too, double val){
        edge[++cnt].nxt = hea[fro];
        edge[cnt].too = too;
        edge[cnt].val = val;
        hea[fro] = cnt;
    }
    void spfa(){
        memset(dis, 127, sizeof(dis));
        dis[n] = 0.0;
        d.push(n);
        vis[n] = true;
        while(!d.empty()){
            int x=d.front();
            d.pop();
            vis[x] = false;
            for(int i=hea[x]; i; i=edge[i].nxt)
                if(i%2==0){
                    int t=edge[i].too;
                    if(dis[t]>dis[x]+edge[i].val){
                        dis[t] = dis[x] + edge[i].val;
                        if(!vis[t]){
                            vis[t] = true;
                            d.push(t);
                        }
                    }
                }
        }
    }
    void astar(){
        cr.idx = 1; cr.hfc = cr.gfc = 0.0;
        q.push(cr);
        while(!q.empty()){
            cc = q.top();
            q.pop();
            if(cc.hfc+cc.gfc-eps>e)	return ;
            if(cc.idx==n)	ans++, e -= cc.hfc;
            for(int i=hea[cc.idx]; i; i=edge[i].nxt)
                if(i%2){
                    cr.idx = edge[i].too;
                    cr.hfc = cc.hfc + edge[i].val;
                    cr.gfc = dis[cr.idx];
                    if(cr.hfc+cr.gfc>e)	continue;
                    q.push(cr);
                }
        }
    }
    int main(){
        cin>>n>>m>>e;
        for(int i=1; i<=m; i++){
            scanf("%d %d %lf", &uu, &vv, &ww);
            add_edge(uu, vv, ww);
            add_edge(vv, uu, ww);
        }
        spfa();
        astar();
        cout<<ans<<endl;
        return 0;
    }
    
  • 相关阅读:
    git clone 解决Permission Denied (publickey)问题
    json-server 的基本使用
    存储过程的基本使用(1)
    Linux中的yum是什么?如何配置?如何使用?
    搭建博客园皮肤
    PSCP和SCP区别和用法
    Linux 磁盘分区和挂载
    win10产生文件的哈希值
    linux下刻录iso到U盘
    jquery鼠标移入移出
  • 原文地址:https://www.cnblogs.com/poorpool/p/8320108.html
Copyright © 2011-2022 走看看