首页 技术 正文
技术 2022年11月17日
0 收藏 526 点赞 3,385 浏览 2799 个字

Charitable Exchange

Time Limit: 20 Sec

Memory Limit: 256 MB

题目连接

http://acm.uestc.edu.cn/#/problem/show/482

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 $1$ yuan. 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 $R_i$ yuan, with a time cost of $T_i$ minutes.

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

Input

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

For each case, two integers $N$, $M$ ($1 \leq N \leq 10^5$, $1 \leq M \leq 109$) in the first line indicates the number of available exchanges and the expected value of final item. Then $N$ lines follow, each line describes an exchange with $3$ integers $V_i$, $R_i$, $T_i$ ($1 \leq R_i \leq V_i \leq 10^9$, $1 \leq T_i \leq 109$).

Output

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

Sample Input

3
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

HINT

题意

每一个物品价值为x,可以由价值为r的物品来换,需要花费t秒

然后问你最小花费多少秒能够得到价值至少为m的物品

一开始你有价值为1的物品

题解:

为毛我的dp+线段树优化t了,迷的不行

正解用优先队列,然后直接bfs就好了

代码

#include <cstdio>
#include <cmath>
#include <cstring>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <set>
#include <vector>
#include <sstream>
#include <queue>
#include <typeinfo>
#include <fstream>
#include <map>
#include <stack>
typedef long long ll;
using namespace std;
//freopen("D.in","r",stdin);
//freopen("D.out","w",stdout);
#define sspeed ios_base::sync_with_stdio(0);cin.tie(0)
#define test freopen("test.txt","r",stdin)
const int maxn=;
#define mod 1000000007
#define eps 1e-9
const int inf=0x3f3f3f3f;
const ll infll = 0x3f3f3f3f3f3f3f3fLL;
inline ll read()
{
ll x=,f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
//**************************************************************************************struct node
{
ll r,v,t;
}a[maxn];
bool cmp(node aa,node bb)
{
if(aa.r==bb.r&&aa.v==bb.v)
return aa.t<bb.t;
if(aa.r==bb.r)
return aa.v<bb.v;
return aa.r<bb.r;
}
struct node2
{
ll x,t;
bool operator<(const node2 &p1)const{
return t>p1.t||t==p1.t&&x>p1.x;
}
};
ll ans=;
int n,m;
ll bfs()
{
priority_queue<node2>q;
node2 xx;
xx.x=;
xx.t=;
q.push(xx);
int B=;
int i;
ll anss=-;
while(!q.empty())
{
xx=q.top();
q.pop();
if(xx.x>=m)
{
anss=xx.t;
break;
}
for(i=B;i<=n;i++)
{
if(a[i].r>xx.x)
break;
if(a[i].r<=xx.x&&a[i].v>xx.x)
{
node2 kk;
kk.x=a[i].v;
kk.t=xx.t+a[i].t;
q.push(kk);
}
}
B=i;
}
return anss;
}
int main()
{
int t=read();
for(int cas=;cas<=t;cas++)
{
n=read(),m=read();
for(int i=;i<n;i++)
a[i].v=read(),a[i].r=read(),a[i].t=read();
sort(a,a+n,cmp);
ans=;
ans=bfs();
printf("Case #%d: %lld\n",cas,ans);
}
}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,104
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,581
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,428
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,200
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,835
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,918