Uva 1664 Conquer a New Region (Kruskal化用)
来源:互联网 发布:星星知我心演员 编辑:程序博客网 时间:2024/06/05 01:03
该题巧妙的运用了并查集,运用了类似于最小生成树算法的过程 ,通过该题可以对并查集有一个更深的理解 。
题目:由于i和j唯一通路上容量的最小值为该两点的容量,求一个点到其他所有点的容量最大值 。
显然题目叙述与MST相似。问题是在合并集合时,中心城市的选择。
首先,解决两点的容量问题 ,我们将所有边从大到小排序,然后从大到小枚举,我们假设根结点就是要找的城市中心点,那么当又加入一条边时,该边的两个顶点所在的集合设为A、B,集合A、B的顶点a、b,要让谁当中心点呢? 易知:无论谁当中心点,它与另一个集合中任一点的容量都为该边长度(因为是从大到小枚举的)。
那么为了求出总容量,我们要维护一些值,用空间换时间 。 维护每个顶点的总容量sum[i],维护每个顶点与之相连的顶点数量,cnt[i],当前答案ans 。
那么对于a、b点,如果以a为中心,总容量为sum[a] + cnt[b] * e[i].c 。 反之亦然,哪个量大,则以哪个点为城市中心,也就是并查集的根结点 。
该题的巧妙之处在于,将答案结点维护成并查集的根结点,快速的找出一个集合中的城市中心 。
并查集用了路径压缩之后其实已经很快了,没有必要在改变树的高度,因为那样会改变根结点,不仅写起来麻烦,还丢掉了许多很好的特性 。
该题就是通过这些特性(根节点不变时),维护一些重要的量以达到快速求解的目的 。 请读者细细品味 。
通过并查集进行不连续多源遍历,最后合并成为整体
参考:http://blog.csdn.net/weizhuwyzc000/article/details/47957359
#include<iostream>#include<queue>#include<cmath>#include<cstring>#include<vector>#include<algorithm>using namespace std;typedef long long LL; //int不够,WA.....const int maxn = 200010, INF = 10000000; //需改struct Edge{ LL from, to, dist; Edge(LL u, LL v, LL w) :from(u), to(v), dist(w) {}};bool cmp(const Edge &i, const Edge &j) //间接排序函数//注意定义顺序{ return i.dist > j.dist;}struct Kruskal{ int n, m; vector<Edge> edges; int p[maxn]; LL cnt[maxn];//每个顶点与之相连的顶点数(直接或间接) LL cap[maxn];//每个顶点的总容量 void init(int n) { this->n = n; edges.clear(); memset(p, 0, sizeof(p));//不需要? } void AddEdge(LL u, LL v, LL w) { edges.push_back(Edge(u, v, w)); m = edges.size(); } int find(int x) //并查集的查找 { return p[x] == x ? x : p[x] = find(p[x]); } LL kruskal() { LL ans = 0; for (int i = 0; i <= n; i++) //注意循环中止==情况是否需要 { p[i] = i; //初始化并查集 cnt[i] = 1; } memset(cap, 0, sizeof(cap)); sort(edges.begin(), edges.end(), cmp); for (int i = 0; i < m; i++) { int x = find(edges[i].from); int y = find(edges[i].to); cap[x] += cnt[y] * edges[i].dist; //集合之一的根节点与另一个集合中任一点的容量都为该边长度 cap[y] += cnt[x] * edges[i].dist; if (cap[x] > cap[y]) { p[y] = x; ans = cap[x]; cnt[x] += cnt[y]; } else //if (cap[y] > cap[x]) //注意不要落==情况 { p[x] = y; ans = cap[y]; cnt[y] += cnt[x]; } } return ans; }}K;int main(){ int n; while (cin >> n) { int a, b, c; K.init(n); for(int i=1;i<n;i++) { cin >> a >> b >> c; K.AddEdge(a, b, c); } cout << K.kruskal() << endl; //别忘写输出..... }}
0 0
- Uva 1664 Conquer a New Region (Kruskal化用)
- UVA - 1664 Conquer a New Region
- UVA 1664(p382)----Conquer a New Region
- UVA 1664 Conquer a New Region
- UVA 1664 - Conquer a New Region(并查集)
- Conquer a New Region UVA
- UVa 1664 (Conquer a New Region) 并查集
- UVA 1664 Conquer a New Region (并查集+贪心)
- UVA 1664 Conquer a New Region (并查集+贪心)
- zoj3659 Conquer a New Region
- HDU4424--Conquer a New Region
- hdu4424 Conquer a New Region
- Conquer a New Region HDU
- Conquer a New Region HDU
- Conquer a New Region HDU
- Conquer a New Region HDU
- Conquer a New Region HDU
- hdu 4424 Conquer a New Region(并查集)
- mybatis 报错
- UGUI背包实现详解之三——UGUI中如何代码实现背包精灵物品互换拖拽效果
- webview加载不出数据解决方案
- 排序算法之 插入排序
- Genymotion报错An error occured while deploying the file
- Uva 1664 Conquer a New Region (Kruskal化用)
- Shiro入门-rememberMe
- 面试题
- C++语言学习路线
- youbbs的安装与配置
- Python 列表解析
- tensorflow 手写图像验证
- 基于android的网络音乐播放器-播放音乐及收藏音乐的效果展示(四)
- jsf的一个实例