http://acm.hdu.edu.cn/showproblem.php?pid=4424

【题目大意】

给你N个点和N-1条边的连通图,也就是说任意两点间的路径是唯一的。每条边有个权值,从一点到另一点的最大流量是路径中所有边中权值的最小值。要你找一个点,使得这点到剩下所有点的最大流量之和最大,求这个最大流量和。

【分析1:动态规划 】

假设现在有两个点的集合S1和S2,添加的一条边可以使得这两个集合连通起来,要求最大的流量之和,那么只有两种情况:1、选取的中心点在S1中;2、选取的中心点在S2中。设S中其最小权值的边为E,它连接S1、S2,假设中心点在S1,那么S2中所有的点要连接到中心点必然要通过E这条边过去,且这条边是短板;中心点在S2也类似,所以可以得到如下的DP方程:

DP[S]=max{DP[S1]+e.c*count[S2] , DP[S2]+e.c*count[S1] }  (e.c为边的权值,count为集合中点的数量)

可以用搜索的方法来求对于每层枚举找到最小的边E,再递归下去,直到集合只含一个点,这是种逆向求解的想法,从整体到局部再反推回答案。

【分析2:并查集 & 贪心】

动态规划的方法虽然正确,复杂度也还算可以,但是复杂,涉及到集合的运算和从集合中遍历元素等等...      所以有种贪心的想法:既然每次都是取最小的边,那么随着递推下去,取的边构成的数列是递增有序的,那为什么不直接先把边排序好呢?既然在动态规划中涉及到集合的分裂,那为什么不反过来用集合的合并来求解?这两个是可以同时做到的。

所以可以得到以下做法: 把边从大到小排序,依次选取边的两点所在的集合来合并,并更新集合的元素个数和当前的最大和,用并查集就好了。这种方法其实是动态规划的逆向求解,但本质还是动态规划。

【代码】

 #include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
int n;
long long sum[];
int mset[];
struct egde
{
int a,b,c;
void read(){scanf("%d%d%d",&a,&b,&c);}
bool operator<(const egde& t)const {return c>t.c;}
}data[];
int find(int x)
{
if (mset[x]<) return x;
return mset[x]=find(mset[x]);
}
void uion(int a,int b,int c)
{
int aa=find(a);
int bb=find(b);
long long sum1=sum[aa]+(long long)c*-mset[bb];
long long sum2=sum[bb]+(long long)c*-mset[aa];
if (sum2>sum1) {swap(aa,bb);swap(a,b);swap(sum1,sum2);}
mset[aa]+=mset[bb];
mset[bb]=aa;
sum[aa]=sum1;
}
int main()
{
while (~scanf("%d",&n))
{
memset(sum,,sizeof sum);
memset(mset,-,sizeof mset);
for (int i=;i<n-;++i) data[i].read();
sort(data,data+n-);
for (int i=;i<n-;++i)
uion(data[i].a,data[i].b,data[i].c);
printf("%I64d\n",sum[find()]);
}
}

HDU 4424 Conquer a New Region的更多相关文章

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

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

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

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

  3. HDU 4424 Conquer a New Region 最大生成树

    给你一颗树 每条边有一个权值 选择一个点为中心 定义S值为中心到其它n-1个点的路径上的最小边权 求全部点S值的和 从大到小排序 每次合并2棵树 设为A集合 B集合 设A集合的最大S值的和为sumA ...

  4. HDOJ 4424 Conquer a New Region

    并检查集合 侧降序,每增加一个侧面应该推断,其中基本建设方..... Conquer a New Region Time Limit: 8000/4000 MS (Java/Others)    Me ...

  5. 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 ...

  6. zoj 3659 Conquer a New Region The 2012 ACM-ICPC Asia Changchun Regional Contest

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

  7. ZOJ3659 Conquer a New Region 并查集

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

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

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

  9. hdu 4424 并查集

    思路:将边从大到小排序,判断向哪边连,能使总和最大. #include<map> #include<set> #include<cmath> #include< ...

随机推荐

  1. 《dive into python3》 笔记摘录

    1.list can hold  arbitrary  objects and can expand dynamically as new items are added. A list is an  ...

  2. How to bind to data when the DataContext is not inherited【项目】

    http://www.thomaslevesque.com/2011/03/21/wpf-how-to-bind-to-data-when-the-datacontext-is-not-inherit ...

  3. 【AS3 Coder】任务七:初涉PureMVC——天气预报功能实现

    转自:http://www.iamsevent.com/post/36.html AS3 Coder]任务七:初涉PureMVC——天气预报功能实现 使用框架:AS3任务描述:了解PureMVC框架使 ...

  4. 如何提高数据库update更新的速度

    不用不知道,一用吓一跳..看下面这条SQL语句 String sql="update cats set name_alias='"+rs.getString(1)+"'w ...

  5. Chrysler -- CCD (Chrysler Collision Detection) Data Bus

    http://articles.mopar1973man.com/general-cummins/34-engine-system/81-ccd-data-bus CCD (Chrysler Coll ...

  6. Thinkphp的Volist标签

    Volist标签主要用于在模板中循环输出数据集或者多维数组. volist标签(循环输出数据) 闭合 非闭合标签 属性 name(必须):要输出的数据模板变量 id(必须):循环变量 offset(可 ...

  7. Java数据结构之树和二叉树(2)

    从这里始将要继续进行Java数据结构的相关讲解,Are you ready?Let's go~~ Java中的数据结构模型可以分为一下几部分: 1.线性结构 2.树形结构 3.图形或者网状结构 接下来 ...

  8. Codeforces 427 D. Match &amp; Catch

    后缀数组.... 在两个串中唯一出现的最小公共子串 D. Match & Catch time limit per test 1 second memory limit per test 51 ...

  9. [CentOS]CentOS/RedHat/Fedora的Proxy设定(yum,wget,,rpm)

    yum 「/etc/yum.conf」 proxy=http://proxy.xxx.com:8080/ wget 「/etc/wgetrc」 http_proxy=http://proxy.xxx. ...

  10. Codeforces Round #180 (Div. 2) B. Sail 贪心

    B. Sail 题目连接: http://www.codeforces.com/contest/298/problem/B Description The polar bears are going ...