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 ...
随机推荐
- 单表:1.查询全部 2.条件查询 JSP Servlet
- spring mvc No mapping found for HTTP request with URI [/web/test.do] in DispatcherServlet with name 'spring'
原因: spring-servlet.xml 中 <context:component-scan base-package="com.test.controller" /&g ...
- jsp include
1.<%@ include file="a.jsp"%> 路径无法动态赋值,只能写成固定路径: 生成一个jsp页面,整个编译 2.<jsp:include pag ...
- April Fools Contest 2017 C
Description DO YOU EXPECT ME TO FIND THIS OUT? WHAT BASE AND/XOR LANGUAGE INCLUDES string? DON'T BYT ...
- Java项目的命名规则
Java类的命名规范如下: 1. 项目名全部小写 2. 包名全部小写 3. 类名首字母大写,如果类名由多个单词组成,每个单词的首字母都要大写. 如:public class MyFirstClass{ ...
- [在读]HTML5程序设计(第二版)
去年买的,看了30%不到,之后一直是搁置状态,内容还不错,确确实实纯粹讲H5的.
- 关于<meta NAME="keywords" CONTENT="">
昨天终于以实习身份入职一家小创业公司,今天让我多看看别人的网页怎么写的,发现了一个以前都没关注过的东西. <meta name="keywords" content=&quo ...
- HV000184: ParameterMessageInterpolator has been chosen, EL interpolation will not be supported问题解决
今天创建springboot项目的时候添加完依赖启动出现了这个错误 -- :: --- [ main] o.h.v.m.ParameterMessageInterpolator : HV000184: ...
- 在vscode中显示空格和tab符号
转自:https://blog.csdn.net/bmzk123/article/details/86501706 使用python时最烦人的就是代码对齐,而且tab和空格还不一样,为了便于对其,希望 ...
- 【HEVC简介】CTU、CU、PU、TU结构
参考文献:见<High Efficiency Video Coding (HEVC)>Block Structures and Parallelism Features in HEVC章 ...