17 多校 3 - 1005 - RXD and dividing (HDU 6060)

21 篇文章 0 订阅

RXD and dividing

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


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
 

题意:
1、把2, 3, 4, 5, ... , n-1, n 分为k份分别放入S1,S2, ... , Sk k个集合中,集合可以为空。
2、求各个集合中的点和点1互相连通所需要的花费之和的最大值,将一个集合中的点与点1相连所产生的花费要尽量少。

就是把n-1个点分开然后求各自的最小生成树,并且使其和最大。

题目中说明给的图只有n-1条边,并且所有点都已经连通了,所以该图就是一个最小生成树,从1到其余任一点之间都只有一条路径。
因为要使和尽量大,所以1到a点和b点的路径若有重合的话就要尽量把它们分开在两个集合中,这样才能使重合的路径多算几次,所以就可以用贪心的思想,就是同一条路径下连接的点要尽量分开在不同的集合中,然后这条路径就可以用min(路径下点的个数,k)次,最后求和就可以得到答案了。

对边进行操作比对点要麻烦些,所以就改成边连接下的第一个点,该点的值就为边的值,点的个数就是其子孙节点个数加上其自身。

#include<iostream>
#include<string.h>
#include<stdio.h>
#include<vector>
using namespace std;
#define LL long long
const LL N = 1e6 + 1000;
LL n,k,use[N],pre_cost[N],ans,vis[N];
struct node
{
    int to,cost;
    node(int a,int b)
    {to = a,cost = b;}
};
vector<node>e[N];
int dfs(int pos,int pre)
{
    use[pos] = 1,pre_cost[pos] = pre,vis[pos] = 1;
    for(int i=0;i<e[pos].size();i++)
    {
        int to = e[pos][i].to,cost = e[pos][i].cost;
        if(vis[to]) continue;
        use[pos]+=dfs(to,cost);
    }
    return use[pos];
}
void init()
{
    memset(vis,0,sizeof vis);
    memset(use,0,sizeof use);
    memset(pre_cost,0,sizeof pre_cost);
    for(int i=0;i<=n;i++)
        e[i].clear();
}
int main()
{
    while(scanf("%lld%lld",&n,&k)!=EOF)
    {
        init();
        for(int i=1;i<n;i++)
        {
            int a,b,c;
            scanf("%d%d%d",&a,&b,&c);
            e[a].push_back(node(b,c));
            e[b].push_back(node(a,c));//邻接表建图
        }
        dfs(1,0);//计算每条路径下有多少个点
        LL ans = 0;
        for(int i=2;i<=n;i++)
            ans += pre_cost[i]*min(use[i],k);
        printf("%lld\n",ans);
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值