Hdu 6060 RXD and dividing【思维】
来源:互联网 发布:反恐数据库外泄 编辑:程序博客网 时间:2024/06/06 00:58
RXD and dividing
Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 524288/524288 K (Java/Others)Total Submission(s): 22 Accepted Submission(s): 6
Problem Description
RXD has a tree T , with the size of n . Each edge has a cost.
Definef(S) as the the cost of the minimal Steiner Tree of the set S on tree T .
he wants to divide2,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 calulatesres=∑ki=1f({1}⋃Si) .
He wants to maximize theres .
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
Define
he wants to divide
where
Then he calulates
He wants to maximize the
Input
There are several test cases, please keep reading until EOF.
For each test case, the first line consists of 2 integern,k , which means the number of the tree nodes , and k means the number of parts.
The nextn−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 meansn≤100 .
For each test case, the first line consists of 2 integer
The next
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
Output
For each test case, output an integer, which means the answer.
Sample Input
5 41 2 32 3 42 4 52 5 6
Sample Output
27
题目大意:
给一颗N个点的树,让你将2~N共n-1个点分到K个集合当中,使得ΣF(1∪Si)的最大。
定义F(集合)表示在原树中,将这个集合中的各个点连通起来最小的边权值花费。
思路:
因为是求ΣF(1∪Si)的最大,所以我们希望从1节点出发,经过的边次数越多越好。
所以我们希望子树中的节点尽量均分给这K个集合,使得这条边尽量的遍历K次即可。
那么对于一条边(u--->v)权值为w,我们能够贡献的最大价值就是w*min(size【v】,k),这里size【v】表示点v子树中点的个数。
Ac代码:
#include<stdio.h>#include<string.h>#include<iostream>using namespace std;#define ll __int64struct node{ int from; int to; int w; int next;}e[2500000];int head[1050000];ll size[1050000];ll ans;int cont;int n,k;void add(int from,int to,int w){ e[cont].to=to; e[cont].w=w; e[cont].next=head[from]; head[from]=cont++;}ll Dfs(int u,int from,ll len){ size[u]=1; for(int i=head[u];i!=-1;i=e[i].next) { int v=e[i].to; ll w=e[i].w; if(v==from)continue; Dfs(v,u,w); size[u]+=size[v]; } ans+=len*min((ll)k,size[u]); return size[u];}int main(){ while(~scanf("%d%d",&n,&k)) { ans=0; cont=0; memset(head,-1,sizeof(head)); memset(size,0,sizeof(size)); for(int i=1;i<=n-1;i++) { int x,y; ll w; scanf("%d%d%I64d",&x,&y,&w); add(x,y,w); add(y,x,w); } Dfs(1,-1,0); printf("%I64d\n",ans); }}
阅读全文
0 0
- HDU 6060 RXD and dividing 思维 + dfs
- Hdu 6060 RXD and dividing【思维】
- HDU 6060 RXD and dividing(思维)
- -----dfs+思维 hdu 6060-RXD and dividing
- 【思维】hdu 6060 RXD and dividing
- HDU 6060 RXD and dividing(贪心,思维)
- HDU 6060 RXD and dividing
- HDU 6060 RXD and dividing
- [HDU]-6060 RXD and dividing
- HDU 6060 RXD and dividing
- HDU-6060 RXD and dividing
- HDU 6060 RXD and dividing
- hdu 6060 RXD and dividing
- HDU 6060 RXD and dividing
- hdu--6060-RXD and dividing
- HDU 6060 RXD and dividing
- hdu-6060-RXD and dividing
- hdu--6060--RXD and dividing
- HTTP常见状态码
- maven自定义catalog创建archetype
- mongoexport和mongoimport
- hdu12823(两凸包之间最短距离)
- HDU 4185 Oil Skimming 二分匹配
- Hdu 6060 RXD and dividing【思维】
- 冒泡排序和插入排序
- 【转】快速搭建react的webpack初始环境
- Javascript整理
- 挑战程序竞赛系列(31):4.5剪枝
- var 有无的区别
- ccf认证窗口
- openvpn配置
- LeetCode oj 21. Merge Two Sorted Lists(分类:链表)(难度:easy)