题目描述

In a 10-dollar shop, everything is worthy 10 dollars or less. In order to serve customers more effectively at the cashier, change needs to be provided in the minimum number of coins.
In this problem, you are going to provide a given value of the change in different coins. Write a program to calculate the number of coins needed for each type of coin.
The input includes a value v, a size of the coinage set n, and a face value of each coin, f1, f2, ..., fn. The output is a list of numbers, namely, c1, ..., cn, indicating the number of coins needed for each type of coin. There may be many ways for the change. The value v is an integer satisfying 0 < v ≤ 2000, representing the change required
in cents. The face value of a coin is less than or equal to 10000. The output of your program should take the combination with the least number of coins needed.
For example, the Hong Kong coinage issued by the Hong Kong Monetary Authority consists of 10 cents, 20 cents, 50 cents, 1 dollar, 2 dollars, 5 dollars and 10 dollars would be represented in the input by n = 7, f1 = 10, f2 = 20, f3 = 50, f4 = 100, f5 = 200, f6 = 500, f7 = 1000.

输入

The test data may contain many test cases, please process it to the end of the file.
Each test case contains integers v, n, f1, ..., fn in a line. It is guaranteed that n ≤ 10 and 0 < f1 < f2 < ...< fn.

输出

The output be n numbers in a line, separated by space. If there is no possible change, your output should be a single −1. If there are more than one possible solutions, your program should output the one that uses more coins of a lower face value.

样例输入

2000 7 10 20 50 100 200 500 1000
250 4 10 20 125 150
35 4 10 20 125 150
48 4 1 8 16 20
40 4 1 10 13 37
43 5 1 2 21 40 80

样例输出

0 0 0 0 0 0 2
0 0 2 0
-1
0 1 0 2
3 0 0 1
1 1 0 1 0
 #include<bits/stdc++.h>
const int inf=0x3f3f3f;
using namespace std;
int dp[];
int s[];
int f[];
int n;
void ptans(int t)//递推找上一个最小的钱
{
for(int i=;i<=n;i++)
if(t>=s[i]&&dp[t]==dp[t-s[i]]+){
f[i]++;
ptans(t-s[i]);
break;
}
}
int main()
{
int i,j;
int ans;
ios::sync_with_stdio(false);
while(cin>>ans){
memset(dp,,sizeof(dp));
memset(s,,sizeof(s));
memset(f,,sizeof(f));//初始化
for(i=;i<=ans;i++) dp[i]=inf;
cin>>n;
for(i=;i<=n;i++) cin>>s[i];
for(i=;i<=ans;i++)
for(j=;j<=n;j++) if(i>=s[j]) dp[i]=min(dp[i],dp[i-s[j]]+);
if(dp[ans]==inf) cout<<-<<endl;
else{
ptans(ans);
for(i=;i<=n;i++){
if(i!=) cout<<" ";
cout<<f[i];
}
cout<<endl;
}
}
return ;
}

DAG 相当于一环套一环,但一个并不能直接或间接套在自己内部

就跟导弹拦截类似的那种

#include<bits/stdc++.h>
const int inf=0x3f3f3f;
using namespace std;
int dp[];
int s[];
int f[];
int gf[];
int n;
int main()
{
int i,j;
int ans;
ios::sync_with_stdio(false);
while(cin>>ans){
memset(dp,,sizeof(dp));
memset(s,,sizeof(s));
memset(f,,sizeof(f));
memset(gf,,sizeof(gf));
for(i=;i<=ans;i++) dp[i]=inf;
cin>>n;
for(i=;i<=n;i++) cin>>s[i];
for(i=;i<=ans;i++)
for(j=;j<=n;j++)
if(i>=s[j]){
if(dp[i]>dp[i-s[j]]+){
dp[i]=dp[i-s[j]]+;
gf[i]=j;
}
}
if(dp[ans]==inf) cout<<-<<endl;
else{
int op=ans;
while(op!=){
f[gf[op]]++;
op=op-s[gf[op]];
}
for(i=;i<=n;i++){
if(i!=) cout<<" ";
cout<<f[i];
}
cout<<endl;
}
}
return ;
}

这个代码没有递归的过程 而是直接在更新的过程中记录路径

DAG Optimal Coin Change的更多相关文章

  1. [LeetCode] Coin Change 硬币找零

    You are given coins of different denominations and a total amount of money amount. Write a function ...

  2. HDOJ 2069 Coin Change(母函数)

    Coin Change Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...

  3. HDU 2069 Coin Change

    Coin Change Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total S ...

  4. UVA 674 Coin Change(dp)

    UVA 674  Coin Change  解题报告 题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=87730#problem/ ...

  5. JSU省赛队员选拔赛个人赛1(Coin Change、Fibbonacci Number、Max Num、单词数、无限的路、叠筐)

    JSU省赛队员选拔赛个人赛1 一.题目概述: A.Coin Change(暴力求解.动态规划)     B.Fibbonacci Number(递推求解) C.Max Num(排序.比较) D.单词数 ...

  6. C - Coin Change (III)(多重背包 二进制优化)

    C - Coin Change (III) Time Limit:2000MS     Memory Limit:32768KB     64bit IO Format:%lld & %llu ...

  7. Coin Change (IV) (dfs)

    Coin Change (IV) Time Limit: 1000MS   Memory Limit: 32768KB   64bit IO Format: %lld & %llu [Subm ...

  8. Coin Change (II)(完全背包)

                                                               Coin Change (II) Time Limit: 1000MS   Mem ...

  9. [LeetCode] Coin Change 2 硬币找零之二

    You are given coins of different denominations and a total amount of money. Write a function to comp ...

随机推荐

  1. [SDOI2016]游戏(树剖+李超树)

    趁着我把李超树忘个一干二净的时候来复习一下吧,毕竟马上NOI了. 题解:看着那个dis就很不爽,直接把它转换成深度问题,然后一条直线x->y,假设其lca为z,可以拆分成x->z和z-&g ...

  2. 7206VXR系列板卡简介(转)

    原文链接:https://www.it610.com/article/3116409.htm作者:hjw2011 ATM板卡介绍 板卡PA-A6-OC3MM,PA-A6-OC3SMI,PA-A6-OC ...

  3. 吴裕雄--天生自然 JAVASCRIPT开发学习: 正则表达式

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...

  4. SQL基础教程(第2版)第6章 函数、谓词、CASE表达式:6-1 函数

    6-1 各种各样的函数 ● 函数的种类很多,无需全都记住,只需要记住具有代表性的函数就可以了,其他的可以在使用时再进行查询. ■函数的种类所谓函数,就是输入某一值得到相应输出结果的功能,输入值称为参数 ...

  5. [RoarCTF 2019]Easy Calc-协议层攻击之HTTP请求走私

    0X01:什么是HTTP请求走私 HTTP请求走私属于协议层攻击,是服务器漏洞的一种. HTTP请求走私是一种干扰网站处理从一个或多个用户接收的HTTP请求序列的方式的技术.使攻击者可以绕过安全控制, ...

  6. Python—后台运行(nohup 、&、 2>&1详解)

    一.脚本文件(test.py) # -*- coding: UTF-8 -*- import time print("hello"," python") os. ...

  7. Maven--Eclipse maven相关配置

    选择自己安装的 Maven 版本: 更改配置文件路径,这里选择自己安装的 Maven 下的配置文件,方便配置及统一控制:

  8. myeclipse跟tomcat的同步

    一般来说,我们在myeclipse里把文件内容改了并保存之后,直接刷新网页就可以非常直观的看到内容的改变. 这是因为myeclipse检测到文件内容的变动,及时地把新的文件部署到了tomcat上. m ...

  9. Linux文件共享的实现方式

    前两天跟老师去北京开了一个会议,好久没学习了,今天才回学校,其中的辛酸就不说了.来正文: 1.什么是文件共享 (1).文件共享就是同一个文件(同一个文件指的是同一个inode,同一个pathname) ...

  10. centos快速安装mysql

    1. wget http://repo.mysql.com/mysql-community-release-el7-5.noarch.rpm 2. rpm -ivh mysql-community-r ...