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. Java 1.7 ReentrantReadWriteLock源代码解析

    因为本人水平与表达能力有限,有错误的地方欢迎交流与指正. 1 简单介绍 可重入读写锁时基于AQS实现的,典型的用法如JDK1.7中的演示样例: class RWDictionary { private ...

  2. Web前端开发实战1:二级下拉式菜单之CSS实现

    二级下拉式菜单在各大学校站点.电商类站点.新闻类站点等大型?站点非经常见,那么它的实现原理是什么呢? 学习了Web前端开发的知识后,我们是能够实现这种功能的.复杂的都是从基础效果上加入做出来的.原理和 ...

  3. JAVA 模块

    commons-lang3 maven repository, 项目主页 fastjson maven repository, 项目主页 fastjson 是阿里巴巴开源的序列化和反序列化 JSON ...

  4. jquery遍历DOM方法总结

    1.jQuery 遍历 - 祖先 向上遍历 DOM 树 这些 jQuery 方法很有用,它们用于向上遍历 DOM 树: parent() parents() parentsUntil() jQuery ...

  5. MySQL学习总结(五)表数据查询

    查询数据记录,是指从数据库对象表中获取所要查询的数据记录,该操作可以说是数据最基本的操作之一,也是使用频率最高.最重要的数据操作. 1.单表数据记录查询 1.1.简单数据查询 SELECT field ...

  6. jQuery校验 表单验证

    官网地址:http://bassistance.de/jquery-plugins/jquery-plugin-validation jQuery plugin: Validation 使用说明 转载 ...

  7. mybatis学习笔记(14)-查询缓存之中的一个级缓存

    mybatis学习笔记(14)-查询缓存之中的一个级缓存 标签: mybatis mybatis学习笔记14-查询缓存之中的一个级缓存 查询缓存 一级缓存 一级缓存工作原理 一级缓存測试 一级缓存应用 ...

  8. python 属性 property、getattr()、setattr()详解

    直奔主题 使用中文注释需要使用 #-*-coding:utf-8-*- property property在python中有2中使用property方法:1.@property @属性名称.sette ...

  9. vue的计算属性

    在模板中写入过多的逻辑使模板过重且难以维护.因此有了计算属性(computed)的产生. 你可以像绑定普通属性一样在模板中绑定计算属性,vue知道计算属性中的函数依赖data中的数据.所以当data中 ...

  10. JNDI提供了一种统一的方式,可以用在网络上查找和访问服务

    JNDI提供了一种统一的方式,可以用在网络上查找和访问服务.通过指定一个资源名称,该名称对应于数据库或命名服务中的一个记录,同时返回数据库连接建立所必须的信息. JNDI主要有两部分组成:应用程序编程 ...