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 ...
随机推荐
- 洛谷 P2341 [HAOI2006]受欢迎的牛|【模板】强连通分量
题目传送门 解题思路: 先求强联通分量,缩点,然后统计新图中有几个点出度为0,如果大于1个,则说明这不是一个连通图,答案即为0.否则入度为0的那个强连通分量的点数即为答案 AC代码: #include ...
- 用户使用API函数对创建的文件进行读写操作
HANDLE handle; //定义文件句柄 ]; //定义缓冲区 int i; //接收实际操作的字节数 CString str; //定义字符串变量 handle = ::CreateFile( ...
- NBU For Windows 更新后无法启动Java Console
系统环境:Win Server 2016 备份软件:NBU 8.1 Case :Windows系统默认自动安装系统补丁,重启过程中自动进行了Update,打上了最新的补丁后,NBU Java Co ...
- github访问过慢解决
为了更加愉快地使用全球最大同性交友网站上的优质资源,我们来做一些简单的本机上的调整. 通过查看下载链接,能够发现最终被指向到Amazon的服务器(http://github-cloud.s3.amaz ...
- Linux-线程引入
1.使用进程技术的优势 (1).CPU分时复用,单核心CPU可以实现宏观上的并行 (2).实现多任务系统需求(多任务的系统是客观的) 2.进程技术的劣势 (1).进程间切换开销大 (2).进程间通信麻 ...
- Activity的Launch mode详解 :standard(默认), singleTop, singleTask和 singleInstance
本文参考了此文http://hi.baidu.com/amauri3389/blog/item/a54475c2a4b2f040b219a86a.html 另附 android task与back s ...
- CodeForces 1294D MEX maximizing(思维)
http://codeforces.com/contest/1294/problem/D 大致题意: 刚开始有一个空集合,会往里添加q次数,每次加一个值,而且你可以让这个数任意加减x若干次 每次添加后 ...
- php开启opcache
OPcache 通过将 PHP 脚本预编译的字节码存储到共享内存中来提升 PHP 的性能, 存储预编译字节码的好处就是 省去了每次加载和解析 PHP 脚本的开销. 一.php.ini配置opchche ...
- 配置Action
配置Action 实现了Action类后,就可以在struts.xml中配置该Action类了.配置Action就是让Struts2 知道哪个Action处理哪个请求,也就是完成用户请求和Action ...
- textarea高度自适应解决方法
引入autosize.js <script src="./autosize.js"></script> autosize(document.getEleme ...