zoukankan      html  css  js  c++  java
  • 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 }
  • 相关阅读:
    python装饰器的wraps作用
    lambda函数和map函数
    python直接赋值、切片、浅拷贝和深拷贝
    ubuntu shell脚本出错 dash
    关于方法论和相关书籍
    如何安全的大数据量表在线进行DML操作
    mysql group by 查询非聚集列
    MongoTemplate进行增删改查
    Mockito 的用法
    一个人开始优秀的3种迹象
  • 原文地址:https://www.cnblogs.com/helica/p/5205218.html
Copyright © 2011-2022 走看看