【思维】hdu 6060 RXD and dividing

数组模拟链表:http://blog.csdn.net/baidu_35643793/article/details/76850687



RXD and dividing

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


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    and for all different i,j , we can conclude that 
Then he calulates
He wants to maximize the res.
1≤k≤n≤106
the cost of each edge∈[1,1e5]
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各节点的树,现将节点2-n分为k组,定义每组的权值为该组内所有点加编号为1的节点相互连接所经过的边的权值之和,求k组点集最大的和;

思路:思考方向:每条边的贡献,(发现这个关于贡献的思考方向很重要 )

题解思路;

dif ( i ) : i 节点子树内不同的标号数目;

sz( i ) : i 节点子树的大小;

(x,fax) : 节点x和它的父亲所组成的边;


代码:

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;

typedef long long ll;

const int N=2001000;

int head[N+10];

struct node
{
    int v,w;
    int next;
} edge[N+10];

int n,m,cnt=0;
ll ans=0;

void add_edge(int from,int to,int val)  //创建邻接表
{
    edge[++cnt].v=to;
    edge[cnt].w=val;
    edge[cnt].next=head[from];
    head[from]=cnt;
}

int dfs(int fa,int x,int val)  //dfs求各子树的大小;
{
    int sum=1;
    for(int i=head[x];i;i=edge[i].next)
    {
        int to=edge[i].v;
        int val=edge[i].w;
        if(to!=fa)
            sum+=dfs(x,to,val);
    }
    if(sum>m)
        ans+=(ll)m*val;
    else
        ans+=(ll)sum*val;
    return sum;
}

int main()
{
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        cnt=0;
        memset(head,0,sizeof(head));
        for(int i=1; i<=n-1; i++)
        {
            int u,v,w;
            scanf("%d%d%d",&u,&v,&w);
            add_edge(u,v,w);
            add_edge(v,u,w);
        }

        ans=0;
        dfs(0,1,0);  //
        printf("%I64d\n",ans);
    }
    return 0;
}


参考:http://blog.csdn.net/your_eyes_see_sea/article/details/76578077、http://blog.csdn.net/kkkkahlua/article/details/76562559



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值