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. Selenium webdriver Java 封装与重用

    目的 1. 简化调用 WebDriver对页面的操作,需要找到一个WebElement,然后再对其进行操作,比较繁琐: WebElement element =driver.findElement(B ...

  2. Linux 忘记密码怎么办

    Linux 忘记密码解决方法 很多朋友经常会忘记Linux系统的root密码,linux系统忘记root密码的情况该怎么办呢?重新安装系统吗?当然不用!进入单用户模式更改一下root密码即可. 步骤如 ...

  3. Java线程总结(转)

    作者的blog:(http://blog.matrix.org.cn/page/Kaizen) 首先要理解线程首先须要了解一些主要的东西,我们如今所使用的大多数操作系统都属于多任务,分时操作系统.正是 ...

  4. Java 创建用户异常类、将异常一直向上抛、 throw和throws的区别

    如果java提供的系统异常类型不能满足程序设计的需求,那么可以设计自己的异常类型. 从java异常类的结构层次可以看出,java类型的公共父类为Throwable.在程序运行中可能出现俩种问题:一种是 ...

  5. oracle获取时间毫秒数

    select (sysdate-to_date('1970-01-01','yyyy-mm-dd')-8/24)*24*60*60*1000-1* 60 * 60 * 1000  millisecon ...

  6. window.onload 、body.onload 以及 jQuery 等dom加载完成后执行脚本的区别

    1.关于window.onload 和 body.onload 的区别 当我们将onload 事件写在body元素上时,真正执行的其实是window对象的onload事件.因素HTMl页面中没有win ...

  7. Log4net 根据日志类型输出日志

    第一步:引入Log4net.dll 文件的引用 第二步:添加LogHelper类,代码如下 using System; using System.Collections.Generic; using ...

  8. POJ 3667 Hotel(线段树)

    POJ 3667 Hotel 题目链接 题意:有n个房间,如今有两个操作 1.找到连续长度a的空房间.入住,要尽量靠左边,假设有输出最左边的房间标号,假设没有输出0 2.清空[a, a + b - 1 ...

  9. MongoDB安装、CURD增改查删操作、应用场景

    NoSQL(NoSQL = Not Only SQL ),意即"不仅仅是SQL".非关系型的数据存储 MongoDB 是一个基于分布式文件存储的数据库.由 C++ 语言编写.旨在为 ...

  10. Atitit.python web环境的配置 attilax 总结

    Atitit.python web环境的配置 attilax 总结 1. 下载modpython/1 1.1. 安装python2.5.11 1.2. 安装modpython1 2. 设置py文件的u ...