这道题感觉思路非常巧妙, 我是看了别人的博客才想明白的。

这里用到了并查集, 以根节点为中心城市, 然后把边从大到小排序, 每次的当前的边即为容量,

因为是目前的最小值, 然后去算总的容量, 每次选容量大的点作为新的根节点, 也就是新的

中心城市。细节见代码。

#include<cstdio>
#include<algorithm>
#define REP(i, a, b) for(int i = (a); i < (b); i++)
using namespace std; typedef long long ll;
const int MAXN = 212345;
struct node
{
ll u, v, w;
bool operator < (const node& rhs) const
{
return w > rhs.w;
}
}Edge[MAXN];
ll sum[MAXN], cnt[MAXN];
int f[MAXN], n; int find(int x)
{
if(f[x] != x)
f[x] = find(f[x]);
return f[x];
} int main()
{
while(~scanf("%d", &n))
{
REP(i, 1, n + 1) sum[i] = 0, cnt[i] = 1, f[i] = i;
REP(i, 0, n - 1) scanf("%lld%lld%lld", &Edge[i].u, &Edge[i].v, &Edge[i].w);
sort(Edge, Edge + n - 1); ll ans = 0;
REP(i, 0, n - 1)
{
int u = find(Edge[i].u), v = find(Edge[i].v), w = Edge[i].w;
ll sumu = sum[u] + cnt[v] * w, sumv = sum[v] + cnt[u] * w;
if(sumu < sumv) f[u] = v, ans = sumv, cnt[v] += cnt[u], sum[v] = sumv;
else f[v] = u, ans = sumu, cnt[u] += cnt[v], sum[u] = sumu;
} printf("%lld\n", ans);
} return 0;
}

紫书 习题11-11 UVa 1644 (并查集)的更多相关文章

  1. 紫书 习题 11-9 UVa 12549 (二分图最小点覆盖)

    用到了二分图的一些性质, 最大匹配数=最小点覆盖 貌似在白书上有讲 还不是很懂, 自己看着别人的博客用网络流写了一遍 反正以后学白书应该会系统学二分图的,紫书上没讲深. 目前就这样吧. #includ ...

  2. 紫书 习题 11-8 UVa 1663 (最大流求二分图最大基数匹配)

    很奇怪, 看到网上用的都是匈牙利算法求最大基数匹配 紫书上压根没讲这个算法, 而是用最大流求的. 难道是因为第一个人用匈牙利算法然后其他所有的博客都是看这个博客的吗? 很有可能-- 回归正题. 题目中 ...

  3. 紫书 习题8-12 UVa 1153(贪心)

    本来以为这道题是考不相交区间, 结果还专门复习了一遍前面写的, 然后发现这道题的区间是不是 固定的, 是在一个范围内"滑动的", 只要右端点不超过截止时间就ok. 然后我就先考虑有 ...

  4. 紫书 习题8-7 UVa 11925(构造法, 不需逆向)

    这道题的意思紫书上是错误的-- 难怪一开始我非常奇怪为什么第二个样例输出的是2, 按照紫书上的意思应该是22 然后就不管了,先写, 然后就WA了. 然后看了https://blog.csdn.net/ ...

  5. 紫书 习题 10-4 UVa 1644(素数筛)

    素数筛没什么好说的 #include<cstdio> #include<vector> #include<cstring> #define REP(i, a, b) ...

  6. UVA 1594 Ducci Sequence(紫书习题5-2 简单模拟题)

    A Ducci sequence is a sequence of n-tuples of integers. Given an n-tuple of integers (a1, a2, · · · ...

  7. 紫书 习题7-8 UVa 12107 (IDA*)

    参考了这哥们的博客 https://blog.csdn.net/hyqsblog/article/details/46980287  (1)atoi可以char数组转int, 头文件 cstdlib ...

  8. 紫书 习题 11-10 UVa 12264 (二分答案+最大流)

    书上写的是UVa 12011, 实际上是 12264 参考了https://blog.csdn.net/xl2015190026/article/details/51902823 这道题就是求出一种最 ...

  9. 紫书 习题 10-13 UVa 11526(打表找规律+分步枚举)

    首先看这道题目,我预感商数肯定是有规律的排列的,于是我打表找一下规律 100 / 1 = 100 100 / 2 = 50  100 / 3 = 33  100 / 4 = 25  100 / 5 = ...

随机推荐

  1. easyUI在使用字符串拼接时样式不起作用,点击加号增加一行,点击减号删除一行效果。

    拼接的按钮没有样式,需要使用 var str = $("<a href='javascript:void(0)' class='easyui-linkbutton' onclick=' ...

  2. Oracle-Trigger-Insert tableA and tableB

    create or replace trigger trg_a after insert ON a for each rowbegin   INSERT INTO b values(:NEW.ID,: ...

  3. Extjs toolbar 如何添加竖杆分隔符

    如下: { xtype:'button', text:'学生档案', iconCls:'file', handler:function(){ console.log(222) }, }, {xtype ...

  4. fflush()函数总结

    1. 概述 函数名: fflush() 功 能: 清除读写缓冲区,需要立即把输出缓冲区的数据进行物理写入时 头文件: stdio.h 原型: int fflush(FILE *stream),其中st ...

  5. CF895C Square Subsets (组合数+状压DP+简单数论)

    题目大意:给你一个序列,你可以在序列中任选一个子序列,求子序列每一项的积是一个平方数的方案数. 1<=a[i]<=70 因为任何一个大于2的数都可以表示成几个质数的幂的乘积 所以我们预处理 ...

  6. freeswitch 中文语音

    1.下载中文语音包 链接:https://pan.baidu.com/s/1UODvqj8NAQw7_CRatfl0kg 提取码:qwdn 创建目录 /usr/local/freeswitch/sou ...

  7. [学习笔记] CS131 Computer Vision: Foundations and Applications:Lecture 4 像素和滤波器

    Background reading: Forsyth and Ponce, Computer Vision Chapter 7 Image sampling and quantization Typ ...

  8. SpringBoot下支付宝接口的使用

    SpringBoot下支付宝接口的使用 前期准备: 参考之前写过的 支付宝接口引入servlet版本 Jar包引入: <!-- 支付宝 --> <dependency> < ...

  9. IE9以下版本兼容h5标签

    随着html5(后面用h5代表)标签越来越广泛的使用,IE9以下(IE6-IE8)不识别h5标签的问题让人很是烦恼. 在火狐和chrome之类的浏览器中,遇到不认识的标签,只要给个display:bl ...

  10. [terry笔记]python购物程序

    如下是一个购物程序: 先输入工资,显示商品列表,购买,quit退出,最后格式化输出所买的商品. count = 0 while True: #做一个循环判断,如果输入的不是数字,基于提示,三次后退出 ...