Repository navigation
Expand file tree
/
Copy pathCode03_Water_Luogu.java
More file actions
145 lines (127 loc) · 3.37 KB
/
Copy pathCode03_Water_Luogu.java
File metadata and controls
145 lines (127 loc) · 3.37 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
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
package class061;
// 水井
// 本题就是题目3,题意是一样的,来自洛谷的测试
// 一共n个田地,给定长度为n的数组cost,cost[i]表示在i号田地打井的花费
// 给定n * n的矩阵mat,mat[i][j]表示i号田地和j号田地连通的花费
// 题目保证 mat[i][j] == mat[j][i]
// 如果某个田地打了井,那么认为该田地有水可用
// 或者某个田地和有水可用的田地连通,也认为该田地有水可用
// 打印让所有田地都有水可用,最小的花费
// 1 <= n <= 300
// 0 <= 花费 <= 10^5
// 测试链接 : https://www.luogu.com.cn/problem/P1550
// 提交以下的code,提交时请把类名改成"Main",可以通过所有测试用例
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.util.Arrays;
public class Code03_Water_Luogu {
public static int MAXN = 301;
public static int MAXM = 100001;
// 题目输入
public static int n;
public static int[] cost = new int[MAXN];
public static int[][] mat = new int[MAXN][MAXN];
// 端点u、端点v、边权w
public static int[][] edges = new int[MAXM][3];
// 并查集
public static int[] father = new int[MAXN];
public static int find(int i) {
if (i != father[i]) {
father[i] = find(father[i]);
}
return father[i];
}
public static boolean union(int x, int y) {
int fx = find(x);
int fy = find(y);
if (fx != fy) {
father[fx] = fy;
return true;
} else {
return false;
}
}
public static int compute() {
for (int i = 0; i <= n; i++) {
father[i] = i;
}
int cnt = 0;
// 节点0是虚拟水源,连接0和i的边表示在i号田地打井,边权为cost[i]
for (int i = 1; i <= n; i++) {
edges[++cnt][0] = 0;
edges[cnt][1] = i;
edges[cnt][2] = cost[i];
}
// 根据给定的连通代价,去生成其他的边
for (int i = 1; i <= n; i++) {
for (int j = i + 1; j <= n; j++) {
edges[++cnt][0] = i;
edges[cnt][1] = j;
edges[cnt][2] = mat[i][j];
}
}
// 边按照权值排序
Arrays.sort(edges, 1, cnt + 1, (a, b) -> a[2] - b[2]);
int ans = 0;
for (int i = 1; i <= cnt; i++) {
if (union(edges[i][0], edges[i][1])) {
ans += edges[i][2];
}
}
return ans;
}
public static void main(String[] args) throws Exception {
FastReader in = new FastReader(System.in);
PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));
n = in.nextInt();
for (int i = 1; i <= n; i++) {
cost[i] = in.nextInt();
}
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
mat[i][j] = in.nextInt();
}
}
int ans = compute();
out.println(ans);
out.flush();
out.close();
}
// 读写工具类
static class FastReader {
private final byte[] buffer = new byte[1 << 16];
private int ptr = 0, len = 0;
private final InputStream in;
FastReader(InputStream in) {
this.in = in;
}
private int readByte() throws IOException {
if (ptr >= len) {
len = in.read(buffer);
ptr = 0;
if (len <= 0)
return -1;
}
return buffer[ptr++];
}
int nextInt() throws IOException {
int c;
do {
c = readByte();
} while (c <= ' ' && c != -1);
boolean neg = false;
if (c == '-') {
neg = true;
c = readByte();
}
int val = 0;
while (c > ' ' && c != -1) {
val = val * 10 + (c - '0');
c = readByte();
}
return neg ? -val : val;
}
}
}