Charitable Exchange

Time Limit: 4000/2000MS (Java/Others)     Memory Limit: 65535/65535KB (Java/Others)
Submit 
Status

Have you ever heard a star charity show called Charitable Exchange? In this show, a famous star starts with a small item which values 11 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 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 NN, MM (1≤N≤1051≤N≤105, 1≤M≤1091≤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 ViVi, RiRi, TiTi (1≤Ri≤Vi≤1091≤Ri≤Vi≤109, 1≤Ti≤1091≤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 and output

Sample Input Sample Output
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

优先队列 贪心+bfs

#include <iostream>

#include <string.h>
#include <stdlib.h>
#include <algorithm>
#include <math.h>
#include <stdio.h>
#include <queue> using namespace std;
#define MAX 100000
struct Node
{
long long int v;
long long int r;
long long int t;
}a[MAX+5];
struct node
{
long long int value;
long long int time;
bool friend operator <(node a,node b)
{
return a.time>b.time;
} };
priority_queue<node> q;
int n,m;
int right1;
int cmp(Node a,Node b)
{
return a.r<b.r;
}
long long int bfs()
{
node term1;
term1.value=1;term1.time=0;
q.push(term1);
int left=1,i;
while(!q.empty())
{
node term2=q.top();
q.pop();
if(term2.value>=m)
{
return term2.time;
}
for(i=left;i<=right1;i++)
{
if(term2.value>=a[i].r&&term2.value<a[i].v)
{
node temp;
temp.value=a[i].v;
temp.time=term2.time+a[i].t;
q.push(temp);
}
if(term2.value<a[i].r)
break; }
left=i; }
return -1;
}
int main()
{
int t;
int cas=0;
scanf("%d",&t);
long long int vv,rr,tt;
while(t--)
{
scanf("%d%d",&n,&m);
right1=0;
for(int i=1;i<=n;i++)
{
scanf("%lld%lld%lld",&vv,&rr,&tt);
if(vv==rr)
continue;
a[++right1].v=vv;a[right1].r=rr;a[right1].t=tt;
}
while(!q.empty())
q.pop();
sort(a+1,a+1+right1,cmp);
printf("Case #%d: %lld\n",++cas,bfs());
}
return 0;
}

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

最新文章

  1. ActiveMQ中的Destination高级特性(一)
  2. es6 Class的继承extends &amp; super
  3. R语言数据的输入
  4. Information retrieval信息检索
  5. Linux下好玩的命令
  6. LingPipe-TextClassification(文本分类)
  7. 【数学/扩展欧几里得/线性求逆元】[Sdoi2008]沙拉公主的困惑
  8. FastScroll(1)ListView打开FastScroll及自定义它的样式
  9. Gitolite轻松部署/管理git server
  10. 微信内移动前端开发抓包调试工具fiddler使用教程
  11. Java I/O之NIO Socket
  12. #pta循环作业
  13. H5 仿ios select滚动选择器。框架。
  14. 使用catboost解决ML中高维度不平衡数据集挑战的解决方案
  15. spring boot 配置注入
  16. JavaSE回顾及巩固的自学之路(四)——————方法和数组,面向对象
  17. Jquert data方法获取不到数据,显示为undefined。
  18. Audition CS6在Win 7中“音频输入的采样频率与输出设备不匹配”问题的解决 - imsoft.cnblogs
  19. cobbler 自定义安装系统
  20. 《Head First 设计模式》读书笔记

热门文章

  1. HDU1157 Who&amp;#39;s in the Middle
  2. Django——基于类的视图源码分析 三
  3. ASP.NET中session和ViewState区别
  4. 记一次R的可视化使用-生成城市各个景点的多边形图
  5. Excle数组用法
  6. SGDMA
  7. Angularjs学习笔记8_directive2
  8. mysql bin-log三种模式
  9. 深度解析丨秒懂nova3手机上超酷炫的AR应用及开发
  10. 15:开发Rsync服务启动脚本案例