DAG Optimal Coin Change

题目描述

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
 1 #include<bits/stdc++.h>
 2 const int inf=0x3f3f3f;
 3 using namespace std;
 4 int dp[10010];
 5 int s[20];
 6 int f[20];
 7 int n;
 8 void ptans(int t)//递推找上一个最小的钱
 9 {
10     for(int i=1;i<=n;i++)
11     if(t>=s[i]&&dp[t]==dp[t-s[i]]+1){
12         f[i]++;
13         ptans(t-s[i]);
14         break;
15     }
16 }
17 int main()
18 {
19     int i,j;
20     int ans;
21     ios::sync_with_stdio(false);
22     while(cin>>ans){
23         memset(dp,0,sizeof(dp));
24         memset(s,0,sizeof(s));
25         memset(f,0,sizeof(f));//初始化
26         for(i=1;i<=ans;i++) dp[i]=inf;
27         cin>>n;
28         for(i=1;i<=n;i++) cin>>s[i];
29         for(i=1;i<=ans;i++)
30           for(j=1;j<=n;j++)  if(i>=s[j]) dp[i]=min(dp[i],dp[i-s[j]]+1);
31         if(dp[ans]==inf) cout<<-1<<endl;
32         else{
33         ptans(ans);
34         for(i=1;i<=n;i++){
35             if(i!=1) cout<<" ";
36             cout<<f[i];
37         }
38         cout<<endl;
39         }
40     }
41     return 0;
42 }
View Code

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

就跟导弹拦截类似的那种

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

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

全部评论

相关推荐

来,说点可能被同行“骂”的大实话。🙊当初接数字马力Offer时,朋友都说:“蚂蚁的“内包”公司?你想清楚啊!”但入职快一年后的今天,我反而对他有了不一样的看法!🔹&nbsp;是偏见?还是信息差!之前没入职之前外面都在说什么岗位低人一等这类。实际上:这种情况不可至否,不能保证每个团队都是其乐融融。但我在的部门以及我了解的周边同事都还是十分好相处的~和蚂蚁师兄师姐之间也经常开一些小玩笑。总之:身份是蚂蚁公司给的,地位是自己挣的(一个傲娇女孩的自述)。🔹&nbsp;待遇?玩的就是真实!试用期工资全额发!六点下班跑得快(早9晚6或者早10晚7,动态打卡),公积金顶格交。别听那些画饼的,到手的钱和下班的时间才是真的(都是牛马何必难为牛马)。🔹&nbsp;能不能学到技术?来了就“后悔”!我们拥有权限直通蚂蚁知识库,技术栈多到学不完。说“学不到东西”的人,来了可能后悔——后悔来晚了(哈哈哈哈,可以不学但是不能没有)!💥&nbsp;内推地址:https://app.mokahr.com/su/ueoyhg❗我的内推码:NTA6Nvs走我的内推,可以直达业务部门,面试流程更快速,进度可查!今天新放HC,之前挂过也能再战!秋招已经正式开始啦~机会就摆在这,敢不敢来试一试呢?(和我一样,做个勇敢的女孩)
下午吃泡馍:数字马力的薪资一般哇,5年经验的java/测试就给人一万出头,而且刚入职第三天就让人出差,而且是出半年
帮你内推|数字马力 校招
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务