hdu 6705 path

题意:找第k短的路径

思路:优先队列bfs(启发式搜索)

#include <bits/stdc++.h>
#define fi first
#define se second
using namespace std;
typedef long long LL;
typedef int lint;
const int maxk = 50005;
const int maxn = 50005;
const int maxq = 50005;
struct type{
    LL v;
    int x,id;
    type( LL vv = 0,int xx = 0,int idid= 0 ){
        v = vv; x = xx;id = idid;
    }
    bool operator < ( const type& b )const{
        return v >b.v;
    }
};
typedef pair<LL,int> pii;
vector<pii> ve[maxn];
LL ans[maxk];
int query[maxq];
priority_queue<type> que;

void init( int n ){
    while(que.size()) que.pop();
    for( int i =0 ;i <= n;i++ ) ve[i].clear();
}

int main(){
    int t;
    scanf("%d",&t);
    while(t--){
        int n,m,q,x,y;
        LL w;
        scanf("%d%d%d",&n,&m,&q);
        init(n);
        for( int i = 1;i <= m;i++ ){
            scanf("%d%d%lld",&x,&y,&w);
            ve[x].push_back( pii( w,y ) );
        }
        int mx = 0;
        for( int i = 1;i <= q;i++ ){
            scanf("%d",&query[i]);
            mx = max( mx,query[i] );
        }
        for( int i = 1;i <= n;i++ ){
            sort( ve[i].begin(),ve[i].end() );
        }
        for( int i = 1;i <= n;i++ ){
            if( ve[i].size() )
            que.push( type( ve[i][0].fi,i,0 ) );
        }
        int cnt = 0;
        while( que.size() ){
            type cur = que.top();
            que.pop();
            int x = cur.x;
            int id = cur.id;
            LL v = cur.v;
            if(v)
                ans[++cnt] = cur.v;
            if( cnt == mx )
                break;
            if( id < (int)ve[x].size()-1 )
                que.push( type( v-ve[x][id].fi + ve[x][id+1].fi,x ,id+1  ) );
            int y = ve[x][id].se;
            if( ve[y].size() )
            que.push( type( v+ve[y][0].fi,y,0 ) );
        }
        for( int i = 1;i <= q;i++ ){
            printf("%lld\n",ans[ query[i] ]   );
        }
    }
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值