HDU 1712 - ACboy needs your help


http://acm.hdu.edu.cn/showproblem.php?pid=1712
ACboy has N courses this term, and he plans to spend at most M days on study.Of course,the profit he will gain from different course depending on the days he spend on it.How to arrange the M days for the N courses to maximize the profit?

Input
The input consists of multiple data sets. A data set starts with a line containing two positive integers N and M, N is the number of courses, M is the days ACboy has.
Next follow a matrix A[i][j], (1<=i<=N<=100,1<=j<=M<=100).A[i][j] indicates if ACboy spend j days on ith course he will get profit of value A[i][j].
N = 0 and M = 0 ends the input.

Output
For each data set, your program should output a line which contains the number of the max profit ACboy will gain.

Sample Input
2 2 1 2 1 3 2 2 2 1 2 1 2 3 3 2 1 3 2 1 0 0

Sample Output
3 4 6
http://www.cppblog.com/Onway/articles/122695.html
可以将每一门课看成一个分组,每门课不同天数的选择看成是分组的物品(显然只能有一个选择),物品的费用即为花费的天数,物品的价值为题中给出的收获。该题中背包容量最大为M。
设dp[x]为前i组物品,在背包容量为x(即费用为x)时的最大价值。则将i从1到N进行过历遍后(第一重循环),dp[m]即为所求。
在这种状态设置中,容易想出以下两种阶段递推方式(以下所述都为第二和第三重循环):
1,在同一个背包容量中,对不同费用的物品进行枚举比较:
for(j=MAX;j>=1;--j)   //背包容量
      for(k=1;k<=m;++k)  //不同费用的物品
2,在同一费用的物品中,对放在不同背包容量时计算最大价值:(该方式同《背包九讲-分组背包》中的伪代码部分)
for(k=1;k<=m;++k)    //不同费用的物品


      for(j=MAX;j>=1;--j)  //背包容量
1,分析第一种递推方式的正确:
该方式即求在容量为j的背包中,选择哪一个物品可以有最大价值。
看递推方程:dp[j]=max(dp[j],dp[j-c[k]]+w[k]);(其中c[k]为k物品的费用,w[k]为价值),由于递降枚举背包容量,max比较中的dp[j]是由上一组物品决策所得,在这里将被忽略。因为就算不忽略,在本组物品中dp[j]的决策依然要取决于dp[j-c[k]]+w[k]。
而同样由于递降枚举背包容量(第二重循环),dp[j-c[k]]在本组物品中是未进行过决策的,亦即背包容量为j-c[k]时,在本组物品中是没有选择任何物品的,这可以保证对dp[j]决策时,不会多选本组中的物品。
2,分析第二种递推方式的错误:
该方式即求对物品k,放在所有背包中,计算各个最大价值。
同样是递推方程:dp[j]=max(dp[j],dp[j-c[k]]+w[k]);(其中c[k]为k物品的费用,w[k]为价值)。能否保证dp[j-c[k]]在本组中未经决策,就成了该递推方式对错的关键。
由于背包容量的递降枚举在第三重循环,只能保证k物品不会重复选择。对于另一k0物品,当背包容量枚举到j-c[k]的时候,由方程可以有:dp[j-c[k]]=max(dp[j-c[k]],dp[j-c[k]-c[k0]]+w[k0],亦即dp[j-c[k]]可能在本组中的其他物品中进行过决策。


那么这样就可能导致在一组物品中选择了多件物品。
https://blog.csdn.net/loveyou11111111/article/details/50674854
这个问题变成了每组物品有若干种策略:是选择本组的某一件,还是一件都不选 。 也就是说设 f[k][v]表示前 k 组物品花费费用 v 能取得的最大权值,则有:
f[k][v]=max{f[k-1][v],f[k-1][v-c[i]]+w[i]|物品 i 属于组 k}
使用一维数组的伪代码如下:
for 所有的组 k
for v=V..0
for 所有的 i 属于组 k
f[v]=max{f[v],f[v-c[i]]+w[i]}

        for(int i=1;i<=n;i++)
        {
            for(int j=m;j>=1;j--)
            {
                for(int k=1;k<=m;k++)
                    if(j-k>=0)
                        dp[i][j]=max(max(dp[i][j],dp[i-1][j]),dp[i-1][j-k]+a[i][k]); //一维就可以,很好改,我就不改了
            }
        }
        printf("%d\n",dp[n][m]);
    }
https://blog.csdn.net/u013480600/article/details/40649797
       所以我们换一种问题描述方式: 有n组物品, 每组物品有m个且每组物品中最多只能选1个物品. 第i组物品的花费分别为1 2 3 …m, 第i组物品的价值分别为val[i][1], val[i][2]…val[i][m]. 现在问你初始金钱为m时, 通过上面n组物品, 最多能获得多少价值的物品?

       上面问题就是一个明显的分组背包问题了. 我们令dp[i][j]==x表示只选前i组物品且总花费<=j时, 能获得的最大价值为x.

       初始化: dp全为0.

       状态转移: dp[i][j] == max( dp[i-1][j] , dp[i-1][j-cost[k]]+val[k])

       其中cost[k]和val[k]指的是第i组物品的第k个物品的花费和价值.

       上面公式前者表示第i组物品一个都不选, 后者表示第i组物品选1个.

       最终所求: dp[n][m]的值.

注意: dp递推的3层循环的相互顺序不能改变, 否者会错.(可以自己想一想为什么是这样的循环顺序).程序用的滚动数组, dp只有一维.


    while(scanf("%d%d",&n,&m)==2 && n)
    {
        //读取输入
        for(int i=1;i<=n;i++)
        for(int j=1;j<=m;j++)
        {
            scanf("%d",&val[i][j]);
            cost[i][j]=j;
        }

        //递推过程
        memset(dp,0,sizeof(dp));
        for(int i=1;i<=n;i++)//第i组
        for(int j=m;j>=0;j--)//<=j花费时
        for(int k=1;k<=m;k++)//第i组的第k个物品
        {
            if(j>=cost[i][k])
                dp[j] = max(dp[j], dp[j-cost[i][k]]+val[i][k]);
        }

https://www.cnblogs.com/00isok/p/9379331.html
这是一个很明显的分组背包问题,将某一门课程花m个不同天数能够得到不同的价值看成是m个有各自花费和价值的物品,然后,又因为根据题意,每一门课程都只能选择一种花费的天数,于是,这道题就被很自然的转化为分组背包问题。
        for (int i = 1; i <= n; i++)
        {
            for (int j = 1; j <= m; j++) {
                scanf("%d", &arr[i][j]);
                c[i][j] = j;     //记录要花费的天数( 即背包中的体积 )
            }
        }

        int dp[110];
        memset(dp, 0, sizeof(dp));

        for (int k = 1; k <= n; k++)             //第k门课程       (对应分组背包中的组序号)
        {
            for (int v = m; v >= 0; v--)         //总共所花的天数  (对应背包的容量)由于此题每组物品只能取一次,所以逆序
            {
                for (int i = 1; i <= m; i++)     //第k门课中序号为i的物品    
                {
                    if (v - i < 0)continue;
                    dp[v] = max(dp[v], dp[v - c[k][i]] + arr[k][i]);
                }
            }
        }      //dp[i][j]为,前i组中花费天数为v时,所能得到的最大价值

https://blog.csdn.net/u012860063/article/details/34107953
题意:一开始输入n和m,n代表有n门课,m代表你有m天,
然后给你一个数组,val[i][j],代表第i门课,在通过j天去修,
会得到的分数。求在m天能得到的最大分数。
while(~scanf("%d%d",&n,&m)&&( n || m))
{
memset(dp,0,sizeof(dp));
for(i = 1; i <=n ; i++)
{
for(j = 1; j <= m ;j++)
scanf("%d",&a[i][j]);
}
for(i = 1 ; i <= n ; i++)//第一重循环:分组数
{
for(j = m ; j >= 1 ; j--) //第二重循环:容量体积
{
for(k = 1 ; k <= j ; k++) //第三重循环:属于i组的k
{
dp[j]=max(dp[j],dp[j-k]+a[i][k]);
}
}
}
printf("%d\n",dp[m]);
}

http://blog.sina.com.cn/s/blog_79b832820100rfpc.html
    for (i=0;i<n;i++)
        for (j=m;j>=1;j--)     //第二重循环和第三重循环不能交换,否则变成了一个课程可以用不同天多次完成。
            for (k=1;k<=j;k++)
                f[j]=max(f[j],f[j-k]+v[i][k]);

    return f[m];


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