题意:

有许多油井和村庄什么的,让你使得这些村庄能连通一个油井就好了。第一行给你一个数字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. c语言字符串内存分配小记

    一.疑问 有这样一道题: #include "stdio.h" int main() { ]; ]; scanf("%s", word1); scanf(&qu ...

  2. 【Divided Two】cpp

    题目: Divide two integers without using multiplication, division and mod operator. If it is overflow, ...

  3. 了解JavaScript核心精髓(三)

    1.js判断对象是否存在属性. hasOwnProperty(‘property’)  判断原型属性是否存在. "property" in o;  判断原型属性和原型链属性是否存在 ...

  4. Jforum环境搭建

    前提:搭建好JDK.JRE.Tomcat.数据库 1.之前安装了Navicat Premium,所以直接用这个创建名为jforum的MySQL数据库,默认密码为空,记得设置密码,因为Jforum要用到 ...

  5. Oracle 学习----游标(使用无参光标cursor)

    表: ----游标----------------------------- declare cursor tt is select name,sal from temp; vname temp.na ...

  6. navmesh自动寻路

    一个导航网格(也就是Navmesh)是世界几何体简化的表示法,被游戏代理用于在世界中进行导航.通常,代理(agent )有一个目标,或一个目的地,它试图找到一个路径,然后沿路径导航到达目标.这个过程被 ...

  7. 先立一个书单【flag】,敦促自己温故知新

    书单来源david mimno副教授给ML新生的建议博文,外加一部分搜罗的书籍 学习方式:以书籍查看,习题为辅,代码为最终实现方式,分主题进行今年的学习笔记,立此旗为证. 线代 --> 概率统计 ...

  8. 锚点自适应 map area coords

    最近做MOBILE的HTML5开发,人体图和页面一样,需要自适应不同的手机屏幕,蛋疼的是coords里面的标记是固定的,图片自适应后,锚点的标记就会产生空白区域,看了下https://github.c ...

  9. PB数据窗口中的几种状态及应用

    数据窗口的状态主要有以下几种: 1)New! 2)NewModified! 3)DataModified! 4)NotModified! 数据窗口可以利用这些状态标志判断数据是否被修改过. 记录和字段 ...

  10. 网络namespace

    sduo sysctl -w net.ipv4.conf.all.forwarding=1 sudo iptables -t nat -A  POSTROUTING -s 172.18.0.0/24 ...