zoukankan      html  css  js  c++  java
  • UVA Live 6437 Power Plant 最小生成树

    题意:

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

    思路:

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

    代码:

     1 import java.util.Scanner;
     2 import java.util.Comparator;
     3 import java.util.Arrays;
     4 import java.text.DecimalFormat;
     5 
     6 class Node{
     7     public int u, v;
     8     public int w;
     9 }
    10 
    11 class mycmp implements  Comparator<Node>{
    12     public int compare(Node A, Node B){ 
    13                 if(A.w == B.w) return 0;
    14                 else if(A.w < B.w) return -1;
    15                 else return 1;
    16       }  
    17 }
    18 
    19 public class Main {
    20     final static int MAXN = 40000 + 13;
    21     final static int INF = 0x3f3f3f3f;
    22     static int[] pre = new int[MAXN];
    23     static Node[] map = new Node[MAXN];
    24     public static void main(String[] args){
    25         Scanner sc = new Scanner(System.in);
    26         int T = sc.nextInt();
    27         int kas = 1;
    28         while(T != 0){
    29             int N = sc.nextInt();
    30             int M = sc.nextInt();
    31             int K = sc.nextInt();
    32             int power = sc.nextInt();
    33             mst(N);
    34             for(int i = 0; i < K - 1; i++){
    35                 int num = sc.nextInt();
    36                 pre[num] = power;
    37             }
    38             for(int i = 1; i <= M; i++){
    39                 map[i] = new Node();
    40                 map[i].u = sc.nextInt();
    41                 map[i].v = sc.nextInt();
    42                 map[i].w = sc.nextInt();
    43             }
    44             Arrays.sort(map, 1, M + 1,  new mycmp());
    45             int ans = ksu(N, M, K);
    46             System.out.println("Case #" + kas + ": " + ans);
    47             kas++;
    48             T--;
    49         }
    50         sc.close();
    51     }
    52     public static int ksu(int N, int M, int k){ 
    53         int cnt = 0;
    54         int ans = 0;
    55         for(int i = 1; i <= M; i++){
    56             int fu = Find(map[i].u);
    57             int fv = Find(map[i].v);
    58             if(fu != fv){
    59                 cnt++;
    60                 pre[fv] = fu;
    61                 ans += map[i].w;
    62             }
    63             if(cnt == N - k){
    64                 return ans;
    65             }
    66         }
    67         return 0;
    68     }
    69     public static int Find(int x){
    70         return x == pre[x] ? x : (pre[x] = Find(pre[x]));
    71     }
    72     public static void mst(int N){
    73         for(int i = 1; i <= N; i++){
    74             pre[i] = i;
    75         }
    76     }
    77 }
    
    
  • 相关阅读:
    Oracle client 安装、配置
    js 计算金额是否小于总金额,大于是不能保存
    SQL Server函数与存储过程 计算时间
    sql Server 创建临时表 嵌套循环 添加数据
    SQL Server 事务日志文件已满,收缩日志文件(9002)
    sql Server 2008 数据库自动备份维护计划
    面向对象的三个基本特征:继承、封装、多肽
    MySQL 语句调优
    SQL 多表关联更新
    值类型、引用类型
  • 原文地址:https://www.cnblogs.com/Ash-ly/p/5397637.html
Copyright © 2011-2022 走看看