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);
}
}

CDOJ 482 Charitable Exchange bfs的更多相关文章

  1. UESTC 482 Charitable Exchange(优先队列+bfs)

    Charitable Exchange Time Limit: 4000/2000MS (Java/Others)     Memory Limit: 65535/65535KB (Java/Othe ...

  2. cdoj 482 优先队列+bfs

    Charitable Exchange Time Limit: 4000/2000MS (Java/Others)     Memory Limit: 65535/65535KB (Java/Othe ...

  3. 【UESTC 482】Charitable Exchange(优先队列+bfs)

    给你n个物品交换,每个交换用r,v,t描述,代表需要用r元的东西花费t时间交换得v元的东西.一开始只有1元的东西,让你求出交换到价值至少为m的最少时间代价.相当于每个交换是一条边,时间为边权,求走到价 ...

  4. 线段树总结 (转载 里面有扫描线类 还有NotOnlySuccess线段树大神的地址)

    转载自:http://blog.csdn.net/shiqi_614/article/details/8228102 之前做了些线段树相关的题目,开学一段时间后,想着把它整理下,完成了大牛NotOnl ...

  5. [转载]完全版线段树 by notonlysuccess大牛

    原文出处:http://www.notonlysuccess.com/ (好像现在这个博客已经挂掉了,在网上找到的全部都是转载) 今天在清北学堂听课,听到了一些很令人吃惊的消息.至于这消息具体是啥,等 ...

  6. 【转】线段树完全版~by NotOnlySuccess

    线段树完全版  ~by NotOnlySuccess 很早前写的那篇线段树专辑至今一直是本博客阅读点击量最大的一片文章,当时觉得挺自豪的,还去pku打广告,但是现在我自己都不太好意思去看那篇文章了,觉 ...

  7. 《完全版线段树》——notonlysuccess

    转载自:NotOnlySuccess的博客 [完全版]线段树 很早前写的那篇线段树专辑至今一直是本博客阅读点击量最大的一片文章,当时觉得挺自豪的,还去pku打广告,但是现在我自己都不太好意思去看那篇文 ...

  8. 【转】 线段树完全版 ~by NotOnlySuccess

    载自:NotOnlySuccess的博客 [完全版]线段树 很早前写的那篇线段树专辑至今一直是本博客阅读点击量最大的一片文章,当时觉得挺自豪的,还去pku打广告,但是现在我自己都不太好意思去看那篇文章 ...

  9. 【转载】完全版线段树 by notonlysuccess大牛

    原文出处:http://www.notonlysuccess.com/ 今晚上比赛就考到了 排兵布阵啊,难受. [完全版]线段树 很早前写的那篇线段树专辑至今一直是本博客阅读点击量最大的一片文章,当时 ...

随机推荐

  1. why dicePlayer cannot player with defy mb526

    硬件加速视频播放器 DicePlayer v2.0.38 ... ..... ...... ........ \ 局限性:- 视频兼容性依赖于您设备的视频硬解码能力

  2. Oracle 创建和使用视图

    一.what(什么是视图?) 1.视图是一种数据库对象,是从一个或者多个数据表或视图中导出的虚表,视图所对应的数据并不真正地存储在视图中,而是存储在所引用的数据表中,视图的结构和数据是对数据表进行查询 ...

  3. [转] AE中如何由IFeature 如何获取所对应的FeatureClass

    转载的原文 AE中如何由IFeature 如何获取所对应的FeatureClass   先获取FeatureClass,然后遍历Map中所有的FeatureLayer,然后比较 FeatureClas ...

  4. Map/Reduce之间的Partitioner接口

    一.Partitioner介绍 Partitioner的作用是对Mapper产生的中间结果进行分片,以便将同一分组的数据交给同一个Reduce处理,它直接影响Reduce阶段的负载均衡(个人理解:就是 ...

  5. Matlab编程实例(1) 移动平均

    MATLAB数字信号处理作业,把自己写的程序发上来..欢迎交流~ QQ 五幺九七九零六四   首先是任意点移动平均: 主程序:mov_average_main.m (运行) 函数:mov_averag ...

  6. 让Apache支持ASP.NET

    Apache是目前广泛使用的一种网络服务器程序,不仅在UNIX/LINUX平台上被大量使用,而且在Windows平台上也有许多站点放弃了IIS 而转向Apache..NET是微软推出的功能强大的开发技 ...

  7. nodejs写的一个网页爬虫例子(坏链率)

    因为工作需要,用nodejs写了个简单的爬虫例子,之前也没用过nodejs,连搭环境加写大概用了5天左右,so...要多简陋有多简陋,放这里给以后的自己看~~ 整体需求是:给一个有效的URL地址,返回 ...

  8. 取消本地SVN文件夹与服务器关联

    问题:之前建了一个SVN代码库,同步了代码上去,但中途发现建库时的规则搞错了,就把服务器上的库给删了重建,然后改变本地代码的svn服务器关联地址,但使用Relocate一直报错. 错误有两种情况:1. ...

  9. socket.io的抽象实现:engine.io

    engine.io是一个socket.io的抽象实现,作为socket.io的服务器和浏览器之间交换的数据的传输层.它不会取代Socket.IO,它只是抽象出固有的复杂性,支持多种浏览器,设备和网络的 ...

  10. 第二百六十三天 how can I 坚持

    今天解脱了,放下了,小罗娜,不给力,话说下一步该咋办呢. 鱼不想过双十一自杀了.这都二十二号了,好快. 该把给罗娜说的那些话保存下来.可惜已经删了. 不知道做的对不对,反正就是没缘分,就这样吧. 睡觉 ...