题意:

有许多油井和村庄什么的,让你使得这些村庄能连通一个油井就好了。第一行给你一个数字T代表有T组测试数据,第二行有 M , N , K ,M代表包括油井在内的村庄数,N 代表有N个 两两连通的地方。K代表有K个油井。接下来有N行,每行三个数 u , v, w, 代表 u 号和 v 是连通的 权值为 w。

思路:

以往做的题目都是只有一个源点,这道题油井的数目就是源点,所以源点不唯一。但是不要想复杂啦、其实一开始直接让所有源点并在一、再求最小生成树就好了。

代码:

 import java.util.Scanner;
import java.util.Comparator;
import java.util.Arrays;
import java.text.DecimalFormat; class Node{
public int u, v;
public int w;
} class mycmp implements Comparator<Node>{
public int compare(Node A, Node B){
if(A.w == B.w) return 0;
else if(A.w < B.w) return -1;
else return 1;
}
} public class Main {
final static int MAXN = 40000 + 13;
final static int INF = 0x3f3f3f3f;
static int[] pre = new int[MAXN];
static Node[] map = new Node[MAXN];
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
int T = sc.nextInt();
int kas = 1;
while(T != 0){
int N = sc.nextInt();
int M = sc.nextInt();
int K = sc.nextInt();
int power = sc.nextInt();
mst(N);
for(int i = 0; i < K - 1; i++){
int num = sc.nextInt();
pre[num] = power;
}
for(int i = 1; i <= M; i++){
map[i] = new Node();
map[i].u = sc.nextInt();
map[i].v = sc.nextInt();
map[i].w = sc.nextInt();
}
Arrays.sort(map, 1, M + 1, new mycmp());
int ans = ksu(N, M, K);
System.out.println("Case #" + kas + ": " + ans);
kas++;
T--;
}
sc.close();
}
public static int ksu(int N, int M, int k){
int cnt = 0;
int ans = 0;
for(int i = 1; i <= M; i++){
int fu = Find(map[i].u);
int fv = Find(map[i].v);
if(fu != fv){
cnt++;
pre[fv] = fu;
ans += map[i].w;
}
if(cnt == N - k){
return ans;
}
}
return 0;
}
public static int Find(int x){
return x == pre[x] ? x : (pre[x] = Find(pre[x]));
}
public static void mst(int N){
for(int i = 1; i <= N; i++){
pre[i] = i;
}
}
}
												

UVA Live 6437 Power Plant 最小生成树的更多相关文章

  1. LA 6437 Power Plant (prim最小生成树)

    还是裸的最小生成树 #include<bits/stdc++.h> using namespace std; int T,N,M,P,K,a,b,c; int dist[1020],m[1 ...

  2. 缩点:Power Plant;

    题目传送门:[UVALive 6437]Power Plant 题目大意:T组数据,给定一幅带权图(n, m), 然后给定k个点, 与图中存在有若干条边.每个点都要至少要和这k个点的一个点直接或间接相 ...

  3. 【CF434D】Nanami's Power Plant 最小割

    [CF434D]Nanami's Power Plant 题意:有n个二次函数$y=a_ix^2+b_ix+c_i$($a_i,b_i,c_i$是整数),第i个函数要求x的取值在$[l_i,r_i]$ ...

  4. Codeforces Round #248 (Div. 1) D - Nanami's Power Plant 最小割

    D - Nanami's Power Plant 思路:类似与bzoj切糕那道题的模型.. #include<bits/stdc++.h> #define LL long long #de ...

  5. Nuclear Power Plant ZOJ - 3840 树形dp

    There are N (1 ≤ N ≤ 105) cities on land, and there are N - 1 wires connecting the cities. Therefore ...

  6. [Codeforces 1245D] Shichikuji and Power Grid (最小生成树)

    [Codeforces 1245D] Shichikuji and Power Grid (最小生成树) 题面 有n个城市,坐标为\((x_i,y_i)\),还有两个系数\(c_i,k_i\).在每个 ...

  7. uva 10369 Arctic Network (最小生成树加丁点变形)

    The Department of National Defence(DND)wishestoconnectseveral northern outposts by a wireless networ ...

  8. UVA 1151二进制枚举子集 + 最小生成树

    题意:平面上有n个点(1<=N<=1000),你的任务是让所有n个点连通,为此, 你可以新建一些边,费用等于两个端点的欧几里得距离的平方.另外还有q(0<=q<=8)个套餐(数 ...

  9. UVA 1151 买还是建(最小生成树)

    买还是建 紫书P358 [题目链接]买还是建 [题目类型]最小生成树 &题解: 这题真的心累,看了3天,最后照着码还是wa,先放lrj代码,以后再看吧 &代码: // UVa1151 ...

随机推荐

  1. iOS笔记056 - UI总结02

    九宫格布局 UICollectionViewController 创建控制器一定要指定默认的布局样式. // 加载一个九宫格布局的控制器,必须指定布局样式 UICollectionViewFlowLa ...

  2. Percona-Tookit工具包之pt-table-usage

      Preface       There always be some table join operations in our SQL statement.Although we can know ...

  3. BigDecimal简单说

    1) 浮点数的舍弃规则: 假设小数点后保留两位 RoundingMode.CEILING:向正无穷大的方向舍入:  1.245 → 1.25   -1.245 → -1.24 RoundingMode ...

  4. 孤荷凌寒自学python第四十天python 的线程锁RLock

     孤荷凌寒自学python第四十天python的线程锁RLock (完整学习过程屏幕记录视频地址在文末,手写笔记在文末) 因为研究同时在多线程中读写同一个文本文件引发冲突,所以使用Lock锁尝试同步, ...

  5. 课时21:函数:lambda表达式

    目录: 一.lambda表达式 二.介绍两个BIF:filter()和map() 三.课时21课后习题及答案 ********************* 一.lambda表达式 *********** ...

  6. 微信公众号开发java框架:wx4j(入门篇)

    导航 入门 http://www.cnblogs.com/2333/p/6617819.html WxServlet介绍 MaterialUtils 素材工具类使用说明 http://www.cnbl ...

  7. nagios服务端安装

    系统环境:操作系统:CentOS-5.7 x86_64Apache版本: Apache-2.2.22Nagios版本: nagios-3.3.1GD库: gd-2.0.33 2.安装前准备:2.1.安 ...

  8. MySQL之优化总结

    http://www.cnblogs.com/benshan/archive/2012/07/27/2612212.html MySQL之优化总结    今天,数据库的操作越来越成为整个应用的性能瓶颈 ...

  9. IPV6地址格式分析

    IPV6地址格式分析 IPv6的地址长度是128位(bit). 将这128位的地址按每16位划分为一个段,将每个段转换成十六进制数字,并用冒号隔开. 例如:2000:0000:0000:0000:00 ...

  10. spring in action 学习笔记十四:用纯注解的方式实现spring mvc

    在讲用纯注解的方式实现springmvc之前先介绍一个类:AbstractAnnotationDispatcherServletInitializer.这个类的作用是:任何一个类继承AbstractA ...