题目描述
Pear市一共有N(<=50000)个居民点,居民点之间有M(<=200000)条双向道路相连。这些居民点两两之间都可以通过双向道路到达。这种情况一直持续到最近,一次严重的地震毁坏了全部M条道路。
震后,Pear打算修复其中一些道路,修理第i条道路需要Pi的时间。不过,Pear并不打算让全部的点连通,而是选择一些标号特殊的点让他们连通。
Pear有Q(<=50000)次询问,每次询问,他会选择所有编号在[l,r]之间,并且 编号 mod K = C 的点,修理一些路使得它们连通。由于所有道路的修理可以同时开工,所以完成修理的时间取决于花费时间最长的一条路,即涉及到的道路中Pi的最大值。
你能帮助Pear计算出每次询问时需要花费的最少时间么?这里询问是独立的,也就是上一个询问里的修理计划并没有付诸行动。
【输入格式】
第一行三个正整数N、M、Q,含义如题面所述。
接下来M行,每行三个正整数Xi、Yi、Pi,表示一条连接Xi和Yi的双向道路,修复需要Pi的时间。可能有自环,可能有重边。1<=Pi<=1000000。
接下来Q行,每行四个正整数Li、Ri、Ki、Ci,表示这次询问的点是[Li,Ri]区间中所有编号Mod Ki=Ci的点。保证参与询问的点至少有两个。
【输出格式】
输出Q行,每行一个正整数表示对应询问的答案。
【样例输入】
7 10 4
1 3 10
2 6 9
4 1 5
3 7 4
3 6 9
1 5 8
2 7 4
3 2 10
1 7 6
7 6 9
1 7 1 0
1 7 3 1
2 5 1 0
3 7 2 1
【样例输出】
9
6
8
8
最小生成树
一种容易想到的思路是在每次询问的时候构造最小生成树,在构造最小生成树的过程中观察询问的[l,r]集合中余k为c的点是否已经连通,如果连通则输出最后加入的那条边的权,该权值即为使得此次查询的点连通的最大花费。
该思路中使用到了conparable接口来实现边的排序,使用到了并查集。
效率较低。
import java.util.*;
public class Main {
static int N, M, Q;
static Edge[] edges;
static UnionFind uf;
static ArrayList<Integer> costs = new ArrayList<Integer>();
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
N = sc.nextInt();
M = sc.nextInt();
Q = sc.nextInt();
edges = new Edge[M];
uf = new UnionFind(N + 1);
for (int i = 0; i < edges.length; i++) {
int a = sc.nextInt();
int b = sc.nextInt();
int cost = sc.nextInt();
edges[i] = new Edge(a, b, cost);
}
Arrays.sort(edges); // 将边集排序
for (int i = 0; i < Q; i++) {
int l = sc.nextInt();
int r = sc.nextInt();
int mod = sc.nextInt();
int c = sc.nextInt();
buildMst(l, r, mod, c);
}
for (Integer i : costs) {
System.out.println(i);
}
sc.close();
}
/**
* 逐步加入边到最小生成树中,若加入该边后使得[l,r]中余mod等于c的点连通,则输出最后加入的边的cost
*
* @param l
* 左边界
* @param r
* 右边界
* @param mod
* 模
* @param c
* 余数
*/
private static void buildMst(int l, int r, int mod, int c) {
for (int i = 0; i < uf.ufNodes.length; i++) {
uf.ufNodes[i].parent = null;
}
for (int i = 0; i < M; i++) {
Edge edge = edges[i];
int from = edge.from;
int to = edge.to;
int cost = edge.cost;
// 如果该边加入不会构成环,则将其加入最小生成树
if (uf.find(from) == uf.find(to)) {
continue;
} else {
uf.merge(from, to);
}
// 判断要关注的所有点是否连通
boolean isOk = true;
UnionFind.UFNode parent = null;
for (int j = l; j <= r; j++) {
if (j % mod == c) {
if (parent == null) {
parent = uf.find(j); // 第一个关注点的老大
} else {
if (parent != uf.find(j)) { // 没有连通
isOk = false;
break;
}
}
}
}
// 如果isOk为true,说明最后加的一条边即为使得要关注的点连通的边,输出cost则为最大花费
if (isOk) {
costs.add(Integer.valueOf(cost));
break;
}
}
}
static class Edge implements Comparable<Edge> {
public int from; // 边的起点
public int to; // 边的终点
public int cost; // 代价
public Edge(int from, int to, int cost) {
super();
this.from = from;
this.to = to;
this.cost = cost;
}
public Edge() {
}
@Override
public int compareTo(Edge o) {
return cost > o.cost ? 1 : (cost == o.cost) ? 0 : -1;
}
}
// 并查集
static class UnionFind {
UFNode[] ufNodes;
int count;
static class UFNode {
UFNode parent;
}
public UnionFind(int count) {
ufNodes = new UFNode[count];
for (int i = 0; i < ufNodes.length; i++) {
ufNodes[i] = new UFNode();
}
this.count = count;
}
// 查找并压缩查找路径
UFNode find(int i) {
UFNode node = ufNodes[i];
// 本身就是树根
if (node == null) {
return node;
}
// set存储不是根节点的UFNode,之后用来压缩路径
Set<UFNode> set = new HashSet<UFNode>();
// 寻找树根
while (node.parent != null) {
set.add(node);
node = node.parent;
}
// 压缩路径,让某个并查集的所有元素的parent为该集合的老大
for (UFNode ufNode : set) {
ufNode.parent = node;
}
return node;
}
// 合并两个并查集
void merge(int a, int b) {
find(b).parent = find(a);
}
}
}
优化1
如上代码在每次查询时均要构建最小生成树,能不能只构建一次最小生成树,之后每次查询只需要查询该最小生成树中使得余Ki等于Ci的所有点连通的花费最大的边呢?
那么需要一个存储最小生成树的数据结构,此处使用邻接表。
同时为了查询,需要一个二维数组ff,利用ff可以查询两个顶点连通的最大花费。
但是由于本人太菜,看不懂官方视频也不会写。