Repository navigation
Expand file tree
/
Copy pathCode05_BusyCities.java
More file actions
102 lines (89 loc) · 3.11 KB
/
Copy pathCode05_BusyCities.java
File metadata and controls
102 lines (89 loc) · 3.11 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
package class061;
// 繁忙的都市
// 一个非常繁忙的大都市,城市中的道路十分的拥挤,于是市长决定对其中的道路进行改造
// 城市的道路是这样分布的:城市中有n个交叉路口,有些交叉路口之间有道路相连
// 两个交叉路口之间最多有一条道路相连接,这些道路是双向的
// 且把所有的交叉路口直接或间接的连接起来了
// 每条道路都有一个分值,分值越小表示这个道路越繁忙,越需要进行改造
// 但是市政府的资金有限,市长希望进行改造的道路越少越好,于是他提出下面的要求:
// 1. 改造的那些道路能够把所有的交叉路口直接或间接的连通起来
// 2. 在满足要求1的情况下,改造的道路尽量少
// 3. 在满足要求1、2的情况下,改造的那些道路中分值最大的道路分值尽量小
// 作为市规划局的你,应当作出最佳的决策,选择哪些道路应当被修建
// 返回选出了几条道路 以及 分值最大的那条道路的分值是多少
// 测试链接 : https://www.luogu.com.cn/problem/P2330
// 请同学们务必参考如下代码中关于输入、输出的处理
// 这是输入输出处理效率很高的写法
// 提交以下所有代码,把主类名改成Main,可以直接通过
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.StreamTokenizer;
import java.util.Arrays;
public class Code05_BusyCities {
public static int MAXN = 301;
public static int MAXM = 8001;
public static int[] father = new int[MAXN];
public static int[][] edges = new int[MAXM][3];
public static int n, m;
public static void build() {
for (int i = 1; i <= n; i++) {
father[i] = i;
}
}
public static int find(int i) {
if (i != father[i]) {
father[i] = find(father[i]);
}
return father[i];
}
// 如果x和y本来就是一个集合,返回false
// 如果x和y不是一个集合,合并之后返回true
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 void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StreamTokenizer in = new StreamTokenizer(br);
PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));
while (in.nextToken() != StreamTokenizer.TT_EOF) {
n = (int) in.nval;
in.nextToken();
m = (int) in.nval;
build();
for (int i = 0; i < m; i++) {
in.nextToken();
edges[i][0] = (int) in.nval;
in.nextToken();
edges[i][1] = (int) in.nval;
in.nextToken();
edges[i][2] = (int) in.nval;
}
Arrays.sort(edges, 0, m, (a, b) -> a[2] - b[2]);
int ans = 0;
int edgeCnt = 0;
for (int[] edge : edges) {
if (union(edge[0], edge[1])) {
edgeCnt++;
ans = Math.max(ans, edge[2]);
}
if (edgeCnt == n - 1) {
break;
}
}
out.println((n - 1) + " " + ans);
}
out.flush();
out.close();
br.close();
}
}