题解:

A*求最短路

采用bfs求k短路,一个一个见过去

不会的还是看http://blog.csdn.net/jzq233jzq/article/details/68921665把

毕竟我也是抄的

代码:

#include<bits/stdc++.h>
using namespace std;
const int N=200005;
struct heap
{
    double v;
    int x;
}h[N*20];
queue<int>q;
int cnt,hsum,n,m,s,t,num,p[N],nex[N],head[N];
int x,y,vis[N],jzq,num2,p2[N],nex2[N],head2[N];
double z,c[N],c2[N],dist[N],K;
heap top(){return h[1];}
void push(heap p)
{
    h[++hsum]=p;
    for (int i=hsum;i>1;i/=2)
     if (h[i].v<=h[i/2].v)swap(h[i],h[i/2]);
    else return;
}
void pop()
{
    h[1]=h[hsum--];
    int ne;
    for (int i=1;i<hsum&&i*2<=hsum;i=ne)
     {
        if (i*2+1<=hsum&&h[i*2].v>h[i*2+1].v)ne=i*2+1;
        else ne=i*2;
        if (h[ne].v<=h[i].v)swap(h[ne],h[i]);
        else return;
     }
}
void jb(int x,int y,double z)
{
    p[++num]=y;c[num]=z;
    nex[num]=head[x];head[x]=num;
}
void jb2(int x,int y,double z)
{
    p2[++num2]=y;c2[num2]=z;
    nex2[num2]=head2[x];head2[x]=num2;
}
void spfa(int x)
{
    for (int i=1;i<=n;i++)dist[i]=2e9;
    memset(vis,0,sizeof vis);
    dist[x]=0;vis[x]=1;q.push(x);
    while (!q.empty())
     {
        int now=q.front();q.pop();
        for (int k=head2[now];k;k=nex2[k])
         if (dist[p2[k]]>dist[now]+c2[k])
          {
            dist[p2[k]]=dist[now]+c2[k];
            if (!vis[p2[k]])
             {
                q.push(p2[k]);
                vis[p2[k]]=1;
             }
          }
        vis[now]=0;
     }
}
void bfs()
{
    heap rp;rp.v=dist[s];rp.x=s;push(rp);
    while (hsum)
     {
        rp=top();pop();
        int x=rp.x;
        for (int k=head[x];k;k=nex[k])
         {
            heap pr=rp;pr.x=p[k];
            pr.v=pr.v-dist[x]+c[k]+dist[p[k]];
            push(pr);
         }
        if (x==t)
         {
            cnt++;
            if (K<rp.v)
             {
                printf("%d",cnt-1);
                jzq=0;
                return;
             }
            else K-=rp.v;
         }
     }
}
int main()
{
    scanf("%d%d%lf",&n,&m,&K);
    while (m--)
     {
        scanf("%d%d%lf",&x,&y,&z);
        jb(x,y,z);jb2(y,x,z);
     }
    jzq=1;
    s=1;t=n;
    spfa(t);
    bfs();
    if (jzq)printf("%d",cnt);
    return 0;
}

 

posted on 2017-12-20 17:42  宣毅鸣  阅读(137)  评论(0编辑  收藏  举报