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 ...
随机推荐
- spring boot eureka server
ServerApplication @EnableEurekaServer @SpringBootApplication public class ServerApplication { public ...
- html 文本溢出显示省略号 .....
- 题解报告:NYOJ #78 圈水池(打印凸包顶点)
描述: 有一个牧场,牧场上有很多个供水装置,现在牧场的主人想要用篱笆把这些供水装置圈起来,以防止不是自己的牲畜来喝水,各个水池都标有各自的坐标,现在要你写一个程序利用最短的篱笆将这些供水装置圈起来!( ...
- MAX458X多通道模拟切换开关(类似74HC4051)
- HDU 2227 Find the nondecreasing subsequences dp思想 + 树状数组
http://acm.hdu.edu.cn/showproblem.php?pid=2227 用dp[i]表示以第i个数为结尾的nondecreasing串有多少个. 那么对于每个a[i] 要去找 & ...
- 前端的百度地图的api的使用
1.打开百度地图官方api网页 http://lbsyun.baidu.com/ 2.点击开发文档 3.选择对应的api 4.点击DEMO详情 5.得到源码复制到你的代码中 <!DOCTYPE ...
- 1. UI Tests简介
(1) User Interface Testing UI Testing库主要提供了与App中的UI元素进行查找和交互的能力,这使得我们可以通过验证UI元素的状态来测试App是否正常运行. ...
- 网页尺寸scrollHeight/offsetHeight
scrollHeight和scrollWidth,获取网页内容高度和宽度. 一.针对IE.Opera: scrollHeight 是网页内容实际高度,可以小于 clientHeight. 二.针对NS ...
- checkbox:click事件触发span元素内容改变
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- ES6学习笔记(10)----Set和Map数据结构
参考书<ECMAScript 6入门>http://es6.ruanyifeng.com/ Set和Map数据结构 1.Set 基本用法 Set是一种新的数据结构,它的成员都是唯一 ...