uva10310(SuperSale)
来源:互联网 发布:女装免费加盟开淘宝店 编辑:程序博客网 时间:2024/06/06 15:48
思路:题目的意思就是有n种物品,m个人,每种物品有对应的价值和重量,数量无限,,但是每种物品每个人只能拿一次,然后每个人有一个最高承重值,问最后这n个人最多可以带走的价值和事多少;
显然对于个人而言,别人拿多少对自己是没有影响的,所以就只讨论每个人的情况,最后加起来就好了。
对于每个人来说就是一个01背包问题,与处理下就好了。
// #pragma comment(linker, "/STACK:1024000000,1024000000")#include <iostream>#include <algorithm>#include <iomanip>#include <sstream>#include <string>#include <stack>#include <queue>#include <deque>#include <vector>#include <map>#include <set>#include <stdio.h>#include <string.h>#include <math.h>#include <stdlib.h>#include <limits.h>using namespace std;typedef long long LL;typedef unsigned long long ULL;typedef pair<int,int> ii;const int inf = 1 << 30;const int INF = 0x3f3f3f3f;const int MOD = 1e9 + 7;const int maxn = 1001;const int maxm = 1e5 + 10;int p[maxn], w[maxn], dp[maxm];int n,m;int ans;void solve(){for (int i=0;i<n;i++){for (int j=ans;j>=w[i];j--)if (dp[j] < dp[j - w[i]] + p[i])dp[j] = dp[j - w[i]] + p[i];}}int main(){// freopen("in.txt","r",stdin);// freopen("out.txt","w",stdout);int t;scanf("%d",&t);while(t--){scanf("%d",&n);memset(dp, 0,sizeof dp);ans = 0;for (int i=0;i<n;i++){scanf("%d%d",&p[i],&w[i]);ans += w[i];}solve();scanf("%d",&m);int sum = 0;while(m--){int x;scanf("%d",&x);sum += *max_element(dp,dp + x + 1);}printf("%d\n",sum);}return 0;}
0 0
- uva10310(SuperSale)
- uva10310
- 10130SuperSale
- 10130 - SuperSale
- 10130 - SuperSale
- SuperSale UVA
- uva 10130 SuperSale
- UVA 10130 SuperSale
- Uva 10130 - SuperSale
- UVa 10130 - SuperSale
- UVa 10130 - SuperSale
- UVa 10130 - SuperSale 背包
- uva 10130 - SuperSale
- UVa 10130 - SuperSale
- UVa 10130 - SuperSale
- Uva-10130-SuperSale
- UVa:10130 SuperSale
- UVA - 10130 SuperSale
- go语音之select
- android自定义theme
- 用 goto + if 模拟 循环
- eclipse集成Android NDK说明
- MySQL的configure参数详解
- uva10310(SuperSale)
- Linux文件解压缩
- HDU 1298 T9 // 字典树,枚举,dfs
- 使用NSURLCache让本地数据来代替远程UIWebView请求
- Linux文件操作
- iPhone开发【二十三】常用IOS开源组件【第1篇】
- java泛型
- 利用jsoup抓取指定网页的照片【也可以修改一下抓取其他内容】
- C++ 类访问控制(public/protected/private)