1024 ProblemX
来源:互联网 发布:淘宝全屏海价格 编辑:程序博客网 时间:2024/06/07 02:36
题意:
给出n件物品,手里的总钱数m,每件物品的价值p,最低要有多少钱才能买q,购买的价格v 求最大价值。
思路:
这是一个背包问题,问题的关键就在于按什么顺序排序也就是先买哪个后买那个。
首先设有两个物品
p1,q1,v1—-A物品
p2,q2,v2—-B物品
如果先买A后买B,手里的钱至少需要q1+p2,如果先买B后买A 手里需要q2+p1,
假设先买A的代价下 那么就是 q1+p2
// ConsoleApplication32.cpp : 定义控制台应用程序的入口点。//#include "stdafx.h"#include<iostream>#include<fstream>#include <stdio.h>#include <string.h>#include <algorithm>using namespace std;struct node{ int p, q, v;} a[555];int cmp(node x, node y){ return x.q - x.p<y.q - y.p;}int main(){ fstream cin("E:/C++/IN/aaa.txt"); int n, m, i, j; int dp[5555]; while (cin>>n>>m&&n&&m) { for (i = 0; i < n; i++) cin >> a[i].p >> a[i].q >> a[i].v; memset(dp, 0, sizeof(dp)); sort(a, a + n, cmp); for (i = 0; i<n; i++) { for (j = m; j >= a[i].q; j--) { dp[j] = max(dp[j], dp[j - a[i].p] + a[i].v); } } cout << dp[m] << endl; } return 0;}
0 0
- 1024 ProblemX
- 1024
- 1024
- 1024
- 1024
- 1024
- 1024
- 1024
- 1024
- 1024
- 1024
- 1024
- 1024
- 1024
- 1024
- 1024
- 1024!
- 1024
- web跨域问题的解决
- PassCode - 打造一个简单易用的安全锁类库
- 我常用的linux命令
- Android:Ubuntu 16.04上编译Android主线最新代码
- LINQ教程
- 1024 ProblemX
- matplotlib使用show方法后不能显示图像的问题
- 顺序表应用2:多余元素删除之建表算法
- android design library提供的TabLayout的用法
- 《如何阅读一本书》笔记
- SVN冲突解决
- swift学习之路(二)整型
- C# MATLAB混编(二)
- 优化安卓应用内存的神秘方法以及背后的原理,一般人我不告诉他