Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <limits.h>
- struct set {
- int *nodes;
- int position;
- int size;
- int capacity;
- };
- int locations;
- int supermarkets;
- void calc_custo(int **matrix, struct set *set, int *lower){
- int i;
- int last;
- int cost;
- int t;
- int j;
- int *mins;
- struct set *new_set;
- new_set = malloc(sizeof(struct set));
- new_set->nodes = malloc(new_set->capacity * sizeof(int));
- mins = malloc((supermarkets + 1) * sizeof(int));
- /*
- for (i = 0; i < set->size; i ++) {
- printf("%d ", set->nodes[i]);
- }
- printf("\n");*/
- for(j = 0; j < supermarkets + 1; j++) {
- mins[j] = INT_MAX;
- }
- cost = 0;
- for(i = 0; i < set->size; i ++) {
- t = set->nodes[i];
- cost += matrix[t][0];
- for(j = 1; j < supermarkets + 1; j++) {
- if (mins[j] > matrix[t][j]) {
- mins[j] = matrix[t][j];
- }
- }
- }
- for(j = 1; j < supermarkets + 1; j++) {
- cost += mins[j];
- }
- free(mins);
- memcpy(new_set, set, sizeof(struct set));
- memcpy(new_set->nodes, set->nodes, set->capacity * sizeof(int));
- if (cost < *lower) {
- *lower = cost;
- }
- if (set->position < set->capacity) {
- set->nodes[set->size] = set->position;
- set->size++;
- set->position++;
- calc_custo(matrix, set, lower);
- }
- if (new_set->position < new_set->capacity) {
- new_set->nodes[new_set->size - 1] = new_set->position;
- new_set->position++;
- calc_custo(matrix, new_set, lower);
- }
- }
- int main (int argc, const char* argv[])
- {
- int t;
- int i, j;
- int lower;
- int **matrix;
- /*allocat matrix*/
- scanf("%d", &locations);
- scanf("%d", &supermarkets);
- matrix = malloc(locations * sizeof(int*));
- for (i = 0; i < locations; i++){
- matrix[i] = malloc((supermarkets + 1) * sizeof(int));
- }
- struct set *set;
- set = malloc(sizeof(struct set));
- set->nodes = malloc(locations * sizeof(int));
- set->size = 1;
- set->position = 1;
- set->capacity = locations;
- set->nodes[0] = 0;
- for (i = 0; i < locations; i++) {
- for (j = 0; j < supermarkets + 1; j++) {
- scanf("%d", &t);
- matrix[i][j] = t;
- }
- }
- lower = INT_MAX;
- calc_custo(matrix, set, &lower);
- printf("%d\n", lower);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement