CCF CSP 数据中心 c++ python csp201812_4 100分
2019-02-25 23:36
866 查看
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; } @Override public 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 = From self.to = to self.distance = distance father = [] result = 0 def init(number): for i in range(number): father.append(i) def findFather(x): a = x while x != father[x]: x = father[x] while a != father[a]: z = father[a] father[a] = x a = z return x def union(From, to, distance): global result fx = findFather(From) fy = findFather(to) if fx != fy: father[fx] = fy result = 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.From to = temp.to distance = temp.distance union(From, to, distance) print(result)
ok!大功告成了,如果你有更好的方法,可以在评论区交流哦!
相关文章推荐
- CCF CSP 跳一跳 c++ java python 201803_1 100分
- CCF CSP 卖菜 Java python csp201809_1 100分
- CCF CSP 再卖菜 c++ java python 201809_4
- CCF CSP 碰撞的小球 java python 201803_2 100分
- CCF CSP 棋局评估 java python 201803_4 100分
- CCF CSP 买菜 Java python csp201809_2 100分
- CCF CSP 201509-2 日期计算(Java-100分)
- CCF Python题解(100分)201503-2 数字排序
- CCF Python题解(100分)201503-1 图像旋转
- 【CCF CSP】 201612-2 工资计算(100分)
- CCF CSP 201712-1 最小差值(Java-100分)
- python ccf 题解历年100分(更新中)
- CCF CSP 201609-2 火车购票(Java-100分)
- python ccf 认证考试 第二题--公共钥匙盒 测试全对(0分-100分的差距在哪?)
- CCF CSP 公共钥匙盒 JAVA 201709-2 100分
- CCF Python题解(100分)201709-3 JSON查询
- CCF Python题解(100分)201709-2 公共钥匙盒
- CCF Python题解(100分)201412-2 Z字形扫描
- CCF Python题解(100分)201512-1 数位之和
- ccf csp考试201609-1最大波动答案(python实现)