/*Continuous knapsack Problem
(fractional knapsack problem)
---------------------------
Given a set of items, each with a weight and a value,
determine a subset of items to include in a collection so that the total weight
is less than or equal to a given limit and the total value is as large as possible.
Se considera un rucsac de capacitate M si N obiecte, ale
caror greutate si valoare sunt date. Sa se gaseasca
o modalitate de a introduce cat mai multe obiecte in rucsac,
astfel incat valoarea globala sa fie maxima. Se considera ca
obiectele poti fi fractionate. Presupunem ca datele de intrare
sunt corecte si valorile obiectelor sunt strict pozitive. Pe prima
linie a fisierului de intrare obiecte.in se gaseste
capacitatea rucsacului. Numarul maxim de obiecte este 50.
obiecte.in
41
12.34 123.99
23.45 600.54
12.78 90.67
9.34 45.32
rucsac.out
In rucsac vor fi introduse obiecte:
Obiectul 2 23.45 600.54 - complet
Obiectul 1 12.34 123.99 - complet
Obiectul 3: 12.34 123.99 - 5.2 kg (fractionar)
*/
#include <stdio.h>
#include <malloc.h>
#include <stdbool.h>
typedef bool (*comparer)(const void *a, const void *b);
typedef struct {
float weight,
value;
int index;
} Object;
void read(Object *ob, int *n, float *capacity){
float w,v;
while(scanf("%f",&w
) == 1) { ob[(*n)].weight = w;
ob[(*n)].value = v;
ob[(*n)].index = (*n);
(*n)++;
}
}
void bubblesort(Object *ob, int n, comparer comp) {
bool finished = false;
while(!finished) {
bool swapped = false;
for(int i = 0; i < n - 1; ++i) {
if( comp(&ob[i], &ob[i+1])) {
Object temp = ob[i];
ob[i] = ob[i+1];
ob[i+1] = temp;
swapped = true;
}
}
if( swapped ) {
n--;
} else {
finished = true;
}
}
}
bool descending(const void *a, const void *b) {
Object *o1 = (Object*)a;
Object *o2 = (Object*)b;
return o2->value/o2->weight - o1->value/o1->weight > 0;
}
int main(int argc, char const *argv[]) {
int count = 0;
float capacity;
Object arr[100];
read(arr, &count, &capacity);
printf("%d %f\n", count
, capacity
);
bubblesort( arr, count, descending );
int i = 0;
while(capacity>0) {
if(arr[i].weight < capacity) {
capacity -= arr[i].weight;
i++;
} else {
capacity = -capacity;
}
}
for(int j = 0; j < i; j++) {
printf("%d: %.4f %.4f completed object\n", arr
[j
].
index,arr
[j
].
weight, arr
[j
].
value); }
if(capacity<0) {
printf("%d: %.4f %.4f fractional object %f\n", arr
[i
].
index,arr
[i
].
weight, arr
[i
].
value, capacity
); }
return 0;
}
LypDb250aW51b3VzIGtuYXBzYWNrIFByb2JsZW0KKGZyYWN0aW9uYWwga25hcHNhY2sgcHJvYmxlbSkKLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tCkdpdmVuIGEgc2V0IG9mIGl0ZW1zLCBlYWNoIHdpdGggYSB3ZWlnaHQgYW5kIGEgdmFsdWUsCmRldGVybWluZSBhIHN1YnNldCBvZiBpdGVtcyB0byBpbmNsdWRlIGluIGEgY29sbGVjdGlvbiBzbyB0aGF0IHRoZSB0b3RhbCB3ZWlnaHQKaXMgbGVzcyB0aGFuIG9yIGVxdWFsIHRvIGEgZ2l2ZW4gbGltaXQgYW5kIHRoZSAgdG90YWwgdmFsdWUgaXMgYXMgbGFyZ2UgYXMgcG9zc2libGUuCgpTZSBjb25zaWRlcmEgdW4gcnVjc2FjIGRlIGNhcGFjaXRhdGUgTSBzaSBOIG9iaWVjdGUsIGFsZQpjYXJvciBncmV1dGF0ZSBzaSB2YWxvYXJlIHN1bnQgZGF0ZS4gU2Egc2UgZ2FzZWFzY2EKbyBtb2RhbGl0YXRlIGRlIGEgaW50cm9kdWNlIGNhdCBtYWkgbXVsdGUgb2JpZWN0ZSBpbiBydWNzYWMsCmFzdGZlbCBpbmNhdCB2YWxvYXJlYSBnbG9iYWxhIHNhIGZpZSBtYXhpbWEuIFNlIGNvbnNpZGVyYSBjYQpvYmllY3RlbGUgcG90aSBmaSBmcmFjdGlvbmF0ZS4gUHJlc3VwdW5lbSBjYSBkYXRlbGUgZGUgaW50cmFyZQpzdW50IGNvcmVjdGUgc2kgdmFsb3JpbGUgb2JpZWN0ZWxvciBzdW50IHN0cmljdCBwb3ppdGl2ZS4gUGUgcHJpbWEKbGluaWUg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JibGVzb3J0KE9iamVjdCAqb2IsIGludCBuLCBjb21wYXJlciBjb21wKSB7CgogICAgICBib29sIGZpbmlzaGVkID0gZmFsc2U7CgogICAgICAgIHdoaWxlKCFmaW5pc2hlZCkgewogICAgICAgICAgICBib29sIHN3YXBwZWQgPSBmYWxzZTsKCiAgICAgICAgICAgIGZvcihpbnQgaSA9IDA7IGkgPCBuIC0gMTsgKytpKSB7CgogICAgICAgICAgICAgICAgaWYoIGNvbXAoJm9iW2ldLCAmb2JbaSsxXSkpIHsKICAgICAgICAgICAgICAgICAgICBPYmplY3QgdGVtcCA9IG9iW2ldOwogICAgICAgICAgICAgICAgICAgICAgICAgICBvYltpXSA9IG9iW2krMV07CiAgICAgICAgICAgICAgICAgICAgICAgICAgIG9iW2krMV0gPSB0ZW1wOwogICAgICAgICAgICAgICAgICAgICAgICAgICBzd2FwcGVkID0gdHJ1ZTsKICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgfQogICAgICAgICAgICBpZiggc3dhcHBlZCApIHsKICAgICAgICAgICAgICBuLS07CiAgICAgICAgICAgIH0gZWxzZSB7CiAgICAgICAgICAgICAgZmluaXNoZWQgPSB0cnVlOwogICAgICAgICAgICB9CiAgICAgICAgfQp9Cgpib29sIGRlc2NlbmRpbmcoY29uc3Qgdm9pZCAqYSwgY29uc3Qgdm9pZCAqYikgewoKICAgICBPYmplY3QgKm8xID0gKE9iamVjdCopYTsKICAgICBPYmplY3QgKm8yID0gKE9iamVjdCopYjsKCiAgICAgcmV0dXJuIG8yLT52YWx1ZS9vMi0+d2VpZ2h0IC0gbzEtPnZhbHVlL28xLT53ZWlnaHQgPiAwOwp9CgppbnQgbWFpbihpbnQgYXJnYywgY2hhciBjb25zdCAqYXJndltdKSB7CgogIGludCBjb3VudCA9IDA7CgogIGZsb2F0IGNhcGFjaXR5OwoKICBPYmplY3QgYXJyWzEwMF07CgogIHJlYWQoYXJyLCAmY291bnQsICZjYXBhY2l0eSk7CgogIHByaW50ZigiJWQgJW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