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 ...
随机推荐
- LVS DR模式搭建、keepalived+LVS搭建介绍
参考文献 http://blog.51cto.com/taoxie/2066993 疑问: 1.为什么要修改RealServer的返回arp响应和发送arp请求参数 echo "1&quo ...
- 代码review还是需要再仔细点-一次crash异常分析的总结
版本发布之后,外网新增了一些crash, 从mini dump看,的确是有异常的,619行crash了. 代码如下: 奔溃的地方如下 可以看到是Zip add的时候指针空了, 为什么呢? 结合代码,可 ...
- ZOJ 3757 Alice and Bod 模拟
上次的ZJU月赛题,规则比较复杂,当时就连题意都没摸清楚,只觉得非常复杂 比完后敲啊敲啊敲,连续WA啊,该反思下自己,没按照题意来设置条件,题目中说了 白球入袋并且... 给对手加分 ,白球未入袋并且 ...
- Java学习十五
学习内容: MyBaits 以前从来没有接触过mybatis,通过今天的学习知道这是一个框架,适用于关注SQL优化和需要频繁更新的项目. 今天做一个关于mybatis项目的入门小程序,效果很不理想. ...
- IOS下的safari不支持localStorage?
同事在统计日志的时候,想用localStorag去记载一些什么,但是在各大浏览器都运行的良好的基础上,唯独IOS下的safari一直静静无声,没有任何反应.打印localStorage都是Object ...
- torch基础学习
目录 Pytorch Leture 05: Linear Rregression in the Pytorch Way Logistic Regression 逻辑回归 - 二分类 Lecture07 ...
- 我读《DOOM启世录》——成为一个真正厉害的人
序言 谈到游戏, 你的当然会想到几乎统治游戏市场多年的英雄联盟,你可能还会想起前段时间风头大盛的王者荣耀手游,你应该还会想起正在冲击着游戏市场的"吃鸡"类型游戏. 那么, 大家是否 ...
- 深入分析Java反射(六)-反射调用异常处理
前提 Java反射的API在JavaSE1.7的时候已经基本完善,但是本文编写的时候使用的是Oracle JDK11,因为JDK11对于sun包下的源码也上传了,可以直接通过IDE查看对应的源码和进行 ...
- javaweb学习——会话技术(二)
文中部分借鉴了:https://www.cnblogs.com/xdp-gacl/p/3855702.html https://blog.csdn.net/p744174529/article/det ...
- Java使用Sftp实现对跨服务器上传、下载、打包、写入相关操作
1.Maven引入jar <dependency> <groupId>com.jcraft</groupId> <artifactId>jsch< ...