• 优先队列题目整理


      1 /*
      2  ***********Good LUCK**********
      3  * Author:  yeahpeng
      4  * Created Time:  2015/6/5 12:32:45
      5  * File Name: uestc_482_CExchange.cpp
      6  * Charitable Exchange
      7 Time Limit: 4000/2000MS (Java/Others)     Memory Limit: 65535/65535KB (Java/Others)
      8 
      9 Submit  Status
     10 Have you ever heard a star charity show called Charitable Exchange? In this show, a famous star starts with a small item which values 1 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.
     11 
     12 In each exchange, one can exchange for an item of Vi yuan if he (she) has an item values more than or equal to Ri yuan, with a time cost of Ti minutes.
     13 
     14 Now, you task is help the star to exchange for an item which values more than or equal to M yuan with the minimum time.
     15 
     16 Input
     17 The first line of the input is T (no more than 20), which stands for the number of test cases you need to solve.
     18 
     19 For each case, two integers N, M (1¡ÜN¡Ü105, 1¡ÜM¡Ü109) in the first line indicates the number of available exchanges and the expected value of final item. Then N lines follow, each line describes an exchange with 3 integers Vi, Ri, Ti (1¡ÜRi¡ÜVi¡Ü109, 1¡ÜTi¡Ü109).
     20 
     21 Output
     22 For every test case, you should output Case #k: first, where k indicates the case number and counts from 1. Then output the minimum time. Output ?1 if no solution can be found.
     23 
     24 Sample input and output
     25 Sample Input    Sample Output
     26 3
     27 3 10
     28 5 1 3
     29 8 2 5
     30 10 9 2
     31 4 5
     32 2 1 1
     33 3 2 1
     34 4 3 1
     35 8 4 1
     36 5 9
     37 5 1 1
     38 10 4 10
     39 8 1 10
     40 11 6 1
     41 7 3 8
     42 Case #1: -1
     43 Case #2: 4
     44 Case #3: 10
     45  */
     46 #include <stdio.h>
     47 #include <iostream>
     48 #include <algorithm>
     49 #include <sstream>
     50 #include <stdlib.h>
     51 #include <string.h>
     52 #include <limits.h>
     53 #include <vector>
     54 #include <string>
     55 #include <time.h>
     56 #include <math.h>
     57 #include <queue>
     58 #include <stack>
     59 #include <set>
     60 #include <map>
     61 #define INF 0x3f3f3f3f
     62 #define Zero(x)  memset((x),0, sizeof(x))
     63 #define Neg(x) memset((x), -1, sizeof(x))
     64 #define dg(x) cout << #x << " = " << x << endl
     65 #define pk(x)   push_back(x)
     66 #define pok()   pop_back()
     67 #define eps 1e-8
     68 #define pii pair<int, int>
     69 #define pi acos(-1.0)
     70 using namespace std;
     71 typedef long long ll;
     72 bool debug = true;
     73 int OK = 1;
     74 const int maxn = 100050;
     75 int n, m, c;
     76 struct node{
     77     int v, u, val;
     78     node(int vv = 0, int uu = 0, int vval = 0){
     79         v = vv;
     80         u = uu;
     81         val = vval;
     82     }
     83     bool operator < (const node& a)const {
     84         return u < a.u;
     85     }
     86 }edge[maxn];
     87 ll ans;
     88 struct noo{
     89     int u;
     90     ll cost;
     91     noo(int uu = 0,  ll cc = 0){
     92         u = uu;
     93         cost = cc;
     94     }
     95     bool operator < (const noo& a)const {
     96         return cost > a.cost;
     97     }
     98 };
     99 bool cal(){
    100     priority_queue<noo> pq;
    101     pq.push(noo(1, 0));
    102     noo top;
    103     int left = 0, i;
    104     while(!pq.empty()){
    105         top = pq.top();
    106         pq.pop();
    107         if(top.u >= m) {
    108             ans = top.cost;
    109             return true;
    110         }
    111         for(i = left; i < c; ++i){
    112             if(top.u < edge[i].u) break;
    113             if(top.u >= edge[i].u && top.u <edge[i].v){
    114                 pq.push(noo(edge[i].v, top.cost + edge[i].val));
    115             }
    116         }
    117         left = i;
    118     }
    119     return false;
    120 }
    121 
    122 int main(){
    123     //freopen("data.in","r",stdin);
    124     //freopen("data.out","w",stdout);
    125     //cin.sync_with_stdio(false);
    126     int T;
    127     cin >> T;
    128     int u, v, val;
    129     for(int cs = 1; cs <= T; ++cs){
    130         scanf("%d%d", &n, &m);
    131         c = 0;
    132         for(int i = 0; i < n; ++i){
    133             scanf("%d%d%d", &v, &u, &val);
    134             if(u >= v) continue;
    135             edge[c++] = node(v, u, val);
    136         }
    137         sort(edge, edge + c);
    138         printf("Case #%d: ", cs);
    139         if(cal()){
    140             cout << ans << endl;
    141         }else cout << -1 << endl;
    142     }
    143     return 0;
    144 }
    View Code
      1 /*
      2  ***********Good LUCK**********
      3  * Author:  yeahpeng
      4  * Created Time:  2015/6/4 22:03:06
      5  * File Name: poj1724优先.cpp
      6  * Description
      7 
      8 N cities named with numbers 1 ... N are connected with one-way roads. Each road has two parameters associated with it : the road length and the toll that needs to be paid for the road (expressed in the number of coins). 
      9 Bob and Alice used to live in the city 1. After noticing that Alice was cheating in the card game they liked to play, Bob broke up with her and decided to move away - to the city N. He wants to get there as quickly as possible, but he is short on cash. 
     10 
     11 We want to help Bob to find the shortest path from the city 1 to the city N that he can afford with the amount of money he has. 
     12 Input
     13 
     14 The first line of the input contains the integer K, 0 <= K <= 10000, maximum number of coins that Bob can spend on his way. 
     15 The second line contains the integer N, 2 <= N <= 100, the total number of cities. 
     16 
     17 The third line contains the integer R, 1 <= R <= 10000, the total number of roads. 
     18 
     19 Each of the following R lines describes one road by specifying integers S, D, L and T separated by single blank characters : 
     20 S is the source city, 1 <= S <= N 
     21 D is the destination city, 1 <= D <= N 
     22 L is the road length, 1 <= L <= 100 
     23 T is the toll (expressed in the number of coins), 0 <= T <=100
     24 
     25 Notice that different roads may have the same source and destination cities.
     26 Output
     27 
     28 The first and the only line of the output should contain the total length of the shortest path from the city 1 to the city N whose total toll is less than or equal K coins. 
     29 If such path does not exist, only number -1 should be written to the output. 
     30 Sample Input
     31 
     32 5
     33 6
     34 7
     35 1 2 2 3
     36 2 4 3 3
     37 3 4 2 4
     38 1 3 4 1
     39 4 6 2 1
     40 3 5 2 0
     41 5 4 3 2
     42 Sample Output
     43 
     44 11
     45  */
     46 #include <stdio.h>
     47 #include <iostream>
     48 #include <algorithm>
     49 #include <sstream>
     50 #include <stdlib.h>
     51 #include <string.h>
     52 #include <limits.h>
     53 #include <vector>
     54 #include <string>
     55 #include <time.h>
     56 #include <math.h>
     57 #include <queue>
     58 #include <stack>
     59 #include <set>
     60 #include <map>
     61 #define INF 0x3f3f3f3f
     62 #define Zero(x)  memset((x),0, sizeof(x))
     63 #define Neg(x) memset((x), -1, sizeof(x))
     64 #define dg(x) cout << #x << " = " << x << endl
     65 #define pk(x)   push_back(x)
     66 #define pok()   pop_back()
     67 #define eps 1e-8
     68 #define pii pair<int, int>
     69 #define pi acos(-1.0)
     70 using namespace std;
     71 typedef long long ll;
     72 bool debug = true;
     73 int OK = 1;
     74 const int maxn = 110;
     75 const int maxm = 10010;
     76 int k, n, r;
     77 struct node{
     78     int l, t, v;
     79     node(int ll = 0, int tt = 0, int vv = 0){
     80         l = ll;
     81         t = tt;
     82         v = vv;
     83     }
     84     bool operator<(const node& a) const {
     85         return l > a.l;
     86     }
     87 };
     88 vector<node> gra[maxn];
     89 int ans;
     90 bool cal(){
     91     priority_queue<node> pq;
     92     pq.push(node(0, 0, 1));
     93     node top,t;
     94     int sz;
     95     while(!pq.empty()){
     96         top = pq.top();
     97         pq.pop();
     98         if(top.v == n) {
     99             ans = top.l;
    100             return true;
    101         }
    102         sz = gra[top.v].size();
    103         for(int i = 0; i < sz; ++i){
    104             t = gra[top.v][i];
    105             if(t.t + top.t <= k) pq.push(node(t.l + top.l, t.t + top.t, t.v));
    106         }
    107 
    108     }
    109     return false;
    110 }
    111 int main(){
    112     //freopen("data.in","r",stdin);
    113     //freopen("data.out","w",stdout);
    114     //cin.sync_with_stdio(false);
    115     int u, v, l, t;
    116     while(scanf("%d", &k) != EOF){
    117         scanf("%d%d", &n, &r);
    118         for(int i = 0; i <= n; ++i) gra[i].clear();
    119         for(int i = 0; i < r; ++i){
    120             scanf("%d%d%d%d", &u, &v, &l, &t);
    121             gra[u].pk(node(l, t, v));
    122         }
    123         if(cal()) cout << ans << endl;
    124         else cout << -1 << endl;
    125     }
    126     return 0; 
    127 }
    View Code
      1 /*
      2  ***********Good LUCK**********
      3  * Author:  yeahpeng
      4  * Created Time:  2015/6/3 23:37:49
      5  * File Name: hdu4544_优先.cpp
      6  * 题意:Problem Description
      7   湫湫减肥
      8   越减越肥!
      9   
     10   最近,减肥失败的湫湫为发泄心中郁闷,在玩一个消灭免子的游戏。
     11   游戏规则很简单,用箭杀死免子即可。
     12   箭是一种消耗品,已知有M种不同类型的箭可以选择,并且每种箭都会对兔子造成伤害,对应的伤害值分别为Di(1 <= i <= M),每种箭需要一定的QQ币购买。
     13   假设每种箭只能使用一次,每只免子也只能被射一次,请计算要消灭地图上的所有兔子最少需要的QQ币。
     14  
     15 
     16 Input
     17 输入数据有多组,每组数据有四行;
     18 第一行有两个整数N,M(1 <= N, M <= 100000),分别表示兔子的个数和箭的种类;
     19 第二行有N个正整数,分别表示兔子的血量Bi(1 <= i <= N);
     20 第三行有M个正整数,表示每把箭所能造成的伤害值Di(1 <= i <= M);
     21 第四行有M个正整数,表示每把箭需要花费的QQ币Pi(1 <= i <= M)。
     22 
     23 特别说明:
     24 1、当箭的伤害值大于等于兔子的血量时,就能将兔子杀死;
     25 2、血量Bi,箭的伤害值Di,箭的价格Pi,均小于等于100000。
     26  
     27 
     28 Output
     29 如果不能杀死所有兔子,请输出”No”,否则,请输出最少的QQ币数,每组输出一行。
     30  
     31 
     32 Sample Input
     33 3 3
     34 1 2 3
     35 2 3 4
     36 1 2 3
     37 3 4
     38 1 2 3
     39 1 2 3 4
     40 1 2 3 1
     41  
     42 
     43 Sample Output
     44 6
     45 4
     46  */
     47 #include <stdio.h>
     48 #include <iostream>
     49 #include <algorithm>
     50 #include <sstream>
     51 #include <stdlib.h>
     52 #include <string.h>
     53 #include <limits.h>
     54 #include <vector>
     55 #include <string>
     56 #include <time.h>
     57 #include <math.h>
     58 #include <queue>
     59 #include <stack>
     60 #include <set>
     61 #include <map>
     62 #define INF 0x3f3f3f3f
     63 #define Zero(x)  memset((x),0, sizeof(x))
     64 #define Neg(x) memset((x), -1, sizeof(x))
     65 #define dg(x) cout << #x << " = " << x << endl
     66 #define pk(x)   push_back(x)
     67 #define pok()   pop_back()
     68 #define eps 1e-8
     69 #define pii pair<int, int>
     70 #define pi acos(-1.0)
     71 using namespace std;
     72 typedef long long ll;
     73 bool debug = false;
     74 int OK = 1;
     75 const int maxn = 100100;
     76 int n, m;
     77 ll ans;
     78 int b[maxn];
     79 struct node{
     80     int cost, d;
     81     node(int dd = 0, int cc = 0){
     82         d = dd;
     83         cost = cc;
     84     }
     85     bool operator < (const node& a)const {
     86         return d > a.d ;
     87     }
     88 }jj[maxn];
     89 
     90 priority_queue<int, vector<int>, greater<int> > qj;
     91 bool cal(){
     92     int st = 0;
     93     ans = 0;
     94     for(int i = 0; i < n; ++i){
     95         while(st < m && jj[st].d >= b[i]){
     96             qj.push(jj[st].cost);
     97             st++;
     98         }    
     99         if(qj.empty()) return false;
    100         if(debug){
    101             dg(qj.top());
    102         }
    103         ans += qj.top();
    104         qj.pop();
    105     }
    106     return true;
    107 }
    108 int main(){
    109     //freopen("data.in","r",stdin);
    110     //freopen("data.out","w",stdout);
    111     //cin.sync_with_stdio(false);
    112     while(cin >> n >>m){
    113         while(!qj.empty()) qj.pop();
    114         for(int i = 0; i < n; ++i){
    115             scanf("%d", b + i);
    116         }
    117         sort(b, b + n, greater<int>());
    118         for(int i = 0; i < m; ++i){
    119             scanf("%d", &jj[i].d);
    120         }
    121         for(int i = 0; i < m; ++i){
    122             scanf("%d", &jj[i].cost);
    123         }
    124         sort(jj, jj + m);
    125         if(cal()){
    126             cout << ans << endl;
    127         }else cout << "No" << endl;
    128     }
    129     return 0;
    130 }
    View Code
      1 /*
      2  ***********Good LUCK**********
      3  * Author:  yeahpeng
      4  * Created Time:  2015/6/3 23:09:57
      5  * File Name: hdu1242_优队+bfs.cpp
      6  */
      7 #include <stdio.h>
      8 #include <iostream>
      9 #include <algorithm>
     10 #include <sstream>
     11 #include <stdlib.h>
     12 #include <string.h>
     13 #include <limits.h>
     14 #include <vector>
     15 #include <string>
     16 #include <time.h>
     17 #include <math.h>
     18 #include <queue>
     19 #include <stack>
     20 #include <set>
     21 #include <map>
     22 #define INF 0x3f3f3f3f
     23 #define Zero(x)  memset((x),0, sizeof(x))
     24 #define Neg(x) memset((x), -1, sizeof(x))
     25 #define dg(x) cout << #x << " = " << x << endl
     26 #define pk(x)   push_back(x)
     27 #define pok()   pop_back()
     28 #define eps 1e-8
     29 #define pii pair<int, int>
     30 #define pi acos(-1.0)
     31 using namespace std;
     32 typedef long long ll;
     33 bool debug = true;
     34 int OK = 1;
     35 int n, m;
     36 const int maxn = 220;
     37 char mp[maxn][maxn];
     38 struct node{
     39     int x, y, sp;
     40     node(int xx = 0,int yy = 0, int ss = 0){
     41         x = xx;
     42         y = yy;
     43         sp = ss;
     44     }
     45     bool operator<(const node& b)const {
     46     return sp > b.sp;
     47 }
     48 }st, ed;
     49 int dir[4][2] = {
     50     0, 1, 0, -1, 1, 0, -1, 0
     51 };
     52 int ans;
     53 bool flag[maxn][maxn];
     54 bool cal(){
     55     Zero(flag);
     56     priority_queue<node> q;
     57     q.push(node(st.x, st.y, 0));
     58     node top;
     59     int x, y;
     60     flag[st.x][st.y] = true;
     61     while(!q.empty()){
     62         top = q.top();
     63         q.pop();
     64         if(top.x == ed.x && top.y == ed.y){
     65             ans = top.sp;
     66             return true;
     67         }
     68 
     69         for(int i = 0; i < 4; ++i){
     70             x = top.x + dir[i][0];
     71             y = top.y + dir[i][1];
     72             if(!flag[x][y] && x >= 1 && x <= n && y >= 1 && y <= m){
     73                 if(mp[x][y] != '#'){
     74                     flag[x][y] = true;
     75                     if(mp[x][y] == 'x') q.push(node(x, y,top.sp + 2));
     76                     else q.push(node(x, y, top.sp + 1));
     77                 }
     78             }
     79         }
     80     }
     81     return false;
     82 }
     83 int main(){
     84     //freopen("data.in","r",stdin);
     85     //freopen("data.out","w",stdout);
     86     //cin.sync_with_stdio(false);
     87     while(scanf("%d%d", &n, &m) != EOF){
     88         for(int i = 1; i <= n; ++i) scanf("%s", &mp[i][1]);
     89         for(int i = 1; i <= n; ++i){
     90             for(int j = 1; j <= m; ++j){
     91                 if(mp[i][j] == 'r'){
     92                     st.x = i;
     93                     st.y = j;
     94                 }else if(mp[i][j] == 'a'){
     95                     ed.x = i;
     96                     ed.y = j;
     97                 }
     98             }
     99         }
    100         if(cal()){
    101             cout << ans << endl;
    102         }else cout << "Poor ANGEL has to stay in the prison all his life." << endl;
    103     }
    104     return 0;
    105 }
    View Code

    zoj 3632 watermelon full of watere

     1 /***Good Luck***/
     2 #define _CRT_SECURE_NO_WARNINGS
     3 #include <iostream>
     4 #include <cstdio>
     5 #include <cstdlib>
     6 #include <cstring>
     7 #include <string>
     8 #include <algorithm>
     9 #include <stack>
    10 #include <map>
    11 #include <queue>
    12 #include <vector>
    13 #include <set>
    14 #include <functional>
    15 #include <cmath>
    16 #include <numeric>
    17 
    18 #define Zero(a) memset(a, 0, sizeof(a))
    19 #define Neg(a)  memset(a, -1, sizeof(a))
    20 #define All(a) a.begin(), a.end()
    21 #define PB push_back
    22 #define inf 0x3f3f3f3f
    23 #define inf2 0x7fffffffffffffff
    24 #define ll long long
    25 using namespace std;
    26 //#pragma comment(linker, "/STACK:102400000,102400000")
    27 void get_val(int &a) {
    28     int value = 0, s = 1;
    29     char c;
    30     while ((c = getchar()) == ' ' || c == '
    ');
    31     if (c == '-') s = -s; else value = c - 48;
    32     while ((c = getchar()) >= '0' && c <= '9')
    33         value = value * 10 + c - 48;
    34     a = s * value;
    35 }
    36 const int maxn = 50005;
    37 int n;
    38 ll arr1[maxn], arr2[maxn];
    39 ll dp[maxn];
    40 struct node {
    41     ll v, i;
    42     node(ll vv = 0, ll ii = 0) :i(ii), v(vv){}
    43     bool operator<(const node& a) const {
    44         return v > a.v || (v == a.v && i > a.i);
    45     }
    46 };
    47 int main() {
    48     //freopen("data.out", "w", stdout);
    49     //freopen("data.in", "r", stdin);
    50     //cin.sync_with_stdio(false);
    51     while (scanf("%d", &n) != EOF) {
    52         for (int i = 1; i <= n; ++i) scanf("%lld", arr1 + i);
    53         for (int i = 1; i <= n; ++i) scanf("%lld", arr2 + i);
    54         priority_queue<node> pq;
    55         dp[0] = 0;
    56         for (int i = 1; i <= n; ++i) {
    57             node tmp = node(dp[i - 1] + arr1[i], arr2[i] + i - 1);
    58             while (!pq.empty() && pq.top().i < i) pq.pop();
    59             pq.push(tmp);
    60             dp[i] = pq.top().v;
    61         }
    62         printf("%lld
    ", dp[n]);
    63     }
    64     return 0;
    65 }
    View Code

    hdu 5090

     1 //*************************
     2 //*******GL  HF************
     3 //*************************
     4 #include<cstdio>
     5 #include<cmath>
     6 #include<algorithm>
     7 #include<iostream>
     8 #include<cstring>
     9 #include<cstdlib>
    10 #include<map>
    11 #include<queue>
    12 #include<set>
    13 #include<vector>
    14 #pragma comment(linker, "/STACK:102400000,102400000")
    15 #define Local
    16 #define o(a) cout << #a << "  =  " << a << endl
    17 #define pb(a) push_back(a)
    18 #define lson l,mid,pos<<1
    19 #define rson mid+1,r,pos<<1|1
    20 typedef long long ll;
    21 using namespace std;
    22 
    23 void local(){
    24 #ifdef Local
    25     freopen("date.in","r",stdin);
    26     freopen("date.out","w",stdout);
    27 #endif
    28 }
    29 const int maxn = 550;
    30 bool debug = false;
    31 int n, k;
    32 int arr[maxn];
    33 priority_queue<int ,vector<int>,greater<int> > q;
    34 int main(){
    35     int T;
    36     scanf("%d", &T);
    37 
    38     while(T--){
    39         scanf("%d%d", &n, &k);
    40         int v;
    41         while(!q.empty()) q.pop();
    42         for(int i = 1; i <= n ; ++i){
    43             scanf("%d", &v);
    44             q.push(v);
    45         }
    46         int top;
    47         int c = 1;
    48         if(debug){ o(q.top());}
    49         while(!q.empty() && q.top() <= n){
    50             top = q.top();
    51             q.pop();
    52             if(debug){ o(top);}
    53             if(c != top){
    54                 q.push(top + k);
    55             }else {
    56             c++;
    57             }
    58 
    59             //cout << "OK" << endl;
    60         }
    61         if(!q.empty()){ cout << "Tom" << endl;}
    62         else cout << "Jerry" << endl;
    63     }
    64 }
    View Code
  • 相关阅读:
    佛學概要十四講表
    冰川时代4中英台词全集
    Linux Mysql 每天定时备份
    zabbix拓扑图
    搭建zabbix 3.4
    ★日常工作保养电脑及设备★
    宽带突然断网了,需要做如下应急措施
    预防这几点,可以让你的电脑长久耐用!!!!
    搭建简易的 DISCUZ论坛
    format 的常见用法
  • 原文地址:https://www.cnblogs.com/yeahpeng/p/4555620.html
Copyright © 2020-2023  润新知