Advertisement
Guest User

Untitled

a guest
Mar 12th, 2011
237
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.21 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <limits.h>
  5.  
  6. struct set {
  7. int *nodes;
  8. int position;
  9. int size;
  10. int capacity;
  11. };
  12.  
  13. int locations;
  14. int supermarkets;
  15.  
  16.  
  17.  
  18.  
  19.  
  20. void calc_custo(int **matrix, struct set *set, int *lower){
  21.  
  22.  
  23. int i;
  24. int last;
  25. int cost;
  26. int t;
  27. int j;
  28. int *mins;
  29. struct set *new_set;
  30. new_set = malloc(sizeof(struct set));
  31. new_set->nodes = malloc(new_set->capacity * sizeof(int));
  32.  
  33. mins = malloc((supermarkets + 1) * sizeof(int));
  34. /*
  35. for (i = 0; i < set->size; i ++) {
  36. printf("%d ", set->nodes[i]);
  37. }
  38. printf("\n");*/
  39. for(j = 0; j < supermarkets + 1; j++) {
  40. mins[j] = INT_MAX;
  41. }
  42.  
  43. cost = 0;
  44. for(i = 0; i < set->size; i ++) {
  45. t = set->nodes[i];
  46. cost += matrix[t][0];
  47. for(j = 1; j < supermarkets + 1; j++) {
  48. if (mins[j] > matrix[t][j]) {
  49. mins[j] = matrix[t][j];
  50. }
  51.  
  52. }
  53. }
  54.  
  55. for(j = 1; j < supermarkets + 1; j++) {
  56. cost += mins[j];
  57. }
  58.  
  59. free(mins);
  60.  
  61. memcpy(new_set, set, sizeof(struct set));
  62. memcpy(new_set->nodes, set->nodes, set->capacity * sizeof(int));
  63.  
  64. if (cost < *lower) {
  65. *lower = cost;
  66.  
  67. }
  68.  
  69. if (set->position < set->capacity) {
  70. set->nodes[set->size] = set->position;
  71. set->size++;
  72. set->position++;
  73. calc_custo(matrix, set, lower);
  74.  
  75. }
  76.  
  77. if (new_set->position < new_set->capacity) {
  78. new_set->nodes[new_set->size - 1] = new_set->position;
  79. new_set->position++;
  80. calc_custo(matrix, new_set, lower);
  81. }
  82.  
  83. }
  84.  
  85.  
  86. int main (int argc, const char* argv[])
  87. {
  88.  
  89.  
  90. int t;
  91. int i, j;
  92. int lower;
  93. int **matrix;
  94.  
  95. /*allocat matrix*/
  96.  
  97. scanf("%d", &locations);
  98. scanf("%d", &supermarkets);
  99.  
  100. matrix = malloc(locations * sizeof(int*));
  101. for (i = 0; i < locations; i++){
  102. matrix[i] = malloc((supermarkets + 1) * sizeof(int));
  103.  
  104. }
  105.  
  106. struct set *set;
  107. set = malloc(sizeof(struct set));
  108. set->nodes = malloc(locations * sizeof(int));
  109. set->size = 1;
  110. set->position = 1;
  111. set->capacity = locations;
  112. set->nodes[0] = 0;
  113.  
  114. for (i = 0; i < locations; i++) {
  115. for (j = 0; j < supermarkets + 1; j++) {
  116. scanf("%d", &t);
  117. matrix[i][j] = t;
  118. }
  119. }
  120. lower = INT_MAX;
  121. calc_custo(matrix, set, &lower);
  122. printf("%d\n", lower);
  123. return 0;
  124. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement