-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcowtour.java
157 lines (143 loc) · 5.54 KB
/
cowtour.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;
/*
ID: Xu Yan
LANG: JAVA
TASK: cowtour
*/
/**
* Thoughts:
* Pitfalls:
*
* Take-away tips:
*
* @author Xu Yan
* @date May 25th, 2016
*/
public class cowtour {
public static void main(String[] args) throws IOException {
BufferedReader f = new BufferedReader(new FileReader("cowtour.in"));
int pastureCount = Integer.parseInt(f.readLine());
int[][] pastureCoordinate = new int[pastureCount][2];
for (int i = 0; i < pastureCount; i++) {
StringTokenizer coordinate_token = new StringTokenizer(f.readLine());
pastureCoordinate[i] = new int[]{Integer.parseInt(coordinate_token.nextToken()), Integer.parseInt(coordinate_token.nextToken())};
}
char[][] adj_matrix = new char[pastureCount][pastureCount];
for (int row = 0; row < pastureCount; row++) {
String line = f.readLine();
for (int col = 0; col < pastureCount; col++) {
adj_matrix[row][col] = line.charAt(col);
}
}
f.close();
List<List<Integer>> subgraphs = new ArrayList<List<Integer>>();
boolean[] visited = new boolean[pastureCount];
for (int pasture = 0; pasture < pastureCount; pasture++) {
if (!visited[pasture]) {
List<Integer> subgraph = new ArrayList<Integer>();
DFS(subgraph, adj_matrix, pasture, visited);
subgraphs.add(subgraph);
}
}
double[] max_distance_from_node_in_subgraph = new double[pastureCount];
double[] subgraph_diameters = new double[subgraphs.size()];
for (int i = 0; i < subgraphs.size(); i++) {
subgraph_diameters[i] = preprocessing(subgraphs.get(i), adj_matrix, pastureCoordinate, max_distance_from_node_in_subgraph);
}
double subgraph_pair_diameter = 0;
double answer = Double.MAX_VALUE;
for (int i = 0; i < subgraphs.size(); i++) {
List<Integer> subgraph1 = subgraphs.get(i);
for (int j = i + 1; j < subgraphs.size(); j++) {
List<Integer> subgraph2 = subgraphs.get(j);
subgraph_pair_diameter = Math.max(subgraph_diameters[i], subgraph_diameters[j]);
for (int p1 : subgraph1) {
for (int p2 : subgraph2) {
answer = Math.min(answer, Math.max(max_distance_from_node_in_subgraph[p1] + calculateDistance(pastureCoordinate[p1], pastureCoordinate[p2]) + max_distance_from_node_in_subgraph[p2], subgraph_pair_diameter));
}
}
}
}
PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("cowtour.out")));
out.println(String.format("%.6f", answer));
out.close();
}
private static void DFS(List<Integer> subgraph, char[][] adj_matrix, int pasture, boolean[] visited) {
int pastureCount = visited.length;
subgraph.add(pasture);
visited[pasture] = true;
for (int i = 0; i < pastureCount; i++) {
if (!visited[i] && adj_matrix[pasture][i] != '0') {
DFS(subgraph, adj_matrix, i, visited);
}
}
}
private static double preprocessing(List<Integer> subgraph, char[][] adj_matrix, int[][] pastureCoordinate, double[] max_distance_from_node) {
/* Floyd-Warshall algorithm starts ... */
// Initialize every value in distance matrix to Double.POSITIVE_INFINITY
double[][] distance_matrix = initMatrix(adj_matrix.length, adj_matrix[0].length);
// For each edge in subgraph, calculate its length and put into distance_matrix
for (int i = 0; i < subgraph.size(); i++) {
int p1 = subgraph.get(i);
for (int j = 0; j < subgraph.size(); j++) {
int p2 = subgraph.get(j);
if (p1 == p2) { // Be careful about this case
distance_matrix[p1][p2] = 0;
distance_matrix[p2][p1] = 0;
} else if (p1 < p2 /*This aims to avoid duplicate calculation*/ && adj_matrix[p1][p2] == '1') {
double euclidean_distance = calculateDistance(pastureCoordinate[p1], pastureCoordinate[p2]);
distance_matrix[p1][p2] = euclidean_distance;
distance_matrix[p2][p1] = euclidean_distance;
}
}
}
for (int k = 0; k < subgraph.size(); k++) {
int intermediate =subgraph.get(k);
for (int i = 0; i < subgraph.size(); i++) {
int p1 = subgraph.get(i);
for (int j = 0; j < subgraph.size(); j++) {
int p2 = subgraph.get(j);
if (distance_matrix[p1][intermediate] != Double.POSITIVE_INFINITY && distance_matrix[intermediate][p2] != Double.POSITIVE_INFINITY) {
double alternative_distance = distance_matrix[p1][intermediate] + distance_matrix[intermediate][p2];
if (distance_matrix[p1][p2] > alternative_distance) {
distance_matrix[p1][p2] = alternative_distance;
}
}
}
}
}
/* Floyd-Warshall algorithm end ... */
double subgraph_diameter = 0;
for (int i = 0; i < subgraph.size(); i++) {
int p1 = subgraph.get(i);
double p1_max_distance = 0;
for(int j = 0; j < subgraph.size(); j++) {
int p2 = subgraph.get(j);
p1_max_distance = Math.max(p1_max_distance, distance_matrix[p1][p2]);
subgraph_diameter = Math.max(subgraph_diameter, distance_matrix[p1][p2]);
}
max_distance_from_node[p1] = p1_max_distance;
}
return subgraph_diameter;
}
private static double calculateDistance(int[] coordinate1, int[] coordinate2) {
return Math.sqrt(Math.pow(coordinate1[0]-coordinate2[0], 2) + Math.pow(coordinate1[1] - coordinate2[1], 2));
}
private static double[][] initMatrix(int rowCount, int colCount) {
double[][] matrix = new double[rowCount][colCount];
for (int row = 0; row < rowCount; row++) {
for (int col = 0; col < colCount; col++) {
matrix[row][col] = Double.POSITIVE_INFINITY;
}
}
return matrix;
}
}