UESTC 482 Charitable Exchange (线段树)

本文介绍了一个基于慈善交换概念的算法挑战,目标是最小化通过一系列价值递增交易达到指定价值物品所需的时间。文章提供了输入输出样例及解析,并详细展示了使用离散化处理和线段树来优化求解过程的技术实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Description

Have you ever heard a star charity show called Charitable Exchange? In this show, a famous star starts with a small item which values 11yuan. Then, through the efforts of repeatedly exchanges which continuously increase the value of item in hand, he (she) finally brings back a valuable item and donates it to the needy.

In each exchange, one can exchange for an item of Vi yuan if he (she) has an item values more than or equal to RiRi yuan, with a time cost of TiTi minutes.

Now, you task is help the star to exchange for an item which values more than or equal to MM yuan with the minimum time.

Input

The first line of the input is TT (no more than 2020), which stands for the number of test cases you need to solve.

For each case, two integers NNMM (1N1051≤N≤1051M1091≤M≤109) in the first line indicates the number of available exchanges and the expected value of final item. Then NN lines follow, each line describes an exchange with 33 integers ViViRiRiTiTi (1RiVi1091≤Ri≤Vi≤1091Ti1091≤Ti≤109).

Output

For every test case, you should output Case #k: first, where kk indicates the case number and counts from 11. Then output the minimum time. Output 1−1 if no solution can be found.

Sample Input


3 10 
5 1 3 
8 2 5 
10 9 2 
4 5 
2 1 1 
3 2 1 
4 3 1 
8 4 1 
5 9 
5 1 1 
10 4 10 
8 1 10 
11 6 1 
7 3 8

Sample Output

Case #1: -1 
Case #2: 4 
Case #3: 10

这题需要对于每个价值离散化,价值只有大小关系有用,具体数值是没用的。离散化后就变成了n条边,最多有200000个点,每个点的最小花费是该点到最大值的点之间的花费得的最小值,从左到右扫一便,更新和询问最小值用线段树维护。

#include<cstdio>
#include<cstring>
#include<cmath>
#include<iostream>
#include<map>
#include<queue>
#include<algorithm>
using namespace std;
struct node
{
    int l,r,v,next;
}p[200005];
long long tree[300005*2];
long long inf;
void update(int rt,int l,int r,int rr,long long v)
{
    if(l>rr||r<rr) return ;
    if(l==rr&&r==rr)
    {
        tree[rt]=v;
        return ;
    }
    int mid=(l+r)/2;
    update(2*rt,l,mid,rr,v);
    update(2*rt+1,mid+1,r,rr,v);
    tree[rt]=min(tree[2*rt+1],tree[2*rt]);
}
long long query(int rt,int l,int r,int ll,int rr)
{
    if(ll>r||rr<l) return inf;
    if(ll<=l&&rr>=r) return tree[rt];
    int mid=(l+r)/2;
    long long ret=inf;
    ret=min(ret,query(2*rt,l,mid,ll,rr));
    ret=min(ret,query(2*rt+1,mid+1,r,ll,rr));
    return ret;
}
int h[300005],vis[300005],a[300005],v[3][100005],cnt,r[300005];
int n,m,mx;

void add(int l,int r,int t)
{
    p[++cnt].l=l; p[cnt].r=r;
    p[cnt].next=h[l];h[l]=cnt;
    p[cnt].v=t;
}
void spfa(int st)
{
    int i,j;
    for(i=1;i<=mx;i++)
    {
        update(1,1,mx,i,inf);
        vis[i]=0;
        r[i]=i;
    }
    update(1,1,mx,1,0);
    for(i=1;i<=mx;i++)
    {
        int now=i;
        long long mi=query(1,1,mx,i,mx);
        for(j=h[now];j>0;j=p[j].next)
        {
            int r=p[j].r;
            long long tp=query(1,1,mx,r,mx);
            if(tp>mi+p[j].v)
            {
                update(1,1,mx,r,mi+p[j].v);
            }
        }
    }
}
int main()
{
    int T; scanf("%d",&T);
    int i,j,k;
    inf=1000000000;
    inf*=inf;
    for(int Case=1;Case<=T;Case++)
    {
    scanf("%d%d",&n,&m);
    cnt=0;memset(h,0,sizeof(h));
    map<int,int> f;
    f[m]=1;
    a[++cnt]=m;
    a[++cnt]=1;
    f[1]=1;
    for(i=1;i<=n;i++)
    {
        scanf("%d%d%d",&v[0][i],&v[1][i],&v[2][i]);
        for(j=0;j<2;j++)
        {
            if(f[v[j][i]]==0)
            {
                f[v[j][i]]=1;
                a[++cnt]=v[j][i];
            }
        }
    }
    sort(a+1,a+1+cnt);
    for(i=1;i<=cnt;i++) f[a[i]]=i;
    m=f[m];
    mx=cnt;
    cnt=0;
    for(i=1;i<=n;i++)
    {
        add(f[v[1][i]],f[v[0][i]],v[2][i]);
    }
    spfa(f[1]);
    long long ans=inf;
    ans=query(1,1,mx,m,mx);
    printf("Case #%d: ",Case);
    if(ans==inf) printf("-1\n");
    else printf("%lld\n",ans);
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值