题目链接 解法: 初始n个节点,n颗树,每连一条边,减少一棵树。k棵树需要连n-k条边。。。1棵树需要连n-1条边。。。 给每条可以连的边按代价从小到大排个序,然后连n-k条边造k个最小生成树就可以了。 Code: 1 #include <bits/stdc++.h> 2 # define LL l ...
分类:
其他好文 时间:
2020-01-31 19:15:49
阅读次数:
107
给定边权为正的连通图G,找出连接所有顶点的边的最小权值(集)。 Kruskal:步骤:1. 按边权从小到大的顺序遍历边。2. 如果边对应的两个点不在同一连通分量中,则将其相连。否则忽略。贪心 + 并查集,时间复杂度$O(ElogE+E*A(V)+V)(A\ is\ Ackermann)$。 Prim ...
分类:
其他好文 时间:
2020-01-30 23:21:43
阅读次数:
137
#include<iostream> #include<cstring> #include<algorithm> using namespace std; const int N=110; int p[N]; struct edge{ int a; int b; int w; }e[N*N]; in ...
分类:
其他好文 时间:
2020-01-29 14:22:30
阅读次数:
62
#include<iostream> #include<cmath> #include<algorithm> #include<cstdio> using namespace std; const int N=1e5; struct edge{ int a,b; double w; }e[N]; d ...
分类:
其他好文 时间:
2020-01-29 14:01:16
阅读次数:
89
#include<iostream> #include<algorithm> using namespace std; const int N=1e5; struct edge{ int a,b,w; }e[N]; bool cmp(edge a,edge b) { return a.w<b.w; ...
分类:
Web程序 时间:
2020-01-29 12:50:21
阅读次数:
89
求最小生成树常用,因为效率高(Omlgm) 给定一个n个点m条边的无向图,图中可能存在重边和自环,边权可能为负数。 求最小生成树的树边权重之和,如果最小生成树不存在则输出impossible。 给定一张边带权的无向图G=(V, E),其中V表示图中点的集合,E表示图中边的集合,n=|V|,m=|E| ...
分类:
编程语言 时间:
2020-01-28 15:46:00
阅读次数:
65
形似dijsktra算法, 但是不同于dijsktra算法,prim算法是找到当前集合最近的点, 而dij算法是找距离当前起点最近的点 给定一个n个点m条边的无向图,图中可能存在重边和自环,边权可能为负数。 求最小生成树的树边权重之和,如果最小生成树不存在则输出impossible。 给定一张边带权 ...
分类:
编程语言 时间:
2020-01-28 14:10:48
阅读次数:
80
类似于1213取水 可以把空投当作第0个城市 最后将0~n的所有城市跑最小生成树 1 /* 2 Written By StelaYuri 3 */ 4 #include<iostream> 5 #include<algorithm> 6 using namespace std; 7 struct r ...
分类:
其他好文 时间:
2020-01-27 09:43:38
阅读次数:
80
普普通通的最小生成树,写了一遍prim的,所以用一下克鲁斯卡尔 注意输入数据有多组 #include <iostream> #include <cstdio> #include <cstdlib> #include <cstring> #include <algorithm> using names ...
分类:
Web程序 时间:
2020-01-26 22:15:08
阅读次数:
97
一、Prim Prim算法的思想是: 1. 整个顶点集为$V$,初始选一个起点$s$,令集合$u=\{s\}, v=\{\}$; 2. 在集合$u$与集合$V u$中的点组成的边中,选一条权值最小的边$u_0v_0$加入MST,并且将$u_0$加入$u$; 3. 重复直到MST有$n 1$条边或$n ...
分类:
其他好文 时间:
2020-01-26 16:06:05
阅读次数:
101