码迷,mamicode.com
首页 > 其他好文 > 详细

USACO money packageDP

时间:2014-07-01 09:29:13      阅读:209      评论:0      收藏:0      [点我收藏+]

标签:blog   2014   os   cti   for   c++   

裸0/1背包,就是从各种币种里面拿来凑足N元,求最多有多种方案。用dp[i][j]表示选前i个币种凑成j的方案数量

状态转移方程: dp[i][j] = dp[i- 1][j]    j < coins[i]      dp[i][j] = dp[i-1][j] + dp[i-1][j-coins[i]]  j >= coins[i]

/*
ID:kevin_s1
PROG:money
LANG:C++
*/

#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <vector>
#include <map>
#include <set>
#include <algorithm>
#include <cstdlib>
#include <list>
#include <cmath>

using namespace std;

#define MAXN 10010


//gobal variable====
int V;
long long N;
int coins[30];
long long dp[30][MAXN];
//==================


//function==========

//==================

int main(){
	freopen("money.in","r",stdin);
	freopen("money.out","w",stdout);
	cin>>V>>N;
	int coin;
	for(int i = 1; i <= V; i++){
		cin>>coin;
		coins[i] = coin;
	}
	for(int i = 1; i <= V; i++)
		dp[0][i] = 0;
	dp[0][0] = 1;
	for(int i = 1; i <= V; i++){
		for(long long j = 0; j < coins[i]; j++){
			dp[i][j] = dp[i - 1][j];
		}
		for(long long j = coins[i]; j <= N; j++){
			dp[i][j] = dp[i - 1][j] + dp[i][j - coins[i]];
		}
	}
	cout<<dp[V][N]<<endl;
	return 0;
}

USACO money packageDP,布布扣,bubuko.com

USACO money packageDP

标签:blog   2014   os   cti   for   c++   

原文地址:http://blog.csdn.net/kevin_samuel/article/details/36090451

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!