CCF CSP 数据中心 c++ python csp201812_4 100分



样例输入
4
5
1
1 2 3
1 3 4
1 4 5
2 3 8
3 4 2
样例输出
4
样例说明
  下图是样例说明。


问题分析
        这道题理解了题意后,其实就是求图的最小生成树,那个根没有一点用,根本不用考虑,只需要求出最小生成树的最大权值就ok了。传统的求最小生成树的算法有Prim算法和kruskal算法。但这篇博客我使用并查集的方法去解决。如果对并查集不了解的可以先去查查并查集。具体代码如下:
c++代码如下:

#include<cstdio>
#include<vector>
#include<algorithm>
using namespace std;
struct Edge{int from;int to;int distance;
};
int father[50005];
bool cmp(Edge e1,Edge e2){return e1.distance < e2.distance;
}
int result = 0;
int main(){void init(int number);void unio(int from,int to,int distance);int vertextNumber,edgeNumber,root;scanf("%d%d%d",&vertextNumber,&edgeNumber,&root);vector<Edge> vec;Edge temp;int from,to,distance;for(int i = 0;i < edgeNumber;i++){scanf("%d%d%d",&from,&to,&distance);temp.from = from;temp.to = to;temp.distance = distance;vec.push_back(temp);}sort(vec.begin(),vec.end(),cmp);init(vertextNumber);for(int i = 0;i < vec.size();i++){temp = vec[i];from = temp.from;to = temp.to;distance = temp.distance;unio(from,to,distance);}printf("%d\n",result);return 0;
}
void init(int number){for(int i = 0;i <= number;i++){father[i] = i;}
}
int findFather(int x){int a = x;while(x != father[x]){x = father[x];}while(a != father[a]){int z = father[a];father[a] = x;a = z;}return x;
}
void unio(int from,int to,int distance){int fx = findFather(from);int fy = findFather(to);if(fx != fy){father[fx] = fy;result = distance;}
}

java代码如下:

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Scanner;public class csp201812_4 {public static int edgeNumber;public static int[] father;public static int result;public static void main(String[] args) {Scanner input = new Scanner(System.in);edgeNumber = input.nextInt();int number = input.nextInt();int root = input.nextInt();List<Edge> list = new ArrayList<>();int from,to,distance;for(int i = 0;i < number;i++) {from = input.nextInt();to = input.nextInt();distance = input.nextInt();list.add(new Edge(from,to,distance));}init();Collections.sort(list);Edge temp;for(int i = 0;i < list.size();i++) {temp = list.get(i);Union(temp.from,temp.to,temp.distance);}System.out.println(result);input.close();}public static void init() {result = 0;father = new int[edgeNumber + 1];for(int i = 0;i < father.length;i++) {father[i] = i;}}public static int findFather(int x) {int a = x;while(x != father[x]) {x = father[x];}while(a != father[a]) {int z = a;a = father[a];father[z] = x; }return x;}public static void Union(int x,int y,int dd) {int fx = findFather(x);int fy = findFather(y);if(fx != fy) {father[fx] = fy;result = dd;}}
}
class Edge implements Comparable<Edge>{public int from;public int to;public int distance;public Edge(int from, int to, int distance) {super();this.from = from;this.to = to;this.distance = distance;}@Overridepublic int compareTo(Edge o) {return this.distance > o.distance ? 1 : this.distance < o.distance ? -1 : 0;}
}

由于java代码运行时间比较长,故提交后运行超时,只能得80分。
python3代码如下:

from operator import attrgetter
class Edge(object):def __init__(self,From,to,distance):self.From = Fromself.to = toself.distance = distance
father = []
result = 0
def init(number):for i in range(number):father.append(i)
def findFather(x):a = xwhile x != father[x]:x = father[x]while a != father[a]:z = father[a]father[a] = xa = zreturn x
def union(From, to, distance):global resultfx = findFather(From)fy = findFather(to)if fx != fy:father[fx] = fyresult = distance
if __name__ == '__main__':vertexNumber = (int)(input())edgeNumber = (int)(input())root = (int)(input())lists = []for i in range(edgeNumber):temp = input().split(" ")From = (int)(temp[0])to = (int)(temp[1])distance = (int)(temp[2])edge = Edge(From, to, distance)lists.append(edge)init(vertexNumber + 1)lists = sorted(lists, key=attrgetter('distance'))for i in range(edgeNumber):temp = lists[i]From = temp.Fromto = temp.todistance = temp.distanceunion(From, to, distance)print(result)

ok!大功告成了,如果你有更好的方法,可以在评论区交流哦!

CCF CSP 数据中心 c++ python csp201812_4 100分相关推荐

  1. CCF CSP 数据中心

    题目 试题编号: 201812-4 试题名称: 数据中心 时间限制: 1.0s 内存限制: 512.0MB 问题描述: 样例输入 4 5 1 1 2 3 1 3 4 1 4 5 2 3 8 3 4 2 ...

  2. CCF CSP 201609-2 火车购票(C++语言100分)[简单模拟题]

    1. 问题链接:CCF 201609-2 火车购票 试题编号: 201609-2 试题名称: 火车购票 时间限制: 1.0s 内存限制: 256.0MB 问题描述: 问题描述 请实现一个铁路购票系统的 ...

  3. CCF CSP 20190901 小明种苹果 100分 解法/思想

    近几天正在疯狂的刷CCF题,写博客主要是做一下笔记以及记录一下自己的心得,如果能够帮到大家那就再好不过了,废话不多说,咱们放题: [题目概述] 小明(小明事情真多)要种苹果,需要疏果把不好的苹果去掉, ...

  4. CCF CSP 201609-2 火车购票 C++实现 100分

    问题描述 请实现一个铁路购票系统的简单座位分配算法,来处理一节车厢的座位分配. 假设一节车厢有20排.每一排5个座位.为方便起见,我们用1到100来给所有的座位编号,第一排是1到5号,第二排是6到10 ...

  5. # CSP 201609-2 火车购票购买(100分)

    试题编号: 201612-2 试题名称: 火车购票 时间限制: 1.0s 内存限制: 256.0MB 问题描述 请实现一个铁路购票系统的简单座位分配算法,来处理一节车厢的座位分配. 假设一节车厢有20 ...

  6. 201609-2 python CCF 更简单的思路和代码 100分

    n = int(input()) ps = list(map(int, input().split())) #p的list seats = [list(range(i,i + 5)) for i in ...

  7. CCF CSP——202206-5 PS无限版(70分题解)

    问题描述 试题链接:PS无限版 70分题解 题目还是比较简单的,不至于长篇大论的(读题都累死人了),此题要求:有一系列的点(用数组保存下来),编号(1,2--,n),对编号[l,r]的点进行不同的7个 ...

  8. csp第二题火车购票c++100分运行0ms(不会见缝插针的同学只能拿90分)

    这是个水题但是大部分人就只拿到90分 why? 我分析了一下大家应该是有一种情况没有考虑 所有车厢内没连续座位则应该安排在编号最小的几个空位,这个小细节应该是大部分人没注意到的 就是兄弟们没有见缝插针 ...

  9. 制冷量系数(CCF)在数据中心的价值

    目前数据中心的平均制冷量几乎是IT热负荷的四倍.本文将介绍一个简单实用的标准――制冷量系数(CCF),说明如何通过将CCF作为参照衡量数据中心冷却效率的标准,从而实现降低成本的机会. 制冷量系数(CC ...

  10. 西门子绿色数据中心的“新”境界

    迈进北京西门子数据中心机房的大门,展现在眼前的不是宽敞明亮的机房和一排排整齐的机柜,而是一扇扇紧闭的房门,这让记者感到有些困惑:难道这就是数据中心吗? "这是采取分舱式设计理念建造的数据中心 ...

最新文章

  1. MySQL升级教程(CentOS)
  2. 图解MySql命令行创建存储过程
  3. perl大骆驼和小骆驼_骆驼路线的主/从故障转移
  4. ios6.0,程序为横屏,出现闪退
  5. javascript正则表达式复习
  6. java 奇数 字符乱码_socket中文奇数个出现乱码的解决办法
  7. mysql条件变量单引号_mysql语法
  8. 李沐-动手学深度学习
  9. 矩形波的傅里叶级数及代码
  10. 大会没看够?2021 Google 开发者大会总结看这里!
  11. 智商情商哪个重要_智商or情商 哪个对孩子更重要
  12. ps-抠头发-选择并遮住工具
  13. 360手机怎样更新系统版本android,360手机N5迎来安卓7.0稳定版升级
  14. 【078】Town Scaper-创造属于自己的唯美水上小镇
  15. 关于系统安装之U盘制作【install.wim】
  16. 300多张精美京剧脸谱,收藏~~
  17. 部分软件安装界面出现乱码(奇形怪状的问号等)/文件打开异常/文件无法正常打开
  18. windows电脑系统优化
  19. 微信小程序开发基础(03视图与逻辑)
  20. R的一些常用函数【基于尚学堂的部分总结】

热门文章

  1. 如何快速设计一款万能遥控器产品原型(SoC免开发)
  2. 访问限制:由于对必需的库E:\j2sdk\jre\lib\rt.jar具有一定限制,因此无法访问类型JFrame
  3. java 2 sdk下载_Java 2 SDK Standard Edition官方版
  4. CSS border设置虚线可调节虚线间距
  5. 鼎立测试软件速率在哪里看,鼎力测试软件中参数详解.docx
  6. 如何监控网页卡顿和崩溃?
  7. 关于计算机四级网络工程师的考试
  8. 火车头采集的数据库文件*.bd3是什么格式的数据库?
  9. 1. 测度论-概率空间的基本概念
  10. c语言的二维数组的指针访问,用指针访问二维数组