-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtps.c
More file actions
109 lines (101 loc) · 2.4 KB
/
tps.c
File metadata and controls
109 lines (101 loc) · 2.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#include <stdlib.h>
#include <stdio.h>
#include <getopt.h>
#include "instance.h"
#include "core.h"
int main(int argc, char * argv[]) {
bool DEBUG = false;
bool LEGACY = false;
int c;
while ((c = getopt(argc , argv, "dl")) != -1) {
switch (c) {
case 'd':
DEBUG = true;
break;
case 'l':
LEGACY = true;
break;
}
}
//file given on cmd line, defaul to stdin
FILE * f = stdin;
if(argc > optind) {
f = fopen(argv[optind],"r");
}
size_t n, m;
fscanf(f," %zd %zd ",&n,&m);
Instance input = {
.height = n,
.width = m,
.agents = calloc(n,sizeof(AJ_Entry)),
.jobs = calloc(m,sizeof(AJ_Entry)),
.matrix = malloc(n*sizeof(M_Entry*)),
};
//store the matrix in a contiguous region of memory
M_Entry * contiguous = calloc(n*m,sizeof(M_Entry));
for(size_t i = 0; i < n; i++) {
input.matrix[i] = contiguous + m*i;
}
uint64_t agents_sum = 0;
for(size_t i = 0; i < n; i++) {
if(LEGACY)
fscanf(f," %ld ",&input.agents[i].discr);
else
fscanf(f," %ld:%ld ",&input.agents[i].tag,&input.agents[i].discr);
agents_sum += input.agents[i].discr;
}
uint64_t jobs_sum = 0;
for(size_t i = 0; i < m; i++) {
if(LEGACY)
fscanf(f," %ld ",&input.jobs[i].discr);
else
fscanf(f," %ld:%ld ",&input.jobs[i].tag,&input.jobs[i].discr);
jobs_sum += input.jobs[i].discr;
}
if (jobs_sum != agents_sum) {
fprintf(stderr,"Error, different sum of agents and jobs: %ld vs %ld !",agents_sum,jobs_sum);
exit(1);
}
for(size_t i = 0; i < n; i++) {
for(size_t j = 0; j < m; j++) {
uint64_t tmp;
fscanf(f," %ld ",&tmp);
input.matrix[i][j].init_cost = tmp;
input.matrix[i][j].val = tmp;
}
}
if(DEBUG) {
printf("Input:\n");
print_inst(&input);
printf("-----------\n");
}
prelim(&input);
if(DEBUG) {
printf("After preliminary work:\n");
print_inst(&input);
printf("-----------\n");
}
while(!done(&input)) {
if(DEBUG)
print_inst(&input);
step1(&input);
}
if(DEBUG)
print_inst(&input);
uint64_t cost = 0;
for (size_t i = 0; i < input.height; i++) {
for (size_t j = 0; j < input.width; j++) {
cost += input.matrix[i][j].init_cost * input.matrix[i][j].quota;
}
}
printf("Minimum cost: %ld\n",cost);
if(!LEGACY) {
for (size_t i = 0; i < input.height; i++) {
for (size_t j = 0; j < input.width; j++) {
if (input.matrix[i][j].quota != 0) {
printf("%ld %ld %ld\n", input.agents[i].tag, input.jobs[j].tag, input.matrix[i][j].quota);
}
}
}
}
}