-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMain.java
92 lines (85 loc) · 2.17 KB
/
Main.java
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
package Module1;
import java.util.Arrays;
import java.util.Comparator;
import java.util.PriorityQueue;
class Node {
Node parent;
int pathCost;
int cost;
int workerID;
int jobID;
boolean assigned[];
public Node(int N) {
assigned = new boolean[N];
}
}
public class Main{
static final int N = 4;
static Node newNode(int x,int y,boolean assigned[],Node parent) {
Node node = new Node(N);
for(int j=0;j<N;j++)
node.assigned[j]=assigned[j];
if(y!=-1)
node.assigned[y]=true;
node.parent=parent;
node.workerID=x;
node.jobID=y;
return node;
}
static int calculateCost(int costMatrix[][],int x,int y,boolean assigned[]) {
int cost=0;
boolean available[]=new boolean[N];
Arrays.fill(available, true);
for(int i=x+1;i<N;i++) {
int min=Integer.MAX_VALUE, minIndex=-1;
for(int j=0;j<N;j++) {
if(!assigned[j] && available[j] && costMatrix[i][j]<min) {
minIndex=j;
min=costMatrix[i][j];
}
}
cost += min;
available[minIndex]=false;
}
return cost;
}
static void printAssignments(Node min) {
if(min.parent == null)
return;
printAssignments(min.parent);
System.out.println("Assign Worker "+(char)(min.workerID +'A')+" to job "+min.jobID);
}
static int findMinCost(int costMatrix[][]) {
PriorityQueue<Node> pq = new PriorityQueue<>(Comparator.comparingInt(node->node.cost));
boolean assigned[]=new boolean[N];
Node root=newNode(-1,-1,assigned,null);
root.pathCost=root.cost=0;
root.workerID=-1;
pq.add(root);
while(!pq.isEmpty()) {
Node min = pq.poll();
int i = min.workerID +1;
if(i==N) {
printAssignments(min);
return min.cost;
}
for(int j=0;j<N;j++) {
if(!min.assigned[j]) {
Node child = newNode(i,j,min.assigned,min);
child.pathCost=min.pathCost+costMatrix[i][j];
child.cost=child.pathCost+calculateCost(costMatrix,i ,j, child.assigned);
pq.add(child);
}
}
}
return 0;
}
public static void main(String args[]) {
int costMatrix[][]= {
{9,2,7,8},
{6,4,3,7},
{5,8,1,8},
{7,6,9,4}};
System.out.println("\nOptimal Cost is "+findMinCost(costMatrix));
}
}