uestc 482 Charitable Exchange 广搜+优先队列优化

用优先队列对花费时间进行从大到小排序

#include<bits/stdc++.h>
using namespace std;
const int N=100000+1;
typedef long long ll;
struct edge
{
    ll u,v,w;
}g[N];
struct node
{
    ll m,t;
    friend bool operator <(node n1,node n2)
    {
        return n2.t<n1.t;
    }
};
int n;
int cmp(edge a,edge b)
{
    return a.v<b.v;
}

ll bfs(ll m)
{
    int i,st=0;
    priority_queue<node>pq;
    node o,p;
    p.m=1;
    p.t=0;
    pq.push(p);
    while(!pq.empty())
    {
        p=pq.top();
        pq.pop();
        if(p.m>=m) return p.t;
        for(i=st;i<n;i++)
        {
            if(g[i].v>p.m) break;
            if(g[i].u<=p.m) continue;
            o.m=g[i].u;
            o.t=p.t+g[i].w;
            pq.push(o);
        }
        st=i;
    }
    return -1;
}

int main()
{
    int _,i,cas=0;
    ll m;
    scanf("%d",&_);
    while(_--)
    {
        scanf("%d%lld",&n,&m);
        for(i=0;i<n;i++)
            scanf("%lld%lld%lld",&g[i].u,&g[i].v,&g[i].w);
        sort(g,g+n,cmp);
        printf("Case #%d: %lld\n",++cas,bfs(m));
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值