本文主要是介绍woj 1005 Holding Animals,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
一个简单的背包问题。dp
/** Author: stormdpzh* Created Time: 2012/7/11 21:23:48* File Name: woj_1005.cpp*/
#include <iostream>
#include <cstdio>
#include <sstream>
#include <cstring>
#include <string>
#include <cmath>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <algorithm>
#include <functional>#define sz(v) ((int)(v).size())
#define rep(i, n) for(int i = 0; i < n; i++)
#define repf(i, a, b) for(int i = a; i <= b; i++)
#define repd(i, a, b) for(int i = a; i >= b; i--)
#define out(n) printf("%d\n", n)
#define mset(a, b) memset(a, b, sizeof(a))
#define wh(n) while(1 == scanf("%d", &n))
#define whz(n) while(1 == scanf("%d", &n) && n != 0)
#define lint long longusing namespace std;const int MaxN = 105;
const int MaxS = 100005;int s[MaxN], p[MaxN];
int dp[MaxS];
int n, ts;void init()
{mset(dp, 0);rep(i, n) scanf("%d%d", &s[i], &p[i]);scanf("%d", &ts);
}int gao()
{rep(i, n) repd(v, ts, 0) {if(v - s[i] >= 0)dp[v] = max(dp[v], dp[v - s[i]] + p[i]);}return dp[ts];
}int main()
{wh(n) {init();out(gao());}return 0;
}
这篇关于woj 1005 Holding Animals的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!