-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path116_Unidirectional_TSP.java
85 lines (73 loc) · 2.77 KB
/
116_Unidirectional_TSP.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
import java.io.IOException;
import java.util.Arrays;
import java.util.Scanner;
public class Main {
public static void main(String[] args) throws IOException {
Scanner in = new Scanner(System.in);
int rows;
int cols;
while( in.hasNext()) {
rows = in.nextInt();
cols = in.nextInt();
if(rows < 1 || cols < 1)
continue;
Node[][] matrix = new Node[rows][cols];
int colsminus = cols -1;
for (int r = 0; r < rows; r++) {
for (int c = 0; c < cols; c++) {
if(c == colsminus){
matrix[r][c] = new Node();
matrix[r][c].weight = in.nextInt();
matrix[r][colsminus].distance = matrix[r][colsminus].weight; //first columns weight is also the distance
} else {
matrix[r][c] = new Node();
matrix[r][c].weight = in.nextInt();
matrix[r][c].distance = Long.MAX_VALUE;
}
}
}
for (int c = cols - 2; c >= 0; c--) {
for (int r = 0; r < rows; r++) {
int up = (r == 0) ? rows - 1 : r - 1;
int down = (r + 1) % rows;
int nextcol = c + 1;
matrix[r][c].next = Integer.MAX_VALUE;
int[] rowsToTry = {up, r, down};
Arrays.sort(rowsToTry);
for (int rowToTry : rowsToTry) {
if (matrix[r][c].next == Integer.MAX_VALUE ||
(matrix[rowToTry][nextcol].distance < matrix[matrix[r][c].next][nextcol].distance)) {
matrix[r][c].next = rowToTry;
matrix[r][c].distance = matrix[r][c].weight + matrix[rowToTry][nextcol].distance;
}
}
}
}
long minWeight = Long.MAX_VALUE;
int startRow = 0;
for (int r = 0; r < rows; r++) {
if (matrix[r][0].distance < minWeight) {
startRow = r;
minWeight = matrix[r][0].distance;
}
}
int r = startRow;
for (int c = 0; c < cols; c++) {
System.out.print((c > 0 ? " " : "") + (r + 1));
r = matrix[r][c].next;
}
System.out.println();
System.out.println(minWeight);
}
}
private static class Node {
public int weight;
public long distance;
public int next;
public Node(){
weight = 0;
distance = 0;
next = 0;
}
}
}