题目链接

Problem Description

RXD has a tree T, with the size of n. Each edge has a cost.

Define f(S) as the the cost of the minimal Steiner Tree of the set S on tree T.

he wants to divide 2,3,4,5,6,…n into k parts S1,S2,S3,…Sk,

where ⋃Si={2,3,…,n} and for all different i,j , we can conclude that Si⋂Sj=∅.

Then he calulates res=∑ki=1f({1}⋃Si).

He wants to maximize the res.

1≤k≤n≤106

the cost of each edge∈[1,105]

Si might be empty.

f(S) means that you need to choose a couple of edges on the tree to make all the points in S connected, and you need to minimize the sum of the cost of these edges. f(S) is equal to the minimal cost

Input

There are several test cases, please keep reading until EOF.

For each test case, the first line consists of 2 integer n,k, which means the number of the tree nodes , and k means the number of parts.

The next n−1 lines consists of 2 integers, a,b,c, means a tree edge (a,b) with cost c.

It is guaranteed that the edges would form a tree.

There are 4 big test cases and 50 small test cases.

small test case means n≤100.

Output

For each test case, output an integer, which means the answer.

Sample Input

5 4

1 2 3

2 3 4

2 4 5

2 5 6

Sample Output

27

题意:

有n个点,其中1为起点,其余的n-1个点(2~n)为我们要到达的点,将剩余的这n-1个点分成k个集合,看一下到这些点的路径的长度。

分析:

最开始做题的时候题意就理解错了,以为只要这k个集合的每个集合都有路径能达到求其中的一条路径就行。后来才知道这样的想法是错误的。

题目让我们求得是从源点1到2~n所有点的距离之和,这是一道关于最小斯坦纳树的问题,斯坦纳树问题的模型就是,比如:

有A,B,C三个村庄,现在要建立一个发电站,要求到这三个村庄的距离和最短,这个问题我们应该都接触过很多次,这就是最原始的斯坦纳树的问题。

现在回归这道题,题上要求我们把2~n这些点分成k部分,那么对于一个节点以及它所有的子节点来说,这一部分最多也只能够被分成k部分,很多人不太理解这一点,我详细解释一下这里的原因。

我们在求1到任意点的路径的时候,如果这个点可以通过以他在同一个集合中的点到达的话,我们就可以直接通过它的父节点到达这个点,而不必要在绕道最开始的节点1.

但是如果一个节点的父节点与它不在同一个集合中,我们要求1到这个节点的距离,就必须从1开始加。

既然让求距离的最大值,那么我们就可以尽可能的把一个节点连带他的所有的子节点分到不同的集合中,这样它前面走过的那一部分才可以近可能的多走几次。但是我们分的集合数肯定不能超过要求分的k部分和节点大小的较小值。

很多人在意到底应该将那一部分分到一个集合里面,其实这个完全没有必要关心,因为不管集合如何分,它们走的路径长度最后都是一样的,这个可以自己画图体会一下。

即一条边(u,father(u))对整个的贡献就相当于这条边的value值乘上k,与size[x](当前情况下x的子节点个数,包括它本身)中的较小者,我们只要遍历所有的边,然后将每条边的值都算出来,最后求和。

代码:

#include<bits/stdc++.h>
using namespace std; const int N=1e6+7;
int head[N],nxt[N*2],v[N*2],w[N*2],sz[N];
int n,k,g[N],ed;
long long ans;
inline void adg(int x,int y,int z)///头插法
{
v[++ed]=y;///v表示的是终点
w[ed]=z;///w表示的是这条边的权重
nxt[ed]=head[x];///xt表示的是钱一条边
head[x]=ed;///头的指向改变
} void dfs(int x,int fa,int val)
{
sz[x]=1;
for(int i=head[x]; i!=0; i=nxt[i])
if(v[i]!=fa)///不是又找到本身这条边了
{
dfs(v[i],x,w[i]);
sz[x]+=sz[v[i]];
// printf("x=%d sz[x]=%d val=%d\n",x,sz[x],val);
}
ans+=1ll*min(k,sz[x])*val;
//printf("ans==%lld\n",ans);
} int main()
{
while(~scanf("%d%d",&n,&k))
{
for(int i=1; i<=n; i++)
head[i]=0;///每一个点的指向都赋初值
ed=0;
ans=0;
for(int i=1;i<n;i++)
{
int x,y,z;
scanf("%d%d%d",&x,&y,&z);
adg(x,y,z),adg(y,x,z);
}
dfs(1,0,0);
printf("%lld\n",ans);
}
return 0;
}

2017ACM暑期多校联合训练 - Team 3 1005 RXD and dividing的更多相关文章

  1. 2017ACM暑期多校联合训练 - Team 9 1005 HDU 6165 FFF at Valentine (dfs)

    题目链接 Problem Description At Valentine's eve, Shylock and Lucar were enjoying their time as any other ...

  2. 2017ACM暑期多校联合训练 - Team 4 1004 HDU 6070 Dirt Ratio (线段树)

    题目链接 Problem Description In ACM/ICPC contest, the ''Dirt Ratio'' of a team is calculated in the foll ...

  3. 2017ACM暑期多校联合训练 - Team 9 1010 HDU 6170 Two strings (dp)

    题目链接 Problem Description Giving two strings and you should judge if they are matched. The first stri ...

  4. 2017ACM暑期多校联合训练 - Team 8 1006 HDU 6138 Fleet of the Eternal Throne (字符串处理 AC自动机)

    题目链接 Problem Description The Eternal Fleet was built many centuries ago before the time of Valkorion ...

  5. 2017ACM暑期多校联合训练 - Team 8 1002 HDU 6134 Battlestation Operational (数论 莫比乌斯反演)

    题目链接 Problem Description The Death Star, known officially as the DS-1 Orbital Battle Station, also k ...

  6. 2017ACM暑期多校联合训练 - Team 8 1011 HDU 6143 Killer Names (容斥+排列组合,dp+整数快速幂)

    题目链接 Problem Description Galen Marek, codenamed Starkiller, was a male Human apprentice of the Sith ...

  7. 2017ACM暑期多校联合训练 - Team 8 1008 HDU 6140 Hybrid Crystals (模拟)

    题目链接 Problem Description Kyber crystals, also called the living crystal or simply the kyber, and kno ...

  8. 2017ACM暑期多校联合训练 - Team 7 1009 HDU 6128 Inverse of sum (数学计算)

    题目链接 Problem Description There are n nonnegative integers a1-n which are less than p. HazelFan wants ...

  9. 2017ACM暑期多校联合训练 - Team 7 1002 HDU 6121 Build a tree (深搜+思维)

    题目链接 Problem Description HazelFan wants to build a rooted tree. The tree has n nodes labeled 0 to n− ...

随机推荐

  1. 奇异值分解(SVD)原理详解及推导 (转载)

    转载请声明出处http://blog.csdn.net/zhongkejingwang/article/details/43053513 在网上看到有很多文章介绍SVD的,讲的也都不错,但是感觉还是有 ...

  2. mysql中(存储)函数

    (存储)函数: 函数,也说成“存储函数”,其实就是js或php中所说的函数! 唯一的区别: 这里的函数必须返回一个数据(值): 定义形式: 注意事项: 1, 在函数内部,可以有各种变量和流程控制的使用 ...

  3. webgl学习笔记五-纹理

    写在前面 建议先阅读下前面我的三篇文章. webgl学习笔记一-绘图单点 webgl学习笔记二-绘图多点 webgl学习笔记三-平移旋转缩放 术语 : 纹理 :图像 图形装配区域 :顶点着色器顶点坐标 ...

  4. Linux服务器ping不通域名出现的unknown host 错误解决办法

    "ping: unknown host www.baidu.com" 解决方法 如果某台Linux服务器ping不通域名, 如下提示: # ping www.baidu.compi ...

  5. 【C++】深度探索C++对象模型读书笔记--关于对象(Object Lessons)

    前言中的内容: 1.什么是C++对象模型? 1.语言中直接支持面向对象程序设计的部分 2. 对于各种支持的底层实现机制 2. C++ class的完整virtual functions在编译时期就固定 ...

  6. Asp.net MVC 获取IPv4 地址

    public static string GetIP4Address() { string IP4Address = String.Empty; foreach (IPAddress IPA in D ...

  7. 51nod-1220-约数之和

    题意 求 \[ \sum _{i=1}^n\sum _{j=1}^nd(ij) \\ d(x)=\sum _{e|x}e \] \(n\le 10^9\) . 分析 没有推出来.这题有几个要点要学习. ...

  8. windows200364位iis6 php环境搭建

    最近接一个小活,就是帮着部署个php网站,服务器是window2003,iis6.之前在我自己得服务器上已经搭建过php环境,区别是我的服务器windows2012,而对方的是windows 2003 ...

  9. [HEOI2014]人人尽说江南好 博弈论

    题面 题面 题解 感觉这题挺神仙的,根据一些奇奇怪怪的证明可以得到: 最后的终止状态一定是\(m, m, m, m, .... n \% m\). 因此我们可以O(1)计算到终止状态所需步数,然后根据 ...

  10. 【BZOJ1458】【洛谷4311】士兵占领(网络流)

    [BZOJ1458][洛谷4311]士兵占领(网络流) 题面 BZOJ权限题,洛谷真好 Description 有一个M * N的棋盘,有的格子是障碍.现在你要选择一些格子来放置一些士兵,一个格子里最 ...