zoukankan      html  css  js  c++  java
  • BZOJ 1975 k短路 A*

    思路:

    直接上A*

    //By SiriusRen
    #include <queue>
    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    using namespace std;
    const int N=5005,M=200005;
    int n,m,xx[M],yy[M],first[N],next[M],v[M],tot,vis[N],ans;
    double e,w[M],zz[M],dis[N];
    void add(int x,int y,float z){w[tot]=z,v[tot]=y,next[tot]=first[x],first[x]=tot++;}
    struct Node{int now;float h,g;}fst,jy;
    bool operator<(Node a,Node b){return a.h+a.g>b.h+b.g;}
    priority_queue<Node>pq;
    void Dijkstra(){
        fst.now=n,pq.push(fst);
        for(int i=1;i<n;i++)dis[i]=0x3f3f3f3f;
        while(!pq.empty()){
            Node t=pq.top();pq.pop();
            if(vis[t.now])continue;
            vis[t.now]=1;
            for(int i=first[t.now];~i;i=next[i])
                if(dis[v[i]]>dis[t.now]+w[i])
                    dis[v[i]]=dis[t.now]+w[i],jy.now=v[i],jy.h=dis[v[i]],pq.push(jy);
        }
    }
    void A_star(){
        fst.now=1,fst.h=dis[1],pq.push(fst);
        while(!pq.empty()){
            Node t=pq.top();pq.pop();
    //      if(t.h+t.g>e)continue;
            if(t.now==n){
                if(e<t.g)return;
                else e-=t.g,ans++;
                continue;
            }
            vis[t.now]++;
            for(int i=first[t.now];~i;i=next[i]){
                jy.h=dis[v[i]];jy.g=t.g+w[i];jy.now=v[i],pq.push(jy);
            }
        }
    }
    int main(){
        memset(first,-1,sizeof(first));
        scanf("%d%d%lf",&n,&m,&e);
        for(int i=1;i<=m;i++)
            scanf("%d%d%lf",&xx[i],&yy[i],&zz[i]),add(yy[i],xx[i],zz[i]);
        Dijkstra();
        memset(first,-1,sizeof(first)),tot=0;
        for(int i=1;i<=m;i++)add(xx[i],yy[i],zz[i]);
        A_star();
        printf("%d
    ",ans);
    }
  • 相关阅读:
    1002. A+B for Polynomials
    1010. 一元多项式求导
    BZOJ3569
    BZOJ3224
    loj2291. 「THUSC 2016」补退选
    平衡树入门
    高斯消元入门
    写手vector
    矩阵乘法入门
    母函数入门
  • 原文地址:https://www.cnblogs.com/SiriusRen/p/6654397.html
Copyright © 2011-2022 走看看