cdoj 482 优先队列+bfs
Charitable Exchange
Time Limit: 4000/2000MS (Java/Others) Memory Limit: 65535/65535KB (Java/Others)
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 |
Case #1: -1 |
Source
一开始只有1元的东西,让你求出交换到价值至少为m的最少时间代价。
/******************************
code by drizzle
blog: www.cnblogs.com/hsd-/
^ ^ ^ ^
O O
******************************/
//#include<bits/stdc++.h>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<map>
#include<algorithm>
#include<queue>
#include<cmath>
#define ll long long
#define PI acos(-1.0)
#define mod 1000000007
using namespace std;
struct node
{
int to;
ll we;
int pre;
friend bool operator < (node a, node b)
{
return a.we > b.we;
}
}N[];
priority_queue<node> pq;
int t;
int n,m;
bool cmp(struct node aa,struct node bb)
{
return aa.pre<bb.pre;
}
ll bfs()
{
struct node exm,now;
while(!pq.empty()) pq.pop();
exm.pre=;
exm.to=;
exm.we=;
pq.push(exm);
int l=;
int i;
while(!pq.empty())
{
now=pq.top();
pq.pop();
if(now.to>=m)
{
return now.we;
}
for(i=l; i<=n; i++)
{
if(now.to>=N[i].pre&&now.to<N[i].to)//遍历可以用的边
{
exm.pre=now.to;
exm.to=N[i].to;
exm.we=now.we+N[i].we;
pq.push(exm);
l=i;//爆内存点
}
if(now.to<N[i].pre)//T点
break;
}
}
return -;
}
int main()
{
scanf("%d",&t);
{
for(int i=; i<=t; i++)
{
scanf("%d %d",&n,&m);
for(int j=; j<=n; j++)
scanf("%d %d %lld",&N[j].to,&N[j].pre,&N[j].we);
sort(N+,N++n,cmp);
ll ans=bfs();
printf("Case #%d: %lld\n",i,ans);
}
}
return ;
}
cdoj 482 优先队列+bfs的更多相关文章
- hdu 1026 Ignatius and the Princess I【优先队列+BFS】
链接: http://acm.hdu.edu.cn/showproblem.php?pid=1026 http://acm.hust.edu.cn/vjudge/contest/view.action ...
- ZOJ 649 Rescue(优先队列+bfs)
Rescue Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Sub ...
- 【POJ3635】Full Tank 优先队列BFS
普通BFS:每个状态只访问一次,第一次入队时即为该状态对应的最优解. 优先队列BFS:每个状态可能被更新多次,入队多次,但是只会扩展一次,每次出队时即为改状态对应的最优解. 且对于优先队列BFS来说, ...
- Codeforces 677D - Vanya and Treasure - [DP+优先队列BFS]
题目链接:http://codeforces.com/problemset/problem/677/D 题意: 有 $n \times m$ 的网格,每个网格上有一个棋子,棋子种类为 $t[i][j] ...
- POJ 2449 - Remmarguts' Date - [第k短路模板题][优先队列BFS]
题目链接:http://poj.org/problem?id=2449 Time Limit: 4000MS Memory Limit: 65536K Description "Good m ...
- CDOJ 482 Charitable Exchange bfs
Charitable Exchange Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.uestc.edu.cn/#/problem/s ...
- 【UESTC 482】Charitable Exchange(优先队列+bfs)
给你n个物品交换,每个交换用r,v,t描述,代表需要用r元的东西花费t时间交换得v元的东西.一开始只有1元的东西,让你求出交换到价值至少为m的最少时间代价.相当于每个交换是一条边,时间为边权,求走到价 ...
- UESTC 482 Charitable Exchange(优先队列+bfs)
Charitable Exchange Time Limit: 4000/2000MS (Java/Others) Memory Limit: 65535/65535KB (Java/Othe ...
- hdu - 1242 Rescue && hdu - 2425 Hiking Trip (优先队列+bfs)
http://acm.hdu.edu.cn/showproblem.php?pid=1242 感觉题目没有表述清楚,angel的朋友应该不一定只有一个,那么正解就是a去搜索r,再用普通的bfs就能过了 ...
随机推荐
- Android 4.3正式发布:四大新功能一览
在旧金山举行的新品发布会上,Google正式发布了Android 4.3,代号仍为“Jelly Bean”.此次更新并没有太大改变,只是紧跟4.1.4.2步伐, 新增了低功耗蓝牙.多用户登录等一系列功 ...
- 二模 (11) day2
第一题: 题目大意: 有一本n个单词的字典,其中每个单词的长度不超过4且大于0.现在给你一篇文章,文章中没有分隔符,只有小写字母.现在需要你修改最少的字母,使文章(长度为m 是由字典中的单词构成. n ...
- 牛场围栏(vijos 1054)
题目大意: 给出N种木棍(每种木棍数量无限)的长度(<=3000),每根木棍可以把它切掉[1,M]的长度来得到新的木棍. 求最大的不能被组合出来的长度. 如果任何长度都能组合出来或者最大值没有上 ...
- C++封装库
1.新建项目 -> Win32项目 选择DLL , 勾选 空项目 , 点击完成. 2.本例程,使用一个CPP文件 , 及一个头文件. 其中头文件包含函数声明,CPP文件实现函数声明. 3. ...
- Rhel6-haproxy+keepalived配置文档
系统环境: rhel6 x86_64 iptables and selinux disabled 主机: 192.168.122.119:haproxy,keepalived server19.exa ...
- PowerShell工具脚本---按行数切割大文本文件
我编写的PowerShell工具脚本,[按行数切割大(文本)文件],生成n个小文件. 主要目的是为了能够让excel快速处理.或用脚本并发处理文本. 注意: 1 如果有必要,你可以先用其他工具,把大文 ...
- 【kate总结】Matlab坐标轴问题
[kate总结]Matlab坐标轴问题 总结而言 行óYó高ó垂直 列óXó宽ó水平 Maplab中存有2张图片 1.JPG 宽(列):320 高(行):482 在matlab中显示 2.JPG ...
- iOS图片攻略之:有3x自动生成2x 1x图片
关键字:Xcode插件,生成图片资源 代码类库:其他(Others) GitHub链接:https://github.com/rickytan/RTImageAssets 本项目是一个 Xc ...
- php 未配置curl
用到PHP的curl功能,发现服务器上的PHP并没有配置curl,进而查询PHP官方文档,得知编译PHP时需要带上 –with-curl参数,才能把curl模块编译进去.我现在PHP已经编译安装进服务 ...
- IOS中nil/Nil/NULL的区别
类与对象的概念 类是对同一类事物高度的抽象,类中定义了这一类对象所应具有的静态属性(属性)和动态属性(方法). 对象是类的一个实例,是一个具体的事物. 类与对象是抽象与具体的关系. 类其实就是一种数据 ...