UESTC482-Charitable Exchange-bfs优先队列

 

 1 #include <cstring>
 2 #include <algorithm>
 3 #include <iostream>
 4 #include <queue>
 5 
 6 using namespace std;
 7 
 8 typedef long long LL;
 9 const int maxn = 1e5+10;
10 int T,N,cnt;
11 LL M;
12 
13 struct node{
14     LL money,time;
15     node(){}
16     node(LL a,LL b){money=a;time=b;}
17     bool operator < (const node &b) const
18         {return time > b.time;}
19 };
20 
21 struct item
22 {
23     LL V,R,time;
24     item(){}
25     item(LL a,LL b,LL c){V = a;R = b;time = c;}
26     bool operator < (const item &b) const
27         {return R < b.R;}
28 }items[maxn];
29 
30 LL bfs()
31 {
32     priority_queue<node> pq;
33     node st(1,0),cur;
34     pq.push(st);
35     int i,x=1;
36     while(!pq.empty())
37     {
38         cur = pq.top();pq.pop();
39         if(cur.money >= M) return cur.time;
40         for(i=x;i<=cnt;i++)
41         {
42             if(cur.money < items[i].R) break;
43             if(cur.money>=items[i].R&&cur.money<items[i].V)
44             {
45                 pq.push(node(items[i].V,cur.time+items[i].time));
46             }
47         }
48         x = i;
49     }
50     return -1;
51 }
52 
53 int main()
54 {
55     cin >> T;
56     for(int cas=1;cas<=T;cas++)
57     {
58         cin >> N >> M;
59         LL v,r,t;
60         cnt=1;
61         for(int i=0;i<N;i++)
62         {
63             cin >> v >> r >> t;
64             if(v==r) continue;
65             items[cnt++] = item(v,r,t);
66         }
67         sort(items+1,items+cnt+1);
68         cout << "Case #"<<cas<<": "<<bfs()<<endl;
69     }
70 }

 

posted @ 2016-02-21 17:08  Helica  阅读(252)  评论(0编辑  收藏  举报