【发布时间】:2011-03-13 05:30:17
【问题描述】:
我一直在研究这个问题,我可以得到一些结果,但是我在这里实现分支和绑定方法时遇到了麻烦。
你们能帮帮我吗?
建造仓库
说明
中奖后,你决定 购买几辆卡车(或卡车)。 您的目标是向所有人交付货物 科英布拉的超市。但现在你 必须建造仓库来存放 货,你要考虑 可能的位置。理想情况下, 仓库应靠近 超市为了减少 交通费用。然而,你 不能把所有的钱都花在建筑上 到处都是仓库,所以你必须 做出明智的决定:鉴于 建造每个仓库的固定成本 在每个可能的位置和 服务每个人的运输成本 超市从各个位置 未来5年,你想知道在哪里 应建造仓库,以便 总成本(运输和固定 成本)在那个时期是最低的。 请注意,至少一个仓库必须 被建造。此外,计算 总运输成本必须 考虑到所有 超市必须提供服务。
输入
每个测试用例都包含信息 关于建筑的固定成本 指定地点的仓库和 与每个相关的运输成本 位置和超市。首先 每个测试用例的行给出 可能的位置数量 可以建造仓库(n
输出
输出是最小的总成本 建设和运营 仓库(整数)。示例
输入:
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;
}
【问题讨论】:
-
我认为没有人会检查您的所有代码。我真的不明白问题出在哪里。
标签: algorithm dynamic-programming branch-and-bound