HDU 6386 Age of Moyu

题目:Age of Moyu

思路:裸的堆优化dijkstra

代码:

#include<bits/stdc++.h>
using namespace std;

#define maxn 200000
#define inf (1LL<<40)
#define LL long long

struct Pair{
    LL y,z,col;
    Pair(){}
    Pair(LL yy,LL zz,LL c=0) {
        y=yy,z=zz,col=c;
    }
    bool operator < (const Pair oth) const {
        return z>oth.z;
    }
};

LL n,m;
vector<Pair> a[maxn+5];
bool Use[maxn+5]={0};

void readin() {
    for(LL i=1;i<=m;i++) {
        LL x,y,z;
        scanf("%lld%lld%lld",&x,&y,&z);
        a[x].push_back(Pair(y,z));
        a[y].push_back(Pair(x,z));
    }
}

void init() {
    for(LL i=1;i<=n;i++) a[i].clear();
}

LL dijkstra(){
    LL dist[maxn+5];
    for(LL i=1;i<=n;i++) dist[i]=inf;
    dist[1]=0;
    Use[1]=0;
    priority_queue<Pair> que;
    que.push(Pair(1,0));
    while(!que.empty()) {
        Pair h=que.top();
        que.pop();
        if(Use[h.y]) continue;
        for(LL i=0;i<a[h.y].size();i++){
            Pair x=a[h.y][i];
            if((h.col!=x.z)+h.z<dist[x.y]) {
                dist[x.y]=(h.col!=x.z)+h.z;
                que.push(Pair(x.y,dist[x.y],x.z));
            }
        }
    }
    return dist[n];
}

int main() {
    while(~scanf("%lld%d",&n,&m)) {
        init();
        readin();
        LL ans=dijkstra();
        if(ans==inf) ans=-1;
        printf("%lld\n",ans);
    }

    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值