cdoj 482 优先队列+bfs

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

 1 /******************************
 2 code by drizzle
 3 blog: www.cnblogs.com/hsd-/
 4 ^ ^    ^ ^
 5  O      O
 6 ******************************/
 7 //#include<bits/stdc++.h>
 8 #include<iostream>
 9 #include<cstring>
10 #include<cstdio>
11 #include<map>
12 #include<algorithm>
13 #include<queue>
14 #include<cmath>
15 #define ll long long
16 #define PI acos(-1.0)
17 #define mod 1000000007
18 using namespace std;
19 struct node
20 {
21     int to;
22     ll we;
23     int pre;
24     friend bool operator < (node a, node b)
25     {
26         return  a.we > b.we;
27     }
28 }N[100005];
29 priority_queue<node> pq;
30 int t;
31 int n,m;
32 bool cmp(struct node aa,struct  node bb)
33 {
34     return aa.pre<bb.pre;
35 }
36 ll bfs()
37 {
38     struct node exm,now;
39     while(!pq.empty()) pq.pop();
40     exm.pre=0;
41     exm.to=1;
42     exm.we=0;
43     pq.push(exm);
44     int l=1;
45     int i;
46     while(!pq.empty())
47     {
48         now=pq.top();
49         pq.pop();
50         if(now.to>=m)
51         {
52             return now.we;
53         }
54         for(i=l; i<=n; i++)
55         {
56             if(now.to>=N[i].pre&&now.to<N[i].to)//遍历可以用的边
57             {
58                 exm.pre=now.to;
59                 exm.to=N[i].to;
60                 exm.we=now.we+N[i].we;
61                 pq.push(exm);
62                 l=i;//爆内存点
63             }
64             if(now.to<N[i].pre)//T点
65                 break;
66         }
67     }
68     return -1;
69 }
70 int main()
71 {
72     scanf("%d",&t);
73     {
74         for(int i=1; i<=t; i++)
75         {
76             scanf("%d %d",&n,&m);
77             for(int j=1; j<=n; j++)
78                 scanf("%d %d %lld",&N[j].to,&N[j].pre,&N[j].we);
79             sort(N+1,N+1+n,cmp);
80             ll ans=bfs();
81             printf("Case #%d: %lld\n",i,ans);
82         }
83     }
84     return 0;
85 }
时间: 2024-09-29 08:05:18

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

HDU 1242 Rescue(优先队列+bfs)

题目地址:HDU 1242 这个题相比于普通的bfs有个特殊的地方,经过士兵时会额外消耗时间,也就是说此时最先搜到的时候不一定是用时最短的了.需要全部搜一遍才可以.这时候优先队列的好处就显现出来了.利用优先队列,可以让队列中的元素按时间排序,让先出来的总是时间短的,这样的话,最先搜到的一定是时间短的,就不用全部搜一遍了.PS:我是为了学优先队列做的这题..不是为了这题而现学的优先队列.. 代码如下: #include <iostream> #include <stdio.h> #i

Ignatius and the Princess I (hdu 1026 优先队列+bfs+输出路径)

Ignatius and the Princess I Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 14624    Accepted Submission(s): 4634 Special Judge Problem Description The Princess has been abducted by the BEelzeb

CDOJ 482 Charitable Exchange bfs

Charitable Exchange Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.uestc.edu.cn/#/problem/show/482 Description Have you ever heard a star charity show called Charitable Exchange? In this show, a famous star starts with a small item which val

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

给你n个物品交换,每个交换用r,v,t描述,代表需要用r元的东西花费t时间交换得v元的东西.一开始只有1元的东西,让你求出交换到价值至少为m的最少时间代价.相当于每个交换是一条边,时间为边权,求走到价值大于等于m的点的最短路径.bfs的时候,用优先队列来储存状态,每次取出花费总时间最小的状态. #include<cstdio> #include<queue> #include<algorithm> #include<vector> #define ll lo

POJ 2312 Battle City(优先队列+BFS)

题目链接:http://poj.org/problem?id=2312 Battle City Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7085   Accepted: 2390 Description Many of us had played the game "Battle city" in our childhood, and some people (like me) even often pl

poj 2251 Dungeon Master(优先队列bfs)

Dungeon Master Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 20867   Accepted: 8090 Description You are trapped in a 3D dungeon and need to find the quickest way out! The dungeon is composed of unit cubes which may or may not be filled

hdu 5040 优先队列BFS+剪枝

(北京网络赛09题)题意:给一矩阵(图),里面有起点,终点,还有探照灯(每个有初始朝向,每秒顺时针转90度),前面有灯或者自己被灯照着,移动就要花3秒,求起点到终点最短时间. 用一个数组三维数组记录一下,用来当前位置当前时间%4有没有灯,然后优先队列(时间短的在前面),搜索即可.考虑到可以来回走或者原地等,不能简单判重剪枝:每个地方最多是4种状态!就是4秒之后就全图状态回到一样!所以若当前状态(时间%4)下来过就不用来了. #include<iostream> #include<vect

hdu 1242 rescue (优先队列 bfs)

题意: 公主被关在 a位置 她的朋友在r位置 路上x位置有恶魔 遇上恶魔花费2 时间 否在时间花费 1 时间 问 最短多少时间 找到公主 思路: bfs+ 优先队列(时间短的先出列) #include<cstdio> #include<cstring> #include<cmath> #include<queue> #include<iostream> #include<algorithm> using namespace std;

hdu 1026 Ignatius and the Princess I(优先队列+bfs+记录路径)

以前写的题了,现在想整理一下,就挂出来了. 题意比较明确,给一张n*m的地图,从左上角(0, 0)走到右下角(n-1, m-1). 'X'为墙,'.'为路,数字为怪物.墙不能走,路花1s经过,怪物需要花费1s+数字大小的时间. 比较麻烦的是需要记录路径.还要记录是在走路还是在打怪. 因为求最短路,所以可以使用bfs. 因为进过每一个点花费时间不同,所以可以使用优先队列. 因为需要记录路径,所以需要开一个数组,来记录经过节点的父节点.当然,记录方法不止一种. 上代码—— 1 #include <c