我一直在研究这个问题,并且可以得到一些结果,但是在这里实现分支和绑定方法时遇到了麻烦。
你们能帮帮我吗?
建造仓库
描述
中了彩票后,您决定购买几辆卡车(或卡车)。您的目标是将货物运送到科英布拉的所有超市。但是现在你必须建立仓库来储存货物,你必须考虑可能的位置。理想情况下,仓库应靠近超市,以降低运输成本。然而,你不可能把所有的钱都花在到处建仓库上,所以你必须做出一个聪明的决定:考虑到在每个可能的地点建造每个仓库的固定成本以及未来 5 年从每个地点为每个超市提供服务的运输成本,您想知道应该在哪里建造仓库,以便在该期间内的总成本(运输和固定成本)最小。注意至少要建一个仓库。此外,总运输成本的计算必须考虑到必须为所有超市提供服务。
输入
每个测试用例都包含有关在给定地点建造仓库的固定成本以及与每个地点和超市相关的运输成本的信息。每个测试用例的第一行给出了可以建造仓库的可能位置的数量(n<51)和超市的数量(m<16)。然后,以下 n 行中的每一行给出了在该位置建造仓库的成本以及与从该位置供应 m 个超市中的每一个相关的运输成本。
输出
输出是建造和运营仓库的最低总成本(整数)。例子
输入:
4 5
10 8 6 10 8 10
9 1 2 10 4 8
10 6 4 2 1 5
1 10 4 6 9 3
输出:
26
#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;
}