24h購物| | PChome| 登入
2012-09-20 09:05:28| 人氣3,578| 回應0 | 上一篇 | 下一篇

[UVA][dp 0/1背包] 12455 - Bars

推薦 0 收藏 0 轉貼0 訂閱站台

 B: Bars 

Background

Some things grow if you put them together.

The Problem

We have some metallic bars, theirs length known, and, if necessary, we want to solder some of them in order to obtain another one being exactly a given length long. No bar can be cut up. Is it possible?

The Input

The first line of the input contains an integer, t, 0 ≤ t ≤ 50, indicating the number of test cases. For each test case, three lines appear, the first one contains a number n, 0 ≤ n ≤ 1000, representing the length of the bar we want to obtain. The second line contains a number p, 1 ≤ p ≤ 20, representing the number of bars we have. The third line of each test case contains p numbers, representing the length of the p bars.

The Output

For each test case the output should contain a single line, consists of the string YES or the string NO, depending on whether solution is possible or not.

Sample Input

4
25
4
10 12 5 7
925
10
45 15 120 500 235 58 6 12 175 70
120
5
25 25 25 25 25
0
2
13 567

Sample Output

NO
YES
NO
YES


#include <stdio.h>

int main() {
int t, w, n, i, j, p;
scanf("%d", &t);
while(t--) {
scanf("%d %d", &w, &n);
int dp[1005] = {};
dp[0] = 1;
for(i = 0; i < n; i++) {
scanf("%d", &p);
for(j = w-p; j >= 0; j--) {
if(dp[j] && !dp[j+p])
dp[j+p] = 1;
}
}
if(dp[w])
puts("YES");
else
puts("NO");
}
return 0;
}

台長: Morris
人氣(3,578) | 回應(0)| 推薦 (0)| 收藏 (0)| 轉寄
全站分類: 不分類 | 個人分類: UVA |
此分類下一篇:[UVA][類似SCC] 12442 - Forwarding Emails
此分類上一篇:[UVA] 12414 - Calculating Yuan Fen

是 (若未登入"個人新聞台帳號"則看不到回覆唷!)
* 請輸入識別碼:
請輸入圖片中算式的結果(可能為0) 
(有*為必填)
TOP
詳全文