poj-1384 Piggy-Bank
poj-1384 Piggy-Bank 地址:http://poj.org/problem?id=1384
题意:
知道盒子里面的物体的总重量,得到每一种硬币的价格和重量,求最少钱构成盒子物体总重量的钱的数量。
分析:
典型的不限重背包问题。当然维度也是在可以接受的范围, 直接设置dp数组进行min操作。
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <string>
using namespace std;
const int maxn = ;
const int max_val = ; int E,F, n, dp[max_val+]; struct Node{
int p,w;
}nd[maxn]; int cmp(const void* a, const void* b){
Node* aa = (Node *)a;
Node* bb = (Node *)b;
return (bb->w*1.0)/(bb->p*1.0) - (aa->w*1.0)/(aa->p*1.0);
} int main(){
freopen("in.txt", "r", stdin); int test_num, i, j;
scanf("%d", &test_num);
while(test_num--){
scanf("%d %d", &E, &F);
scanf("%d", &n);
for(i=; i<n; i++){
scanf("%d %d", &nd[i].p, &nd[i].w);
}
memset(dp, 0x3f3f3f3f, sizeof(dp));
dp[] = ;
for(i=; i<n; i++){
for(j=nd[i].w; j<=(F-E); j++){
if(dp[j - nd[i].w] != 0x3f3f3f3f ){
dp[j] = min(dp[j-nd[i].w]+nd[i].p, dp[j]);
}
}
}
if(dp[F-E] == 0x3f3f3f3f){
printf("This is impossible.\n");
}else{
printf("The minimum amount of money in the piggy-bank is %d.\n", dp[F-E]);
}
}
return ;
}
poj-1384 Piggy-Bank的更多相关文章
- poj 1384 Piggy-Bank(全然背包)
http://poj.org/problem?id=1384 Piggy-Bank Time Limit: 1000MS Memory Limit: 10000K Total Submissions: ...
- POJ 1384 POJ 1384 Piggy-Bank(全然背包)
链接:http://poj.org/problem?id=1384 Piggy-Bank Time Limit: 1000MS Memory Limit: 10000K Total Submissio ...
- POJ 1384 Piggy-Bank (ZOJ 2014 Piggy-Bank) 完全背包
POJ :http://poj.org/problem?id=1384 ZOJ:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode ...
- POJ 1384 Intervals (区间差分约束,根据不等式建图,然后跑spfa)
传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1384 Intervals Time Limit: 10000/5000 MS (Java/Others ...
- poj 1384 Piggy-Bank(完全背包)
Piggy-Bank Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 10830 Accepted: 5275 Descr ...
- POJ 1384
求猜存钱罐中至少有多少钱.容易知道金币总的重量,接着背包. #include<cstdio> #include<iostream> using namespace std; # ...
- POJ 1384 Piggy-Bank 背包DP
所谓的全然背包,就是说物品没有限制数量的. 怎么起个这么intimidating(吓人)的名字? 事实上和一般01背包没多少差别,只是数量能够无穷大,那么就能够利用一个物品累加到总容量结尾就能够了. ...
- poj 1384完全背包
题意:给出猪罐子的空质量和满质量,和n个硬币的价值和质量,求猪罐子刚好塞满的的最小价值. 思路:选择硬币,完全背包问题,塞满==初始化为无穷,求最小价值,min. 代码: #include<io ...
- ACM Piggy Bank
Problem Description Before ACM can do anything, a budget must be prepared and the necessary financia ...
- POJ 1384 Piggy-Bank【完全背包】+【恰好完全装满】(可达性DP)
题目链接:https://vjudge.net/contest/217847#problem/A 题目大意: 现在有n种硬币,每种硬币有特定的重量cost[i] 克和它对应的价值val[i]. 每 ...
随机推荐
- iNeedle系统之国舜项目
一.简介 本周公司接了一个小项目,是给北京国舜科技股份有限公司做一个HTTP相关的小功能产品.大概实现功能是将交换机的源数据通过解析,分析出HTTP包配对的request和response头,并把每对 ...
- Android 横竖屏切换小结
(自己体会:每次横竖屏自动切时都会run Activity的onCreate,即相当后重新进入Activity初始化一样:) 转自:http://www.cnblogs.com/franksunny/ ...
- MYSQL:使用\G参数改变输出结果集的显示方式
在mysql命令行工具中执行查询时,当表的列很多的时候显示很乱. 上面的显示你肯定看不清楚吧.以上方式是默认以列(表格)形式显示的.那怎么以行(表单)的方式显示呢,请看下面 OK,搞定. 参考文档:h ...
- 单源最短路径算法——Dijkstra算法
#include <stdio.h> #include <stdlib.h> #include <string.h> /* run this program usi ...
- stm32之NVIC
非本人原创,转载自http://blog.csdn.net/denghuanhuandeng/article/details/8350392 STM32的NVIC理解 例程: /* Configur ...
- linux挂载windows上的共享文件夹
假定win机d:/folder/share的共享名为 share , 有用户administrator ,密码123 在linux机上,把share挂到/mnt目录:mount -t cifs -o ...
- dba诊断之IO
--查看占用系统io较大的session SELECT se.sid,se.serial#,pr.SPID,se.username,se.status,se.terminal,se.program ...
- UNITY 2D入门基础教程
Unity4.3增加了原生的2D开发环境,新建项目时选2D http://blog.1vr.cn/?p=1422
- java 27 - 5 反射之 通过反射获取成员方法并使用
类Method:提供关于类或接口上单独某个方法(以及如何访问该方法)的信息. A:获取所有方法 数组 1.getMethods 获取该类包括其父类的公共成员方法 2.getDeclaredMetho ...
- Tree
//Header.h #ifndef _HEAD_ #define _HEAD_ #include <queue> #include <iostream> using name ...