题目描述

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. Arduino学习——u8glib提供的字体样式

    Fonts, Capital A Height4 Pixel Height  U8glib Font FontStruct5 Pixel Height  04 Font 04 Font 04 Font ...

  2. 最短路问题--P4779 单源最短路(标准版)Dijkstra堆优化

    题目背景 2018 年7月 19 日,某位同学在 NOI Day 1 T1 归程 一题里非常熟练地使用了一个广为人知的算法求最短路. 最终,他因此没能与理想的大学达成契约. 小 F 衷心祝愿大家不再重 ...

  3. 【mui webAPP】HBuilder创建的iOS平台设置沉浸式状态栏

    应用占用全屏区域,而系统状态栏需要预留出系统状态栏高度. HBuilder创建的应用默认不使用沉浸式状态栏样式,需要进行如下配置开启:打开应用的manifest.json文件,切换到代码视图,在plu ...

  4. vscode 集成git bash, mingw, mintty 的terminal

    设置 右上角打开json文件的设置 输入以下代码: "terminal.external.windowsExec": "D:\\Program Files\\Git\\b ...

  5. 更改php.ini配置

    vi /etc/php.ini #编辑修改 @ini_set('memory_limit',        '64M');                                      / ...

  6. php empty,isset,is_null判断比较(差异与异同)

    php empty,isset,is_null判断比较(差异与异同) 作者: 字体:[增加 减小] 类型:转载 做php开发时候,想必在使用:empty,isset,is_null 这几个函数时候,遇 ...

  7. 安装使用离线版本的维基百科(Wikipedia)

    1 相关背景 平常大家在上网查询一些基本概念的时候常常会参考维基百科上面的资料,但是由于方校长研制的GFW(长城防火墙系统)强大的屏蔽功能,好多链接打开以后,不出意外会出现著名的“404NOT FOU ...

  8. 前端Json 增加,删除,修改元素(包含json数组处理)

    一:基础JSON对象  二:JSON数组数据 以下为增删修改方法: <!DOCTYPE html> <html lang="en"> <head> ...

  9. [Algo] 118. Array Deduplication IV

    Given an unsorted integer array, remove adjacent duplicate elements repeatedly, from left to right. ...

  10. Unity3D事件函数的执行顺序

    In Unity scripting, there are a number of event functions that get executed in a predetermined order ...