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\n",ans);
}

 

posted @ 2017-04-01 08:16  SiriusRen  阅读(176)  评论(0编辑  收藏  举报