DAG Optimal Coin Change
题目描述
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.
输入
Each test case contains integers v, n, f1, ..., fn in a line. It is guaranteed that n ≤ 10 and 0 < f1 < f2 < ...< fn.
输出
样例输入
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的更多相关文章
- [LeetCode] Coin Change 硬币找零
You are given coins of different denominations and a total amount of money amount. Write a function ...
- HDOJ 2069 Coin Change(母函数)
Coin Change Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...
- HDU 2069 Coin Change
Coin Change Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total S ...
- UVA 674 Coin Change(dp)
UVA 674 Coin Change 解题报告 题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=87730#problem/ ...
- JSU省赛队员选拔赛个人赛1(Coin Change、Fibbonacci Number、Max Num、单词数、无限的路、叠筐)
JSU省赛队员选拔赛个人赛1 一.题目概述: A.Coin Change(暴力求解.动态规划) B.Fibbonacci Number(递推求解) C.Max Num(排序.比较) D.单词数 ...
- C - Coin Change (III)(多重背包 二进制优化)
C - Coin Change (III) Time Limit:2000MS Memory Limit:32768KB 64bit IO Format:%lld & %llu ...
- Coin Change (IV) (dfs)
Coin Change (IV) Time Limit: 1000MS Memory Limit: 32768KB 64bit IO Format: %lld & %llu [Subm ...
- Coin Change (II)(完全背包)
Coin Change (II) Time Limit: 1000MS Mem ...
- [LeetCode] Coin Change 2 硬币找零之二
You are given coins of different denominations and a total amount of money. Write a function to comp ...
随机推荐
- 十五、Numpy-科学计算基础库
Numpy: NumPy(Numerical Python) 是科学计算基础库,提供大量科学计算相关功能,比如数据统计,随机数生成等.其提供最核心类型为多维数组类型(ndarray) ...
- (2)关于opencv解压
关于opencv解压,一定不能解压到你的C盘的 ProgramFile(x86)中,不然,你肯定不会成功,你要放在C盘的其他文件夹,或者是别的盘中 就是因为这一个错误,我弄了一天,哎哎,时间宝贵啊
- 吴裕雄--天生自然 JAVASCRIPT开发学习:函数调用
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...
- render_template()的各种用法
1.可以有很多个参数,第一个一定是模板的名字 2.可以传字典.列表.单个变量等等,还可以传函数,在模板中调用函数 后端函数: from flask import Flask from flask im ...
- AXURE方便的功能
(1)建立一个公共的页面,可以把一些常用的组建放进去,就和代码要封装方法一样,这样省区了用到一次画一次的麻烦. 其中可以包括:弹出框.图标.搜索框之类的. 当然还可以把经常用到的 登陆.注册.忘记密码 ...
- Linux下idea由于缺少相关权限导致的tomcat ERROR
昨天一天都在倒腾两个系统,也是醉了. 不过还好,系统修好了,在ubuntu下重新安装idea后,出现了这个错误: Intellij Idea Tmocat Error running Tomcat: ...
- EOF是什么?(笔记)
一.参考文章 1.EOF是什么?(阮一峰网络日志) 2.Linux 中的 EOF 到底是什么 二.知识点 1.EOF 定义在 /usr/include/stdio.h 文件中: 从上面 EOF 的定义 ...
- php empty,isset,is_null判断比较(差异与异同)
php empty,isset,is_null判断比较(差异与异同) 作者: 字体:[增加 减小] 类型:转载 做php开发时候,想必在使用:empty,isset,is_null 这几个函数时候,遇 ...
- Java线程(一)——创建线程的两种方法
Thread 和 Runnable Java程序是通过线程执行的,线程在程序中具有独立的执行路径.当多条线程执行时,它们之间的路径可以不同,例如,一条线程可能在执行switch的一个case语句,另一 ...
- 吴裕雄--天生自然TensorFlow2教程:numpy [ ] 索引
import tensorflow as tf a = tf.ones([1, 5, 5, 3]) a.shape a[0][0] numpy : 索引 a = tf.random.normal([4 ...