L3-001. 凑零钱(dfs + 各种剪枝)
来源:互联网 发布:centos 没有桌面 编辑:程序博客网 时间:2024/06/08 14:25
L3-001. 凑零钱
时间限制
200 ms
内存限制
65536 kB
代码长度限制
8000 B
判题程序
Standard
作者
陈越
韩梅梅喜欢满宇宙到处逛街。现在她逛到了一家火星店里,发现这家店有个特别的规矩:你可以用任何星球的硬币付钱,但是绝不找零,当然也不能欠债。韩梅梅手边有104枚来自各个星球的硬币,需要请你帮她盘算一下,是否可能精确凑出要付的款额。
输入格式:
输入第一行给出两个正整数:N(<=104)是硬币的总个数,M(<=102)是韩梅梅要付的款额。第二行给出N枚硬币的正整数面值。数字间以空格分隔。
输出格式:
在一行中输出硬币的面值 V1 <= V2 <= ... <= Vk,满足条件 V1 + V2 + ... + Vk = M。数字间以1个空格分隔,行首尾不得有多余空格。若解不唯一,则输出最小序列。若无解,则输出“No Solution”。
注:我们说序列{A[1], A[2], ...}比{B[1], B[2], ...}“小”,是指存在 k >= 1 使得 A[i]=B[i] 对所有 i < k 成立,并且 A[k] < B[k]。
输入样例1:8 95 9 8 7 2 3 4 1输出样例1:
1 3 5输入样例2:
4 87 2 4 3输出样例2:
No Solution
//本题需要强大的剪枝
#include <bits/stdc++.h>using namespace std;#define maxn 11000int vis[maxn];int s[maxn];int tmp[maxn];int n, m;int cnt;int flag;int all;void dfs(int index,int sum, int cnt,int tol)//tol 为剩余的钱数{ if(flag || sum > m || tol < m - sum) return; if(sum == m) { for(int i = 0; i < cnt - 1; i++) printf("%d ",tmp[i]); printf("%d\n",tmp[cnt - 1]); flag = 1; return; } for(int i = index + 1; i < n; i++) { if(!vis[i] && s[i] <= m - sum) { vis[i] = 1; tmp[cnt] = s[i]; dfs(i,sum + s[i], cnt+1, tol - s[i]); if(flag) return; vis[i] = 0; } }}int main(){ scanf("%d%d", &n, &m); all = 0; for(int i = 0; i < n; i++) { scanf("%d", &s[i]); all += s[i]; } sort(s, s + n); cnt = 0; dfs(-1,0, cnt,all); if(!flag) printf("No Solution\n");}
0 0
- L3-001. 凑零钱(dfs + 各种剪枝)
- PAT-4C-L3-001-凑零钱(DFS剪枝)
- 团体程序设计天梯赛-练习集 L3-001. 凑零钱 强行dfs+剪枝 解题报告
- L3-1. 凑零钱 (DFS)
- L3-001. 凑零钱
- L3-001. 凑零钱
- L3-001. 凑零钱
- L3-001. 凑零钱
- L3-001. 凑零钱
- L3-001. 凑零钱
- L3-001. 凑零钱
- PAT CCCC L3-1 凑零钱(DFS)
- L3-001. 凑零钱(dp)
- L3-001. 凑零钱 (01背包)@
- L3-001. 凑零钱【dp】
- PAT--L3-001. 凑零钱
- PAT L3-001. 凑零钱(背包&路径记录)
- PAT L3-001. 凑零钱((背包&路径记录)
- spring data jpa hibernate 映射 postgres jsonb
- ipv6高级网络配置
- Linux内核RCU机制详解
- 养成良好的行文风格
- 我的第一篇博客
- L3-001. 凑零钱(dfs + 各种剪枝)
- 推荐15个很优秀的跨平台的移动开发工具
- C语言动态内存分配:(二)malloc/calloc/realloc/aligned_malloc
- 页面支持简体和繁体语言的js库
- WebView网络加载
- Java的string类为什么是不可变的
- 结构型之外观模式
- 《JAVA与模式》之代理模式
- 深入浅出JMS,ActiveMQ