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

UESTC 482 Charitable Exchange(优先队列+bfs)的更多相关文章

  1. CDOJ 482 Charitable Exchange bfs

    Charitable Exchange Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.uestc.edu.cn/#/problem/s ...

  2. cdoj 482 优先队列+bfs

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

  3. hdu 1026 Ignatius and the Princess I【优先队列+BFS】

    链接: http://acm.hdu.edu.cn/showproblem.php?pid=1026 http://acm.hust.edu.cn/vjudge/contest/view.action ...

  4. ZOJ 649 Rescue(优先队列+bfs)

    Rescue Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Sub ...

  5. 【POJ3635】Full Tank 优先队列BFS

    普通BFS:每个状态只访问一次,第一次入队时即为该状态对应的最优解. 优先队列BFS:每个状态可能被更新多次,入队多次,但是只会扩展一次,每次出队时即为改状态对应的最优解. 且对于优先队列BFS来说, ...

  6. Codeforces 677D - Vanya and Treasure - [DP+优先队列BFS]

    题目链接:http://codeforces.com/problemset/problem/677/D 题意: 有 $n \times m$ 的网格,每个网格上有一个棋子,棋子种类为 $t[i][j] ...

  7. POJ 2449 - Remmarguts' Date - [第k短路模板题][优先队列BFS]

    题目链接:http://poj.org/problem?id=2449 Time Limit: 4000MS Memory Limit: 65536K Description "Good m ...

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

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

  9. hdu - 1242 Rescue && hdu - 2425 Hiking Trip (优先队列+bfs)

    http://acm.hdu.edu.cn/showproblem.php?pid=1242 感觉题目没有表述清楚,angel的朋友应该不一定只有一个,那么正解就是a去搜索r,再用普通的bfs就能过了 ...

随机推荐

  1. Struts2--百度百科

    Struts 2是Struts的下一代产品,是在 struts 1和WebWork的技术基础上进行了合并的全新的Struts 2框架.其全新的Struts 2的体系结构与Struts 1的体系结构差别 ...

  2. redux-actions

    其作用都是用来简化action.reducer. 1.安装 npm install --save redux-actions // 或 yarn add redux-actions 2.使用 crea ...

  3. 19、Cocos2dx 3.0游戏开发找小三之Action:流动的水没有形状,漂流的风找不到踪迹、、、

    重开发人员的劳动成果.转载的时候请务必注明出处:http://blog.csdn.net/haomengzhu/article/details/30478985 流动的水没有形状.漂流的风找不到踪迹. ...

  4. JavaScript | 基础(变量/引用/转换/函数)

    ———————————————————————————————————————————— 变量 全局变量:在函数体外声明,全局可以使用 局部变量:通过关键字var来声明 变量类型 <script ...

  5. H5页面在IOS下不会自动播放音乐的坑

    document.addEventListener(‘DOMContentLoaded‘, function () { function audioAutoPlay() { var audio = d ...

  6. Python绘制分段函数

    1.绘制分段函数:y=4sin(4πt)-sgn(t-0.3)-sgn(0.72-t) import numpy as npimport matplotlib.pyplot as plt#绘制分段函数 ...

  7. LA 5009 (HDU 3714) Error Curves (三分)

    Error Curves Time Limit:3000MS    Memory Limit:0KB    64bit IO Format:%lld & %llu SubmitStatusPr ...

  8. MongoDB学习——持续更新

    參考MongoDB权威指南,学习阶段.大家多多交流问题.持续更新本文 MongoDB的长处 MongoDB具有丰富的数据模型,是面向文档的数据库. easy扩展.能够在多台server之间切割数据. ...

  9. socket编程之实现简单的ssh

    服务器代码: #-*- coding:utf-8 -*- #edited by python3.6 # import socket,os ''' 创建socket对象 ''' server = soc ...

  10. Vue 组件 非父子组件通信

    有时候两个组件也需要通信(非父子关系),在简单的场景下,可以使用一个空的vue实例作为中央事件总线: var bus = new Vue(); //触发组件a中的事件 bus.$emit('id-se ...