2017 Multi-University Training Contest - Team 3 - 1005 HDU 6060

[转载于家神](http://blog.csdn.net/sdnuzsj/article/details/76595523)

RXD and dividing

Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 524288/524288 K (Java/Others)
Total Submission(s): 1242    Accepted Submission(s): 524



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 SiSj=.
Then he calulates res=ki=1f({1}Si).
He wants to maximize the res.
1kn106
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 n1 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 n100.
 

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,k;下面输入图的n-1条信息分别点点编号,边,然后让你把除了根节点1的所有点分成k个部分,让你求每一部分与根节点所形成的最小斯坦纳树的和最大。

题目思路:官方题解:把1看成整棵树的根.问题相当于把 2∼n的每个点一个 [1,k]的标号.然后根据最小斯坦纳树的定义, (x,fax)这条边的贡献是以
x为根 子树内不同标号的个数目 difi.那么显然有 difi≤min(k,szi) szi表示子树大小.可以通过构造让所有 difi都取到最大值.所以答案就是 ∑x=2nw[x][fax]∗min(szx,k)时间复杂度 O(n).
我解释一下官方题解中的各个表达式的含义。 (x,fax)(x, fa_x)代表的是x节点与其父节点的边,w…..代表的是x节点与它父节点形成的边的长度,szx代表的是以x为根的子树的大小, k代表的就是题目中的k。我们让除了根节点即编号为1的节点之外的点编号(1,k)要想使得最后的结果最大,要使每一条边使用的次数最多,就等价于每一个子树中的所有的节点编号尽可能的不同,那么从最外求的的是最大的每
一次求的都是最大的那么最后求的结果就是最大的。

感觉类似于贪心。
AC代码:

  1. #include<iostream>  
  2. #include<cstdio>  
  3. #include<cstring>  
  4. #include<cmath>  
  5. #include<algorithm>  
  6. using namespace std;  
  7. typedef long long LL;  
  8. const int INF=0x3f3f3f3f;  
  9. const LL MOD=1e9+7;  
  10. const int MAXN=1e6+7;  
  11. int head[MAXN],tot;  
  12. int sz[MAXN],weight[MAXN];  
  13.   
  14. struct Edge  
  15. {  
  16.     int from,to,cost,nxt;  
  17. }e[MAXN*2];  
  18.   
  19. void addedge(int u,int v,int w)  
  20. {  
  21.     e[tot].from=u;  
  22.     e[tot].to=v;  
  23.     e[tot].cost=w;  
  24.     e[tot].nxt=head[u];  
  25.     head[u]=tot++;  
  26. }  
  27.   
  28. void dfs(int u,int fa)  
  29. {  
  30.     sz[u]=1;  
  31.     for(int i=head[u];i!=-1;i=e[i].nxt)  
  32.     {  
  33.         int to=e[i].to;  
  34.         if(to==fa)  continue;  
  35.         weight[to]=e[i].cost;  
  36.         dfs(to,u);  
  37.         sz[u]+=sz[to];  
  38.     }  
  39. }  
  40.   
  41. int main()  
  42. {  
  43.     int n,k;  
  44.     while(scanf(“%d%d”,&n,&k)!=EOF)  
  45.     {  
  46.         memset(sz,0,sizeof(sz));  
  47.         memset(head,-1,sizeof(head));  
  48.         tot=0;  
  49.         int u,v,w;  
  50.         for(int i=1;i<=n-1;++i)  
  51.         {  
  52.             scanf(”%d%d%d”,&u,&v,&w);  
  53.             addedge(u,v,w);  
  54.             addedge(v,u,w);  
  55.         }  
  56.         dfs(1,-1);  
  57.         LL ans=0;  
  58.         for(int i=2;i<=n;i++)  
  59.         {  
  60.             ans+=(LL)weight[i]*min(sz[i],k);  
  61.         }  
  62.         printf(”%lld\n”,ans);  
  63.     }  
  64.     return 0;  
  65. }  
 
 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值