POJ 2449 K短路(A*)

POJ 2449
复杂度O(nk)
注意图不连通 起始点终点为同一点的情况
#include <cstdio>
#include<queue>
#include<cstring>
using namespace std;
typedef long long LL;
const LL maxn = 1000 + 100;
const LL maxm = 200000 + 100;
const LL inf = 0x3f3f3f3f3f3f3f3f;
LL he2[maxn],ne2[maxm],ver2[maxm],tot2,cost2[maxm];
LL dist[maxn],vis[maxn];
void add2( LL x,LL y,LL c ){
    ver2[++tot2] = y;
    ne2[tot2] = he2[x];
    he2[x] = tot2;
    cost2[tot2] = c;
}
priority_queue< pair<LL,LL>,vector< pair<LL,LL> >,greater< pair<LL,LL> > > que2;
void dijkstra( LL S,LL T ){
    while( que2.size() ) que2.pop();
    dist[S] = 0;
    que2.push( make_pair( 0,S ) );
    while( que2.size() ){
        LL x = que2.top().second;
        que2.pop();
        //if( x == T ) break;
        if( vis[x] ) continue;
        vis[x] = 1;
        for( LL cure = he2[x];cure;cure = ne2[cure] ){
            LL y = ver2[cure];
            if( vis[y] ) continue;
            if( dist[y] > dist[x] + cost2[cure] ){
                dist[y] = dist[x] + cost2[cure];
                que2.push( make_pair( dist[y],y ) );
            }
        }
    }
}
LL he[maxn],ne[maxm],ver[maxm],tot,cost[maxm];
void add( LL x,LL y,LL c ){
    ver[++tot] = y;
    ne[tot] = he[x];
    he[x] = tot;
    cost[tot] = c;
}
priority_queue< pair<LL, pair<LL,LL> > ,vector< pair< LL,pair<LL,LL> > >,greater< pair< LL,pair<LL,LL> > > > que;
LL solve( LL S,LL T,LL k ){
    while( que.size() ) que.pop();
    que.push( make_pair( dist[S], make_pair( 0 , S )  ) );
    LL ans = 0;
    while( que.size() ){
        LL d = que.top().second.first;
        LL x = que.top().second.second;
        if( x == T ){
            ans++;
            if( ans == k ) return que.top().first;
        }
        que.pop();
        for( LL cure = he[x];cure;cure = ne[cure] ){
            LL y = ver[cure];
            que.push( make_pair( d+ cost[cure] + dist[ y ],make_pair( d + cost[cure] ,y ) ) );
        }
    }
    return -1;
}
void init( LL n ){
    for( LL i = 1; i <= n;i++ ){
        he[i] = he2[i] = vis[i] = 0;
        dist[i] = inf;
    }
    tot = tot2 = 1;
}
int main()
{
    LL n,m,a,b,t,S,T,k;
    while( 2 == scanf("%lld%lld",&n,&m) ){
        init( n );
        for( LL i = 1;i <= m;i++ ){
            scanf("%lld%lld%lld",&a,&b,&t);
            add( a,b,t );
            add2( b,a,t );
        }
        scanf("%lld%lld%lld",&S,&T,&k);
        if( S == T ) k++;
        dijkstra( T,S );
        if( dist[S] == inf ){
            printf("-1\n");
            continue;
        }
        LL ans = solve( S,T,k );
        printf("%lld\n",ans);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/ehdhg13455/article/details/89479548