Sunday, October 13, 2013

We n rods lengths l1, ..., ln (natural numbers) and prices c1, ..., cn (real numbers) that can not


We n rods lengths l1, ..., ln (natural numbers) and prices c1, ..., cn (real numbers) that can not be cut. Be welded one of them to obtain a rod of total length L (integer). Were asked to write dynamic programming algorithms and calculate retro kitchen accessories uk its complexities, to: Get desired blade minimizing the number of rods used. Get desired blade minimizing the total price of the rods used and return the rods that make up the solution.
This exercise is a case of minimizing, rather than maximizing the backpack, each row having a single element to add. Thus the problem itself is easy, if we make the backpack, you just have to adapt first to minimize the number of rods used and secondly another function to minimize the cost instead of the number retro kitchen accessories uk of rods. const int I = int.MaxValue; / / Infinite static int Min (int a, int b) {return (b <a)? b: a;} static int [,] CalcMinLon (int [] L, int N, int T) {int MaxElem = N + 1, int MaxCant = T + 1, int [,] table = new int [MaxElem, MaxCant ] for (int i = 0; i <MaxElem, i + +) {table [i, 0] = 0;} for (int i = 1; i <MaxCant, i + +) {table [0, i] = I;} int a, b, for (int i = 1; i <MaxElem, i + +) {for (int j = 1, j <MaxCant j + +) {if (L [i - 1] <= j) {a = table [ i - 1, j], b = table [i - 1, j - L [i - 1]] if (b! = I) + b = 1, table [i, j] = Min (a, b) ;} else {table [i, j] = table [i - 1, j];}}} return table;} static int [] TransMinLon (int [] L, int N, int T, int [,] table) {int MaxElem = N + 1, int MaxCant = T + 1; List <int> <int> r = new List (); Show (table MaxElem, MaxCant) int i = MaxElem - 1; int j = MaxCant - 1; while (i> 0 && j> 0) {if (board [i, j] == table [i - 1, j]) {i -;} else if (j> = L [i - 1] && table [i - 1, j - L [i - 1]] == table [i, j] - 1) {r.Add (i - 1), j - = L [i - 1]; i - ;} else if (j <L [i - 1]) {j = 0;}} return r.ToArray ();} static void Resolver1 (int [] L, int N, int T) {int [,] table = CalcMinLon (L, N, T); int [] r = TransMinLon (L, N, T, table) Console.WriteLine ("L" + L.Select (x => x.ToString ()). Aggregate ((x, x) => x + "" + xs)) Console.Write ("List") for (int i = 0; i <r.Length, i + +) {Console.Write (L [r [i]] + "");} Console.WriteLine ("\ n");}
We first made the case of minimizing the number of rods used to construct the final solution. As you can see is quite simple, when we go to add the object retro kitchen accessories uk we add one more unit to the solution and so forward. The funny thing is that if adding the element retro kitchen accessories uk we have not managed to cover the final length, there is no solution, which will be filled with infinite triangular area at the top, thereby indicating that no solution with that number of elements for that size. static int [,] CalcMinCos (int [] L, int [] C, int N, int T) {int MaxElem = N + 1, int MaxCant = T + 1, int [,] table = new int [MaxElem, MaxCant retro kitchen accessories uk ] for (int i = 0; i <MaxElem, i + +) {table [i, 0] = 0;} for (int i = 1; i <MaxCant, i + +) {table [0, i] = I;} int a, b, for (int i = 1; i <MaxElem, i + +) {for (int j = 1, j <MaxCant j + +) {if (L [i - 1] <= j) {a = table [ i - 1, j], b = table [i - 1, j - L [i - 1]] if (b! = I) = b + C [i - 1], table [i, j] = Min (a, b);} else {table [i, j] = table [i - 1, j];}}} return table;} static int [] TransMinCos (int [] L, int [] C, int N , T int, int [,] board) {int MaxElem = N + 1, int MaxCant = T + 1; List <int> <int> r = new List (); Show (table MaxElem, MaxCant) int i = MaxElem - 1; int j = MaxCant - 1; while (i> 0 && j> 0) {if (board [i, j] == table [i - 1, j]) {i -;} else if (j> = L [i - 1] && table [i - 1, j - L [i - 1]] == table [i, j] - F [i - 1]) {r.Add (i - 1 ) j - = L [i - 1]; i -;} else if (j <L [i - 1]) {j = 0;}} return r.ToArray ();} static retro kitchen accessories uk void Resolver2 (int [ ] L, int [] C, int N, int T) {int [,] table = CalcMinCos (L, C, N, T); int [] r = TransMinCos (L, C, N, T, table); Console.WriteLine ("L" + L.Select (x => x.ToString ()). Aggregate ((x, x) => x + "" + xs)) Console.WriteLine ("C:" + C.Select (x => x.ToString ()). Aggregate ((x, x) => x + "" + xs)) Console.Write ("List") for (int i = 0; i <r.Length, i + +) {Console.Write (L [r [i]] + "");} Console.WriteLine ("\ n");}
Second, practically copy and paste the same algorithm as in the first point, but this time we do not increase the value by 1 unit, but with the current cost of the item. It's the same pattern, only now we are minimizing else than the first point, but in essence is to follow the same method to solve the problem. public static void Resolver () {int [] L = {1, 1, 2, 2, 3, 3, 4, 5, 6, 7}, int [] C = {10, 10, 22, 22, 34, 34, 46, 58, 70, 82}; Resolver1 (L, L.

No comments:

Post a Comment