POJ 1252 Euro Efficiency(最短路 完全背包)
题意:
给定6个硬币的币值, 问组成1~100这些数最少要几个硬币, 比如给定1 2 5 10 20 50, 组成40 可以是 20 + 20, 也可以是 50 -10, 最少硬币是2个。
分析:
这道题可以转化成是一道最短路的方法去做, 设一开始的起点为0(什么硬币都不取), 然后每个点都有12条路(对应正负6种币值), 每条路的权值为1,
令dis[maxn]初始化为inf, 求出dis[1]~dis[100]即可。
但是最大值maxn要取比100更大, 比如 构造100的最优方法为51+51-1-1. 那么我们就需要102面值金钱的构造dist[102] 才能正确的推出dist[100], 虽然不知道这个最大值多少,但这个最大值取200可以过这题。
#include <cstdio>
#include <iostream>
#include <queue>
#include <cstring>
#define mem(a) memset(a, 0, sizeof(a))
using namespace std;
const int maxn = ;
const int inf = 1e9;
int coin[];
int dis[maxn];
int vis[maxn];
void spfa(){
mem(vis);
queue<int> q;
fill(dis,dis+maxn, inf);
dis[] = ;
vis[] = ;
q.push();
while(!q.empty()){
int u = q.front();
for(int i = ; i < ; i++){
int v = u + coin[i];
if(v < || v > ) continue;//如果点v是负数, 那么不需要入队。
if(dis[v] > dis[u] + ){
dis[v] = dis[u] + ;
if(!vis[v]){
vis[v] = ;
q.push(v);
}
}
}
// vis[u] = 0; 这里其实不用标记, 因为每个点只会入队一次,边的权值都是1, 怎么扩展都是最短路
q.pop();
}
}
int main()
{
int t;
cin >> t;
while ( t -- ) {
for ( int i = ; i < ; ++ i ){
cin >> coin[ *i ];
coin[ * i + ] = -coin[ * i];//构造币值为负数的硬币
}
spfa();
double sum = 0.0;
int maxdis = ;
for ( int i = ; i <= ; ++ i ) {
sum += dis [i];
if ( dis[i] > maxdis )
maxdis = dis[i];
}
printf("%.2f %d\n",sum/100.0, maxdis);
}
return ;
}
POJ 1252 Euro Efficiency(最短路 完全背包)的更多相关文章
- POJ 1252 Euro Efficiency(完全背包, 找零问题, 二次DP)
Description On January 1st 2002, The Netherlands, and several other European countries abandoned the ...
- POJ 1252 Euro Efficiency ( 完全背包变形 && 物品重量为负 )
题意 : 给出 6 枚硬币的面值,然后要求求出对于 1~100 要用所给硬币凑出这 100 个面值且要求所用的硬币数都是最少的,问你最后使用硬币的平均个数以及对于单个面值所用硬币的最大数. 分析 : ...
- POJ 1252 Euro Efficiency
背包 要么 BFS 意大利是说给你几个基本的货币,组成 1~100 所有货币,使用基本上的货币量以最小的. 出口 用法概率.和最大使用量. 能够BFS 有可能 . 只是记得数组开大点. 可能会出现 1 ...
- POJ Euro Efficiency 1252
Euro Efficiency Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 4109 Accepted: 1754 D ...
- Euro Efficiency(完全背包)
Euro Efficiency Time Limit : 2000/1000ms (Java/Other) Memory Limit : 20000/10000K (Java/Other) Tot ...
- POJ 3635 - Full Tank? - [最短路变形][手写二叉堆优化Dijkstra][配对堆优化Dijkstra]
题目链接:http://poj.org/problem?id=3635 题意题解等均参考:POJ 3635 - Full Tank? - [最短路变形][优先队列优化Dijkstra]. 一些口胡: ...
- HDU 3339 In Action【最短路+01背包】
题目链接:[http://acm.hdu.edu.cn/showproblem.php?pid=3339] In Action Time Limit: 2000/1000 MS (Java/Other ...
- HDU 3339 In Action【最短路+01背包模板/主要是建模看谁是容量、价值】
Since 1945, when the first nuclear bomb was exploded by the Manhattan Project team in the US, the n ...
- POJ 1337 A Lazy Worker(区间DP, 背包变形)
Description There is a worker who may lack the motivation to perform at his peak level of efficiency ...
随机推荐
- CI框架错误汇总
2017年1月13日12:09:02 [1] A PHP Error was encounteredSeverity: NoticeMessage: Undefined variable: aticl ...
- the little schemer 笔记(7)
第七章 Friends and Relations 这是一个set集合吗 (apple peaches apple plum) 不是,apple出现了不止一次 (set? lat) 是真还是假,其中l ...
- Caffe实战一(环境准备及CPU模式下编译)
经过前几天的折腾,终于把Ubuntu16.04开发环境给搭建了起来,包括win10+Ubuntu双系统的安装.系统安装后的优化等等. 详见之前的文章:Ubuntu16.04.2 LTS 64bit系统 ...
- Educational Codeforces Round 46 (Rated for Div. 2) A. Codehorses T-shirts
Bryce1010模板 http://codeforces.com/problemset/problem/1000/A 题意: 问你将一种类型的衣服转换成另一种的最小次数. #include<b ...
- sql 语句操作,修改字段中字符串的一部分
update 表名 set 字段=replace(字段,‘替换的部分’,‘替换后的字符串’): update 表名 set A=replace( A, '海淀', '朝阳') where A like ...
- 牛客网NOIP赛前集训营-普及组
第一场: A-绩点 题目描述 小A刚考完大学考试.现在已经出了n门课的成绩,他想自己先算一下这些课的绩点是多少.设第i门课的他拿到的绩点是gpai,而这门课的学分是sci,那么他的总绩点用下面的公式计 ...
- No space left on device
No space left on device 数据库无法启动, 发现是内存没有清空导致. 处理过程: ipcs ipcrm
- IF-MIB::ifTable = No Such Object available on this agent at this OID
在server端口执行如下命令 [root@localhost ~]# snmpwalk -v2c -c public 客户端ip ifIF-MIB::ifTable = No Such Object ...
- IOS存储目录documents你在哪里啊
iphone沙箱模型的有四个文件夹,分别是什么,永久数据存储一般放在什么位置,得到模拟器的路径的简单方式是什么. documents,tmp,app,Library. (NSHomeDirectory ...
- codevs 2761 脏话过滤
时间限制: 1 s 空间限制: 8000 KB 题目等级 : 白银 Silver 题目描述 Description 某论坛希望打造文明论坛,对于每个帖子需要将脏话换成*输出. 脏话有38,25 ...