Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
45
Task/Knapsack-problem-0-1/D/knapsack-problem-0-1-1.d
Normal file
45
Task/Knapsack-problem-0-1/D/knapsack-problem-0-1-1.d
Normal file
|
|
@ -0,0 +1,45 @@
|
|||
import std.stdio, std.algorithm, std.typecons, std.array, std.range;
|
||||
|
||||
struct Item { string name; int weight, value; }
|
||||
|
||||
Item[] knapsack01DinamicProgramming(immutable Item[] items, in int limit)
|
||||
pure nothrow @safe {
|
||||
auto tab = new int[][](items.length + 1, limit + 1);
|
||||
|
||||
foreach (immutable i, immutable it; items)
|
||||
foreach (immutable w; 1 .. limit + 1)
|
||||
tab[i + 1][w] = (it.weight > w) ? tab[i][w] :
|
||||
max(tab[i][w], tab[i][w - it.weight] + it.value);
|
||||
|
||||
typeof(return) result;
|
||||
int w = limit;
|
||||
foreach_reverse (immutable i, immutable it; items)
|
||||
if (tab[i + 1][w] != tab[i][w]) {
|
||||
w -= it.weight;
|
||||
result ~= it;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
void main() @safe {
|
||||
enum int limit = 400;
|
||||
immutable Item[] items = [
|
||||
{"apple", 39, 40}, {"banana", 27, 60},
|
||||
{"beer", 52, 10}, {"book", 30, 10},
|
||||
{"camera", 32, 30}, {"cheese", 23, 30},
|
||||
{"compass", 13, 35}, {"glucose", 15, 60},
|
||||
{"map", 9, 150}, {"note-case", 22, 80},
|
||||
{"sandwich", 50, 160}, {"socks", 4, 50},
|
||||
{"sunglasses", 7, 20}, {"suntan cream", 11, 70},
|
||||
{"t-shirt", 24, 15}, {"tin", 68, 45},
|
||||
{"towel", 18, 12}, {"trousers", 48, 10},
|
||||
{"umbrella", 73, 40}, {"water", 153, 200},
|
||||
{"waterproof overclothes", 43, 75},
|
||||
{"waterproof trousers", 42, 70}];
|
||||
|
||||
immutable bag = knapsack01DinamicProgramming(items, limit);
|
||||
writefln("Items:\n%-( %s\n%)", bag.map!q{ a.name }.retro);
|
||||
const t = reduce!q{ a[] += [b.weight, b.value] }([0, 0], bag);
|
||||
writeln("\nTotal weight and value: ", t[0] <= limit ? t : [0, 0]);
|
||||
}
|
||||
56
Task/Knapsack-problem-0-1/D/knapsack-problem-0-1-2.d
Normal file
56
Task/Knapsack-problem-0-1/D/knapsack-problem-0-1-2.d
Normal file
|
|
@ -0,0 +1,56 @@
|
|||
struct Item { string name; int weight, value; }
|
||||
|
||||
immutable Item[] items = [
|
||||
{"apple", 39, 40}, {"banana", 27, 60},
|
||||
{"beer", 52, 10}, {"book", 30, 10},
|
||||
{"camera", 32, 30}, {"cheese", 23, 30},
|
||||
{"compass", 13, 35}, {"glucose", 15, 60},
|
||||
{"map", 9, 150}, {"note-case", 22, 80},
|
||||
{"sandwich", 50, 160}, {"socks", 4, 50},
|
||||
{"sunglasses", 7, 20}, {"suntan cream", 11, 70},
|
||||
{"t-shirt", 24, 15}, {"tin", 68, 45},
|
||||
{"towel", 18, 12}, {"trousers", 48, 10},
|
||||
{"umbrella", 73, 40}, {"water", 153, 200},
|
||||
{"waterproof overclothes", 43, 75},
|
||||
{"waterproof trousers", 42, 70}];
|
||||
|
||||
struct Solution { uint bits; int value; }
|
||||
static assert(items.length <= Solution.bits.sizeof * 8);
|
||||
|
||||
void solve(in int weight, in int idx, ref Solution s)
|
||||
pure nothrow @nogc @safe {
|
||||
if (idx < 0) {
|
||||
s.bits = s.value = 0;
|
||||
return;
|
||||
}
|
||||
|
||||
if (weight < items[idx].weight) {
|
||||
solve(weight, idx - 1, s);
|
||||
return;
|
||||
}
|
||||
|
||||
Solution v1, v2;
|
||||
solve(weight, idx - 1, v1);
|
||||
solve(weight - items[idx].weight, idx - 1, v2);
|
||||
|
||||
v2.value += items[idx].value;
|
||||
v2.bits |= (1 << idx);
|
||||
|
||||
s = (v1.value >= v2.value) ? v1 : v2;
|
||||
}
|
||||
|
||||
void main() @safe {
|
||||
import std.stdio;
|
||||
|
||||
auto s = Solution(0, 0);
|
||||
solve(400, items.length - 1, s);
|
||||
|
||||
writeln("Items:");
|
||||
int w = 0;
|
||||
foreach (immutable i, immutable it; items)
|
||||
if (s.bits & (1 << i)) {
|
||||
writeln(" ", it.name);
|
||||
w += it.weight;
|
||||
}
|
||||
writefln("\nTotal value: %d; weight: %d", s.value, w);
|
||||
}
|
||||
22
Task/Knapsack-problem-0-1/D/knapsack-problem-0-1-3.d
Normal file
22
Task/Knapsack-problem-0-1/D/knapsack-problem-0-1-3.d
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
import std.stdio, std.algorithm, std.typecons, std.array, std.range;
|
||||
|
||||
struct Item { string name; int w, v; }
|
||||
alias Pair = Tuple!(int,"tot", string[],"names");
|
||||
|
||||
immutable Item[] items = [{"apple",39,40}, {"banana", 27, 60},
|
||||
{"beer", 52, 10}, {"book", 30, 10}, {"camera", 32, 30},
|
||||
{"cheese", 23, 30}, {"compass", 13, 35}, {"glucose", 15, 60},
|
||||
{"map", 9, 150}, {"note-case", 22, 80}, {"sandwich", 50, 160},
|
||||
{"socks", 4, 50}, {"sunglasses", 7, 20}, {"suntan cream", 11, 70},
|
||||
{"t-shirt", 24, 15}, {"tin", 68, 45}, {"towel", 18, 12},
|
||||
{"trousers", 48, 10}, {"umbrella", 73, 40}, {"water", 153, 200},
|
||||
{"overclothes", 43, 75}, {"waterproof trousers", 42, 70}];
|
||||
|
||||
auto addItem(Pair[] lst, in Item it) pure /*nothrow*/ {
|
||||
auto aux = lst.map!(vn => Pair(vn.tot + it.v, vn.names ~ it.name));
|
||||
return lst[0..it.w] ~ lst[it.w..$].zip(aux).map!q{ a[].max }.array;
|
||||
}
|
||||
|
||||
void main() {
|
||||
reduce!addItem(Pair().repeat.take(400).array, items).back.writeln;
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue