https://vjudge.net/problem/UVA-1664

题意:

n个城市形成一棵树,每条边有权值C(i,j)。任意两个点的容量S(i,j)定义为i与j唯一通路上容量的最小值。找一个点,使得它到其他所有点的容量之和最大。

思路:

做法有点类似于最小生成树的Kruskal算法。

先将边按权值从大到小排列,每次新加入一条边,检查边两段A和B所处并查集的根结点,并通过计算得出谁作为中心点时容量更大。计算过程中需要维护一些东西,sum[i]是以i为中心时的容量之和,cnt[i]是以i为根结点时树的结点个数(初始时都是为1)。

下面举例说明:

1 2  2

2 4  1

2 3  1

 #include<iostream>
#include<algorithm>
#include<string>
#include<cstring>
using namespace std; const int maxn = + ; struct node
{
int u, v;
int w;
}t[maxn]; int p[maxn];
long long sum[maxn];
long long cnt[maxn];
int n;
long long ans; bool cmp(node a, node b)
{
return a.w > b.w;
} int find(int x)
{
return x == p[x] ? x : find(p[x]);
} void solve()
{
ans = ;
for (int i = ; i <= n; i++) { p[i] = i; sum[i] = ; cnt[i] = ; }
sort(t, t + n - , cmp);
for (int i = ; i < n - ; i++)
{
int x = find(t[i].u);
int y = find(t[i].v);
long long sum1 = sum[x] + cnt[y] * t[i].w;
long long sum2 = sum[y] + cnt[x] * t[i].w;
if (sum1>sum2)
{
p[y] = x;
cnt[x] += cnt[y];
sum[x] = sum1;
ans = sum[x];
}
else
{
p[x] = y;
cnt[y] += cnt[x];
sum[y] = sum2;
ans = sum[y];
}
}
printf("%lld\n", ans);
} int main()
{
//freopen("D:\\txt.txt", "r", stdin);
while (~scanf("%d", &n))
{
for (int i = ; i < n - ; i++)
scanf("%d%d%d", &t[i].u, &t[i].v, &t[i].w);
solve();
}
return ;
}

UVa 1664 Conquer a New Region(并查集)的更多相关文章

  1. UVA 1664 Conquer a New Region (并查集+贪心)

    并查集的一道比较考想法的题 题意:给你n个点,接着给你n-1条边形成一颗生成树,每条边都有一个权值.求的是以一个点作为特殊点,并求出从此点出发到其他每个点的条件边权的总和最大,条件边权就是:起点到终点 ...

  2. UVA 1664 Conquer a New Region (Kruskal,贪心)

    题意:在一颗树上要求一个到其他结点容量和最大的点,i,j之前的容量定义为i到j的路径上的最小边容量. 一开始想过由小到大的去分割边,但是很难实现,其实换个顺序就很容易做了,类似kruskal的一个贪心 ...

  3. hdu 4424 & zoj 3659 Conquer a New Region (并查集 + 贪心)

    Conquer a New Region Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others ...

  4. ZOJ3659 Conquer a New Region 并查集

    Conquer a New Region Time Limit: 5 Seconds      Memory Limit: 32768 KB The wheel of the history roll ...

  5. hdu4424 Conquer a New Region 并查集/类似最小生成树

    The wheel of the history rolling forward, our king conquered a new region in a distant continent.The ...

  6. ZOJ 3659 & HDU 4424 Conquer a New Region (并查集)

    这题要用到一点贪心的思想,因为一个点到另一个点的运载能力决定于其间的边的最小权值,所以先把线段按权值从大到小排个序,每次加的边都比以前小,然后合并集合时,比较 x = findset(a) 做根或 y ...

  7. zoj 3659 Conquer a New Region(并查集)

    题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=4882 代码: #include<cstdio> #inc ...

  8. hdu 4424 Conquer a New Region (并查集)

    ///题意:给出一棵树.树的边上都有边权值,求从一点出发的权值和最大,权值为从一点出去路径上边权的最小值 # include <stdio.h> # include <algorit ...

  9. UVA 1395 苗条的生成树(最小生成树+并查集)

    苗条的生成树 紫书P358 这题最后坑了我20分钟,怎么想都对了啊,为什么就wa了呢,最后才发现,是并查集的编号搞错了. 题目编号从1开始,我并查集编号从0开始 = = 图论这种题真的要记住啊!!题目 ...

随机推荐

  1. 【Android】Android内存溢出问题---用自行开辟的空间进行对内存管理

    public static Bitmap readBitmap(String path) { BitmapFactory.Options options = new BitmapFactory.Opt ...

  2. Feature Tools 简介

    FeatureTools是2017年9月上线的github项目,是一个自动生成特征的工具,应用于关系型数据. github链接:https://github.com/Featuretools/feat ...

  3. 华硕蓝光刻录机在MAC系统里能用吗?

    答案是刻录功能不能用(没有驱动),但可以当外置光驱用.需要注意的是单单把刻录机插到MAC电脑上是没有反应的,放入光盘后Finder里会出现一个可移动设备.

  4. centos7上搭建ftp服务器(亲测可用)

    1.安装vsftpd 首先要查看你是否安装vsftp [root@localhost /]# rpm -q vsftpd vsftpd-3.0.2-10.el7.x86_64 (显示以上相关信息也就安 ...

  5. Linux的操作系统I2C驱动架构解说

    Linux的操作系统I2C驱动架构解说 发布时间:2006.10.16 04:52 来源:赛迪网技术社区 作者:LoneStar 最近因为工作需要涉及到了I2C总线.虽然我过去用过I2c,但看了 Li ...

  6. Ensure Indexes Fit in RAM

    Ensure Indexes Fit in RAM — MongoDB Manual https://docs.mongodb.com/manual/tutorial/ensure-indexes-f ...

  7. Linux squid 缓存服务器

    一.简介 代理服务器英文全称是Proxy Server,其功能就是代理网络用户去取得网络信息. Squid是一个缓存Internet 数据的软件,其接收用户的下载申请,并自动处理所下载的数据.当一个用 ...

  8. django的crontab

    最近需要考虑如何在django环境中跑定时任务. 这个在  stackoverflow 也有对应的 讨论 , 方法也有不少, 这边简单尝试和总结下. 假设我们现在的定期任务就是睡眠  n 秒, 然后往 ...

  9. 用 node.js 的 hexo 框架搭建一个支持 markdown 的静态博客系统

    1,Hexo如何在线可视化写博客:   可以试试这款插件 hexo-admin. 2,马克飞象:   一个非常好的 markdown 编辑器. 3,Hexo博客文章设置密码的方法: 首先,在Hexo中 ...

  10. HahMap

    HashMap的定义 public class HashMap<K, V> extends AbstractMap<K, V> implements Map<K, V&g ...