设为首页 收藏本站
查看: 769|回复: 0

[经验分享] cdoj 482 优先队列+bfs

[复制链接]

尚未签到

发表于 2017-7-4 08:18:18 | 显示全部楼层 |阅读模式
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 InputSample 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.pre&&now.to<N.to)//遍历可以用的边
57             {
58                 exm.pre=now.to;
59                 exm.to=N.to;
60                 exm.we=now.we+N.we;
61                 pq.push(exm);
62                 l=i;//爆内存点
63             }
64             if(now.to<N.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 }

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-390701-1-1.html 上篇帖子: Linux上安装使用SSH 下篇帖子: rabbitmq 简单梳理
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表