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
Case #1: -1
Case #2: 4
Case #3: 10

Source

Sichuan State Programming Contest 2011
 
题意:给你n个物品交换,每个交换用r,v,t描述,代表需要用r元的东西花费t时间交换得v元的东西。
一开始只有1元的东西,让你求出交换到价值至少为m的最少时间代价。
 
题解:相当于每个交换是一条边,时间为边权,求走到价值大于等于m的点的最短路径。bfs的时候,用优先队列来储存状态,
每次取出花费总时间最小的状态。将边按照r来排序,遍历更新得到新的边加入到队列,注意剪枝,当前拥有r无法做任何交换时 break;
 
爆内存点  i=l  不需要每次遍历都从i=1开始 因为已经进行过排序处理,已经遍历过的 为了确保时间代价最小 一定不满足之后取出的状态
T点  r无法做任何交换时 break;
注意开long long
 
 
 /******************************
code by drizzle
blog: www.cnblogs.com/hsd-/
^ ^ ^ ^
O O
******************************/
//#include<bits/stdc++.h>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<map>
#include<algorithm>
#include<queue>
#include<cmath>
#define ll long long
#define PI acos(-1.0)
#define mod 1000000007
using namespace std;
struct node
{
int to;
ll we;
int pre;
friend bool operator < (node a, node b)
{
return a.we > b.we;
}
}N[];
priority_queue<node> pq;
int t;
int n,m;
bool cmp(struct node aa,struct node bb)
{
return aa.pre<bb.pre;
}
ll bfs()
{
struct node exm,now;
while(!pq.empty()) pq.pop();
exm.pre=;
exm.to=;
exm.we=;
pq.push(exm);
int l=;
int i;
while(!pq.empty())
{
now=pq.top();
pq.pop();
if(now.to>=m)
{
return now.we;
}
for(i=l; i<=n; i++)
{
if(now.to>=N[i].pre&&now.to<N[i].to)//遍历可以用的边
{
exm.pre=now.to;
exm.to=N[i].to;
exm.we=now.we+N[i].we;
pq.push(exm);
l=i;//爆内存点
}
if(now.to<N[i].pre)//T点
break;
}
}
return -;
}
int main()
{
scanf("%d",&t);
{
for(int i=; i<=t; i++)
{
scanf("%d %d",&n,&m);
for(int j=; j<=n; j++)
scanf("%d %d %lld",&N[j].to,&N[j].pre,&N[j].we);
sort(N+,N++n,cmp);
ll ans=bfs();
printf("Case #%d: %lld\n",i,ans);
}
}
return ;
}
 

cdoj 482 优先队列+bfs的更多相关文章

  1. 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 ...

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

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

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

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

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

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

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

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

  6. CDOJ 482 Charitable Exchange bfs

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

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

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

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

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

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

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

随机推荐

  1. Fence9

    题目大意: 求点(0,0),(n,m),(p,0)三点构成的三角形内部(不包括边界)整点的个数. 解题过程:1.直接枚举纵坐标,然后算出两条直线上纵坐标为y的点的横坐标,然后他们中间的点就是符合要求的 ...

  2. Problem C 链表

    Description 某部队进行新兵队列训练,将新兵从一开始按顺序依次编号,并排成一行横队,训练的规则如下:从头开始一至二报数,凡报到二的出列,剩下的向小序号方向靠拢,再从头开始进行一至三报数,凡报 ...

  3. android listview getviewtypecount和getItemViewType

    package newdemo.jeno.listviewdemo; import android.app.Activity;import android.os.Bundle;import andro ...

  4. HDFS权限问题

    HDFS权限问题 Win下Eclipse提交hadoop程序出错:org.apache.hadoop.security.AccessControlException: Permission denie ...

  5. C#移动无标题栏窗体的四种代码

    第一种采用,需注意窗体上的控件是否把窗体覆盖了...MouseDown.MouseMove.MouseUp事件应该是鼠标所处位置最顶层的控件的事件在窗体的类中声明两个变量private Point m ...

  6. Iterator之java.util.ConcurrentModificationException

    在运行以下代码时,会报java.util.ConcurrentModificationException异常, public class Demo { public static void main( ...

  7. 玩转无线电 -- 温哥华天车 RFID 票务系统

    0x00 前言 如今物联网 RFID系统已经完全融入了我们的生活当中. 从楼宇门禁到 Apple Pay. 可以说其身影是无处不在.很多网友也分享了自己对RFID系统的安全测试心得.不过大多还是基于门 ...

  8. How many instances created in the WebContainer

    When the Servlet container starts, it: reads web.xml; finds the declared Servlets in the classpath; ...

  9. LeetCode---- 二叉树中,找出和为某值的所有路径

    Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given su ...

  10. Yii2 GridView自定义链接之重写 ActionColumn

    最近刚开始用yii2,真是超棒的,但是也有许多不足的地方,今天要说的就是GridView链接问题.   <?= GridView::widget([ 'dataProvider' => $ ...