金明的预算方案


https://vijos.org/p/1313
金明今天很开心,家里购置的新房就要领钥匙了,新房里有一间金明自己专用的很宽敞的房间。更让他高兴的是,妈妈昨天对他说:“你的房间需要购买哪些物品,怎么布置,你说了算,只要不超过N元钱就行”。今天一早,金明就开始做预算了,他把想买的物品分为两类:主件与附件,附件是从属于某个主件的,下表就是一些主件与附件的例子:
主件 附件
电脑 打印机,扫描仪
书柜 图书
书桌 台灯,文具
工作椅 无
如果要买归类为附件的物品,必须先买该附件所属的主件。每个主件可以有0个、1个或2个附件。附件不再有从属于自己的附件。金明想买的东西很多,肯定会超过妈妈限定的N元。于是,他把每件物品规定了一个重要度,分为5等:用整数1~5表示,第5等最重要。他还从因特网上查到了每件物品的价格(都是10元的整数倍)。他希望在不超过N元(可以等于N元)的前提下,使每件物品的价格与重要度的乘积的总和最大。
设第j件物品的价格为v[j],重要度为w[j],共选中了k件物品,编号依次为j1,j2,……,jk,则所求的总和为:v[j1]*w[j1]+v[j2]*w[j2]+ …+v[jk]*w[jk]。(其中*为乘号)请你帮助金明设计一个满足要求的购物单。

格式

输入格式

输入文件的第1行,为两个正整数,用一个空格隔开:
N m 
其中N(<32000)表示总钱数,m(<60)为希望购买物品的个数。)
从第2行到第m\+1行,第j行给出了编号为j\-1的物品的基本数据,每行有3个非负整数
v p q
(其中v表示该物品的价格(v<10000),p表示该物品的重要度(1~5),q表示该物品是主件还是附件。如果q=0,表示该物品为主件,如果q>0,表示该物品为附件,q是所属主件的编号)

输出格式

输出文件只有一个正整数,为不超过总钱数的物品的价格与重要度乘积的总和的最大值
(<200000)。

样例1

样例输入1

1000 5
800 2 0
400 5 1
300 5 1
400 3 0
500 2 0

样例输出1

2200

限制

1s
考虑到每个主件最多只有两个附件,因此我们可以通过转化,把原问题转化为01背包问题来解决,在用01背包之前我们需要对输入数据进行处理,把每一种物品归类,即:把每一个主件和它的附件看作一类物品。处理好之后,我们就可以使用01背包算法了。在取某件物品时,我们只需要从以下四种方案中取最大的那种方案:只取主件、取主件+附件1、取主件+附件2、既主件+附件1+附件2。很容易得到如下状态转移方程:

f[i,j]=max{f[i-1,j],

f[i-1,j-a[i,0]]+a[i,0]*b[i,0],

f[i-1,j-a[i,0]-a[i,1]]+a[i,0]*b[i,0]+a[i,1]*b[i,1],

f[i-1,j-a[i,0]-a[i,2]]+a[i,0]*b[i,0]+a[i,2]*b[i,2],

f[i-1,j-a[i,0]-a[i,1]-a[i,2]]+a[i,0]*b[i,0]+a[i,1]*b[i,1]+a[i,2]*b[i,2]}

其中,f[i,j]表示用j元钱,买前i类物品,所得的最大值,a[i,0]表示第i类物品主件的价格,a[i,1]表示第i类物品第1个附件的价格,a[i,2]表示第i类物品第2个附件的价格,b[i,0],b[i,1],b[i,2]分别表示主件、第1个附件和第2个附件的重要度。

for(i=1;i<=m;i++)
{
cin>>c>>p>>q;
c/=10; //同上 
if(q==0) {w[i][q]=c; v[i][q]=c*p;}
else if(w[q][1]==0) {w[q][1]=c;v[q][1]=c*p;}
else {w[q][2]=c;v[q][2]=c*p;}
}
for(i=1;i<=m;i++)
for(j=0;j<=n;j++)
{
d[i][j]=d[i-1][j];
if(j>=w[i][0]) {t=d[i-1][j-w[i][0]]+v[i][0];if(t>d[i][j]) d[i][j]=t;}
if(j>=w[i][0]+w[i][1]) {t=d[i-1][j-w[i][0]-w[i][1]]+v[i][0]+v[i][1];if(t>d[i][j]) d[i][j]=t;}
if(j>=w[i][0]+w[i][2]) {t=d[i-1][j-w[i][0]-w[i][2]]+v[i][0]+v[i][2];if(t>d[i][j]) d[i][j]=t;}
if(j>=w[i][0]+w[i][1]+w[i][2]) {t=d[i-1][j-w[i][0]-w[i][1]-w[i][2]]+v[i][0]+v[i][1]+v[i][2];if(t>d[i][j]) d[i][j]=t;}
}
好吧就是一维四状态的背包
    scanf("%d%d",&n,&m);
    /*q,v:如是主件则存在这里
    q1,v1:如是附件一存在这里
    q2,v2:如是附件二则存在这里*/
    memset(f,-1,sizeof(f));
    memset(q,0,sizeof(q));
    memset(q1,0,sizeof(q1));
    memset(q2,0,sizeof(q2));
    memset(v,0,sizeof(v));
    memset(v1,0,sizeof(v1));
    memset(v2,0,sizeof(v2));
    //请自动忽略以上的的OVO

    f[0]=0;
    for (int i=1;i<=m;i++)
    {
        int a,b,c;
        scanf("%d%d%d",&a,&b,&c);
        if (c==0)
        {
            v[i]=a;q[i]=b;  //存为主件  
        }    
        else 
        {
            if (q1[c]==0) {q1[c]=b;v1[c]=a;}
            else {q2[c]=b;v2[c]=a;}
            //存为附件一或附件二
        }
    }    
    for (int i=1;i<=m;i++)
    {
        for (int j=n;j>=v[i];j--)
        {
            //if(f[j]!=-1)
            {
                if (j-v[i]>=0) f[j]=mymax(f[j],f[j-v[i]]+v[i]*q[i]);//只买一个主件
                if (j-v[i]-v1[i]>=0) f[j]=mymax(f[j],f[j-v[i]-v1[i]]+v[i]*q[i]+v1[i]*q1[i]);//买主件和附件一
                if (j-v[i]-v2[i]>=0) f[j]=mymax(f[j],f[j-v[i]-v2[i]]+v[i]*q[i]+v2[i]*q2[i]);//买主件和附件二
                if (j-v[i]-v1[i]-v2[i]>=0) f[j]=mymax(f[j],f[j-v[i]-v1[i]-v2[i]]+v[i]*q[i]+v1[i]*q1[i]+v2[i]*q2[i]);//买主件和两个附件
            }
        }
    }
    int ans=0;
    for (int j=1;j<=n;j++)
    {
        if (f[j]>ans) ans=f[j];
        //不一定用最多钱的就是最优的,扫一遍最大值
    }

方便起见,我们把状态只定义在主件上(对输入进行特殊处理) 
每个主件有0,1,2个附件 
每次购买时,有五种选择 
1.不买 
2.只买主件 
3.买主件和一号附件 
4.买主件和二号附件 
5.买主件和全部附件

[i][0]表示主件的价值与花费 
[i][1]表示一号附件的价值与花费 
[i][2]表示二号附件的价值与花费 
当一个物品没有附件时,[i][1]和[i][2]均为0

状态转移方程(只是一个思路方程,不完整!没有写乘上期望值什么的,具体方程请往下看代码) 
f(i,j) = max ( 
f(i-1,j),//不买 
f(i-1,j - c[i][0]) + v[i][0],//只买主件 
f(i-1,j - c[i][0] - c[i][1]) + v[i][0] + v[i][1],//主件和一号附件 
f(i-1,j - c[i][0] - c[i][2]) + v[i][0] + v[i][2],//主件和二号附件 
f(i-1,j - c[i][0] - c[i][1] - c[i][2]) + v[i][0] + v[i][1] + v[i][2],//主件和全部附件 
)

    for(int i=1; i<=m; i++) {
        int w, p, q;
        scanf("%d %d %d",&w,&p,&q);
        if(q == 0) {
            c[i][0] = w;
            v[i][0] = p;
            continue;

        }
        if(!v[q][1]) { // 如果第一个附件还没有遇到 
            c[q][1] = w;
            v[q][1] = p;
        }
        else { //如果已经遇到第一个附件 
            c[q][2] = w;
            v[q][2] = p;
        }
    }
    for(int i=1; i<=m; i++) {

        for(int j=10; j<=n; j+=10){

            if(j-c[i][0]>= 0) {

                f[i][j] = std::max(f[i-1][j],f[i-1][j-c[i][0]] + v[i][0]*c[i][0]);

            if(j - c[i][0] - c[i][1] >= 0)

                f[i][j] = std::max(f[i][j],f[i-1][j-c[i][0]-c[i][1]] + v[i][1]*c[i][1] + v[i][0]*c[i][0]);  

            if(j - c[i][0] - c[i][2]>= 0)

                f[i][j] = std::max(f[i][j],f[i-1][j-c[i][0]-c[i][2]] + v[i][2]*c[i][2] + v[i][0]*c[i][0]);

            if(j-c[i][0] - c[i][1] - c[i][2] >= 0)

                f[i][j] = std::max(f[i][j],f[i-1][j-c[i][0]-c[i][1]-c[i][2]] + v[i][2]*c[i][2] + v[i][1]*c[i][1] + v[i][0]*c[i][0]);

            }
            else
                f[i][j] = f[i-1][j];
        }
    }
    printf("%d",f[m][n]);


Labels

LeetCode (1432) GeeksforGeeks (1122) LeetCode - Review (1067) Review (882) Algorithm (668) to-do (609) Classic Algorithm (270) Google Interview (237) Classic Interview (222) Dynamic Programming (220) DP (186) Bit Algorithms (145) POJ (141) Math (137) Tree (132) LeetCode - Phone (129) EPI (122) Cracking Coding Interview (119) DFS (115) Difficult Algorithm (115) Lintcode (115) Different Solutions (110) Smart Algorithm (104) Binary Search (96) BFS (91) HackerRank (90) Binary Tree (86) Hard (79) Two Pointers (78) Stack (76) Company-Facebook (75) BST (72) Graph Algorithm (72) Time Complexity (69) Greedy Algorithm (68) Interval (63) Company - Google (62) Geometry Algorithm (61) Interview Corner (61) LeetCode - Extended (61) Union-Find (60) Trie (58) Advanced Data Structure (56) List (56) Priority Queue (53) Codility (52) ComProGuide (50) LeetCode Hard (50) Matrix (50) Bisection (48) Segment Tree (48) Sliding Window (48) USACO (46) Space Optimization (45) Company-Airbnb (41) Greedy (41) Mathematical Algorithm (41) Tree - Post-Order (41) ACM-ICPC (40) Algorithm Interview (40) Data Structure Design (40) Graph (40) Backtracking (39) Data Structure (39) Jobdu (39) Random (39) Codeforces (38) Knapsack (38) LeetCode - DP (38) Recursive Algorithm (38) String Algorithm (38) TopCoder (38) Sort (37) Introduction to Algorithms (36) Pre-Sort (36) Beauty of Programming (35) Must Known (34) Binary Search Tree (33) Follow Up (33) prismoskills (33) Palindrome (32) Permutation (31) Array (30) Google Code Jam (30) HDU (30) Array O(N) (29) Logic Thinking (29) Monotonic Stack (29) Puzzles (29) Code - Detail (27) Company-Zenefits (27) Microsoft 100 - July (27) Queue (27) Binary Indexed Trees (26) TreeMap (26) to-do-must (26) 1point3acres (25) GeeksQuiz (25) Merge Sort (25) Reverse Thinking (25) hihocoder (25) Company - LinkedIn (24) Hash (24) High Frequency (24) Summary (24) Divide and Conquer (23) Proof (23) Game Theory (22) Topological Sort (22) Lintcode - Review (21) Tree - Modification (21) Algorithm Game (20) CareerCup (20) Company - Twitter (20) DFS + Review (20) DP - Relation (20) Brain Teaser (19) DP - Tree (19) Left and Right Array (19) O(N) (19) Sweep Line (19) UVA (19) DP - Bit Masking (18) LeetCode - Thinking (18) KMP (17) LeetCode - TODO (17) Probabilities (17) Simulation (17) String Search (17) Codercareer (16) Company-Uber (16) Iterator (16) Number (16) O(1) Space (16) Shortest Path (16) itint5 (16) DFS+Cache (15) Dijkstra (15) Euclidean GCD (15) Heap (15) LeetCode - Hard (15) Majority (15) Number Theory (15) Rolling Hash (15) Tree Traversal (15) Brute Force (14) Bucket Sort (14) DP - Knapsack (14) DP - Probability (14) Difficult (14) Fast Power Algorithm (14) Pattern (14) Prefix Sum (14) TreeSet (14) Algorithm Videos (13) Amazon Interview (13) Basic Algorithm (13) Codechef (13) Combination (13) Computational Geometry (13) DP - Digit (13) LCA (13) LeetCode - DFS (13) Linked List (13) Long Increasing Sequence(LIS) (13) Math-Divisible (13) Reservoir Sampling (13) mitbbs (13) Algorithm - How To (12) Company - Microsoft (12) DP - Interval (12) DP - Multiple Relation (12) DP - Relation Optimization (12) LeetCode - Classic (12) Level Order Traversal (12) Prime (12) Pruning (12) Reconstruct Tree (12) Thinking (12) X Sum (12) AOJ (11) Bit Mask (11) Company-Snapchat (11) DP - Space Optimization (11) Dequeue (11) Graph DFS (11) MinMax (11) Miscs (11) Princeton (11) Quick Sort (11) Stack - Tree (11) 尺取法 (11) 挑战程序设计竞赛 (11) Coin Change (10) DFS+Backtracking (10) Facebook Hacker Cup (10) Fast Slow Pointers (10) HackerRank Easy (10) Interval Tree (10) Limited Range (10) Matrix - Traverse (10) Monotone Queue (10) SPOJ (10) Starting Point (10) States (10) Stock (10) Theory (10) Tutorialhorizon (10) Kadane - Extended (9) Mathblog (9) Max-Min Flow (9) Maze (9) Median (9) O(32N) (9) Quick Select (9) Stack Overflow (9) System Design (9) Tree - Conversion (9) Use XOR (9) Book Notes (8) Company-Amazon (8) DFS+BFS (8) DP - States (8) Expression (8) Longest Common Subsequence(LCS) (8) One Pass (8) Quadtrees (8) Traversal Once (8) Trie - Suffix (8) 穷竭搜索 (8) Algorithm Problem List (7) All Sub (7) Catalan Number (7) Cycle (7) DP - Cases (7) Facebook Interview (7) Fibonacci Numbers (7) Flood fill (7) Game Nim (7) Graph BFS (7) HackerRank Difficult (7) Hackerearth (7) Inversion (7) Kadane’s Algorithm (7) Manacher (7) Morris Traversal (7) Multiple Data Structures (7) Normalized Key (7) O(XN) (7) Radix Sort (7) Recursion (7) Sampling (7) Suffix Array (7) Tech-Queries (7) Tree - Serialization (7) Tree DP (7) Trie - Bit (7) 蓝桥杯 (7) Algorithm - Brain Teaser (6) BFS - Priority Queue (6) BFS - Unusual (6) Classic Data Structure Impl (6) DP - 2D (6) DP - Monotone Queue (6) DP - Unusual (6) DP-Space Optimization (6) Dutch Flag (6) How To (6) Interviewstreet (6) Knapsack - MultiplePack (6) Local MinMax (6) MST (6) Minimum Spanning Tree (6) Number - Reach (6) Parentheses (6) Pre-Sum (6) Probability (6) Programming Pearls (6) Rabin-Karp (6) Reverse (6) Scan from right (6) Schedule (6) Stream (6) Subset Sum (6) TSP (6) Xpost (6) n00tc0d3r (6) reddit (6) AI (5) Abbreviation (5) Anagram (5) Art Of Programming-July (5) Assumption (5) Bellman Ford (5) Big Data (5) Code - Solid (5) Code Kata (5) Codility-lessons (5) Coding (5) Company - WMware (5) Convex Hull (5) Crazyforcode (5) DFS - Multiple (5) DFS+DP (5) DP - Multi-Dimension (5) DP-Multiple Relation (5) Eulerian Cycle (5) Graph - Unusual (5) Graph Cycle (5) Hash Strategy (5) Immutability (5) Java (5) LogN (5) Manhattan Distance (5) Matrix Chain Multiplication (5) N Queens (5) Pre-Sort: Index (5) Quick Partition (5) Quora (5) Randomized Algorithms (5) Resources (5) Robot (5) SPFA(Shortest Path Faster Algorithm) (5) Shuffle (5) Sieve of Eratosthenes (5) Strongly Connected Components (5) Subarray Sum (5) Sudoku (5) Suffix Tree (5) Swap (5) Threaded (5) Tree - Creation (5) Warshall Floyd (5) Word Search (5) jiuzhang (5)

Popular Posts