• 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;
    }
    
  • 相关阅读:
    「题解」:07.16NOIP模拟T1:礼物
    「题解」:07.16NOIP模拟T2:通讯
    「题解」:图论专题总结
    07.07NOIP模拟赛
    [复习]平衡树splay
    「题解」:[组合数学][DP]:地精部落
    「题解」:[组合数学]:Perm 排列计数
    「题解」:[组合数学]:排队
    dp 杂题
    插头 dp 总结
  • 原文地址:https://www.cnblogs.com/poorpool/p/8320108.html
Copyright © 2020-2023  润新知