-
Notifications
You must be signed in to change notification settings - Fork 7
/
2112 Optimal Milking.java
94 lines (93 loc) · 2.38 KB
/
2112 Optimal Milking.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
import java.util.*;
import java.io.*;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int machineNumber = in.nextInt();
int cowNumber = in.nextInt();
int maxCap = in.nextInt();
int[][] map = new int[machineNumber + cowNumber + 2][machineNumber + cowNumber + 2];
int start = 0;
int end = machineNumber + cowNumber + 1;
for (int i = 1; i < end; ++i) {
for (int j = 1; j < end; ++j) {
map[i][j] = in.nextInt();
if (map[i][j] == 0) {
map[i][j] = 0x3f3f3f3f;
}
}
}
for (int k = 1; k < end; ++k) {
for (int i = 1; i < end; ++i) {
for (int j = 1; j < end; ++j) {
if (map[i][j] > map[i][k] + map[k][j]) {
map[i][j] = map[i][k] + map[k][j];
}
}
}
}
int left = 0;
int right = 10000;
int rst = 0;
while (left <= right) {
int mid = left + (right - left) / 2;
int[][] matrix = build(mid, map, machineNumber, cowNumber, start, end, maxCap);
int sum = edmondsKarp(matrix, start, end);
if (sum == cowNumber) {
rst = mid;
right = mid - 1;
} else {
left = mid + 1;
}
}
System.out.println(rst);
}
private static int[][] build(int distance, int[][] map, int machineNumber, int cowNumber, int begin, int end, int maxCap) {
int[][] matrix = new int[map.length][map[0].length];
for (int i = 1 + machineNumber; i < end; ++i) {
for (int j = 1; j <= machineNumber; ++j) {
if (map[i][j] <= distance) {
matrix[i][j] = 1;
}
}
}
for (int i = 1; i <= machineNumber; ++i) {
matrix[i][end] = maxCap;
}
for (int i = 1 + machineNumber; i < end; ++i) {
matrix[begin][i] = 1;
}
return matrix;
}
private static int edmondsKarp(int[][] matrix, int start, int end) {
int rst = 0;
int[] prev = new int[matrix.length];
while (bfs(matrix, prev, start, end)) {
for (int i = end; i != start; i = prev[i]) {
matrix[prev[i]][i] -= 1;
matrix[i][prev[i]] += 1;
}
rst += 1;
}
return rst;
}
private static boolean bfs(int[][] map, int[] prev, int start, int end) {
Arrays.fill(prev, -1);
prev[start] = 0;
Queue<Integer> queue = new LinkedList<Integer>();
queue.offer(start);
while (!queue.isEmpty()) {
int first = queue.poll();
for (int i = 1; i <= end; ++i) {
if (prev[i] == -1 && map[first][i] != 0) {
queue.offer(i);
prev[i] = first;
if (i == end) {
return true;
}
}
}
}
return false;
}
}