1146. 新的开始 - AcWing题库
//建立一个虚拟远点
import java.util.*;
public class Main{
static int N = 310;
static int[][] w = new int[N][N];
static int[] dist = new int[N];
static boolean[] st = new boolean[N];
static int n, res;
public static int prim(){
Arrays.fill(dist, 0x3f3f3f3f);
dist[0] = 0;
for(int i = 0; i < n + 1; i ++){
int t = -1;
for(int j = 0; j < n + 1; j ++){
if(!st[j] && (t == -1 || dist[t] > dist[j])){
t = j;
}
}
st[t] = true;
res += dist[t];
for(int j = 0; j < n + 1; j ++){
dist[j] = Math.min(dist[j], w[t][j]);//t点到这个点的距离短还是虚拟远点到这个点的距离短
}
}
return res;
}
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
n = sc.nextInt();
for(int i = 1; i <= n; i ++){//虚拟远点到每个点的距离
w[0][i] = sc.nextInt();
w[i][0] = w[0][i];
}
for(int i = 1; i <= n; i ++){
for(int j = 1; j <= n; j ++){
w[i][j] = sc.nextInt();
}
}
System.out.print(prim());
}
}
1145. 北极通讯网络 - AcWing题库
找到一个最小的d值,将所有权值大于d的边删去,整个图形连通块的数量不超过k条
import java.util.*;
class PII_1{
int x, y;
public PII_1(int x, int y){
this.x = x;
this.y = y;
}
}
class PII_2 implements Comparable<PII_2>{
int a, b;
double c;
public PII_2(int a, int b, double c){
this.a = a;
this.b = b;
this.c = c;
}
public int compareTo(PII_2 o){
return Double.compare(c, o.c);
}
}
public class Main{
static int N = 510, M = N * N;
static PII_1[] a = new PII_1[M];
static PII_2[] q = new PII_2[M];
static int[] p = new int[M];
static int n, k, m;
public static double get_dist(PII_1 a, PII_1 b){
int dx = a.x - b.x;
int dy = a.y - b.y;
return Math.sqrt(dx * dx + dy * dy);
}
public static int find(int x){
if(p[x] != x) p[x] = find(p[x]);
return p[x];
}
public static void Kruskal(){
Arrays.sort(q, 0, m);//一定要记得排序
int cnt = n;
double res = 0.0;
for(int i = 0; i < m; i ++){
if(cnt <= k) break;
int a = q[i].a;
int b = q[i].b;
double c = q[i].c;
a = find(a);
b = find(b);
if(a != b){
p[a] = b;
cnt --;
res = c;
}
}
System.out.printf("%.2f", res);
}
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
n = sc.nextInt();
k = sc.nextInt();
for(int i = 0; i < n; i ++) p[i] = i;
for(int i = 0; i < n; i ++){
int x = sc.nextInt();
int y = sc.nextInt();
a[i] = new PII_1(x, y);//每个村庄的坐标
}
for(int i = 0; i < n; i ++){
for(int j = 0; j < n; j ++){//村庄与村庄之间两两的距离
q[m ++] = new PII_2(i, j, get_dist(a[i], a[j]));
}
}
Kruskal();
}
}
346. 走廊泼水节 - AcWing题库
import java.util.*;
class PII implements Comparable<PII>{
int a, b, c;
public PII(int a, int b, int c){
this.a = a;
this.b = b;
this.c = c;
}
public int compareTo(PII o){
return Integer.compare(c, o.c);
}
}
public class Main{
static int N = 6010, n;
static int[] p = new int[N];
static int[] size = new int[N];
static PII[] q = new PII[N];
public static int find(int x){
if(p[x] != x) p[x] = find(p[x]);
return p[x];
}
public static void Kruskal(){
Arrays.sort(q, 0, n - 1);
int res = 0;
for(int i = 0; i < n - 1; i ++){
int a = q[i].a;
int b = q[i].b;
int c = q[i].c;
a = find(a);
b = find(b);
if(a != b){
res += (size[a] * size[b] - 1) * (c + 1);//权值加1
p[a] = b;
size[b] += size[a];
}
}
System.out.println(res);
}
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
int T = sc.nextInt();
while(T -- > 0){
n = sc.nextInt();
for(int i = 1; i <= n; i ++){
p[i] = i;
size[i] = 1;
}
for(int i = 0; i < n - 1; i ++){
int a = sc.nextInt();
int b = sc.nextInt();
int c = sc.nextInt();
q[i] = new PII(a, b, c);//先把点加进去
}
Kruskal();
}
}
}
1148. 秘密的牛奶运输 - AcWing题库
次小生成树:
定义:给一个带权的图,把图的所有生成树按权值从小到大排序,第二小的称为次小生成树(有些题目最小生成树不唯一,次小生成树可以和最小生成树相等)
方法一:先求最小生成树,然后依次枚举删去最小生成树的边求解O(mlogm + nm)
只能求出来非严格最小生成树。
方法二:先求最小生成树,然后依次枚举非树边,将该边加入到树中,同时从树中去掉一条 边,使得最终的图仍是一棵树,则一定可以求出次小生成树。