-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAssignment.C
More file actions
80 lines (67 loc) · 1.58 KB
/
Assignment.C
File metadata and controls
80 lines (67 loc) · 1.58 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
#include <stdio.h>
#include <stdlib.h>
int size, minCost = 100, **mat, *sol;
/*int fact(int n){
int fact = 0;
for(int i=2;i<=n;i++){
fact *= i;
}
return fact;
}*/
void swap(int *x, int *y){
int temp;
temp = *x;
*x = *y;
*y = temp;
}
void permute(int *a, int l, int r){
int i, cost = 0;
if (l == r){
for(int j=0;j<size;j++){
printf("%d ",a[j]+1);
cost += mat[j][a[j]];
}
printf("cost: %d\n", cost);
if(cost < minCost){
minCost = cost;
for(int k=0; k<size; k++){
sol[k] = a[k];
}
}
}
else{
for (i = l; i <= r; i++){
swap((a+l), (a+i));
permute(a, l+1, r);
swap((a+l), (a+i));
}
}
}
int main(){
int n, *Cost;
printf("Enter number of jobs/people: ");
scanf("%d",&n);
//n = 4;
size = n;
printf("\nEnter the person/job matrix:\n");
mat = (int**)calloc(n, sizeof(int*));
for(int i=0;i<n;i++){
mat[i] = (int*)calloc(n ,sizeof(int));
for(int j=0;j<n;j++){
scanf("%d",&mat[i][j]);
}
}
Cost = (int*)malloc(n*sizeof(int));
sol = (int*)malloc(n*sizeof(int));
for(int i=0; i<n; i++){
Cost[i] = i;
}
permute(Cost, 0, n-1);
printf("Solution is: ");
for(int i=0; i<size; i++){
printf("%d ",sol[i]+1);
}
printf("with cost of %d\n", minCost);
printf("\n");
return 0;
}