HDU 6060 RXD and dividing(贪心,思维)
来源:互联网 发布:mac必下的软件 编辑:程序博客网 时间:2024/06/07 05:44
RXD and dividing
Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 524288/524288 K (Java/Others)
Total Submission(s): 441 Accepted Submission(s): 179
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
题目分析:首先要知道对于根节点为1的斯坦纳树,其最小值就是每一个结点直接到1的距离之和,排除同一集合中相同路径。那么我们就很清楚了。这道题要分为K个块,对于同一个块来说,肯定点越分散越好。那么怎样做到最分散呢?即:对于每一颗子树,其下的点分布在
difi≤min(k,szi), szi表示子树大小.就可以取得最分散的情况。然后dfs一下就好咯。
代码如下:
/* Author:kzl */#include<iostream>#include<cstring>#include<algorithm>#include<cstdio>#include<vector>using namespace std;typedef long long LL;const int maxx = 1e6+10;struct My{int to,val;My(){}My(int a,int b):to(a),val(b){}};vector<My>ve[maxx];int n,k;int aa,bb,cc;LL sum = 0;int dfs(int num,int p){ int ans = 0; int sz = ve[num].size(); for(int i=0;i<sz;i++){ int v = ve[num][i].to; if(v==p)continue; int x = dfs(v,num); ans += x; sum += 1LL * (min(k,x)) * ve[num][i].val; //cout<<num<<" "<<k<<" "<<x<<" "<<ve[num][i].val<<" "<<sum<<endl; } //cout<<num<<" "<<ans+sz<<endl; return ans + 1 ;}int main(){while(scanf("%d%d",&n,&k)!=EOF){ for(int i=0;i<=n;i++)ve[i].clear(); for(int i=1;i<n;i++){ scanf("%d%d%d",&aa,&bb,&cc); ve[aa].push_back(My(bb,cc)); ve[bb].push_back(My(aa,cc)); } sum = 0;dfs(1,0); printf("%lld\n",sum);}return 0;}
difi≤min(k,szi), szi表示子树大小
阅读全文
0 0
- HDU 6060 RXD and dividing(贪心,思维)
- HDU 6060 RXD and dividing(思维)
- hdu 6060 RXD and dividing (贪心)
- HDU 6060 RXD and dividing 思维 + dfs
- 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(树形DP+贪心)
- RXD and dividing(HDU 6060)
- 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
- RStudio连接Oracle数据库
- cgo
- 百度地图api路况信息
- mysql创建存储过程
- HTML+SSM + MySQL的简单登录(三)
- HDU 6060 RXD and dividing(贪心,思维)
- 依赖注入(DI)和控制反转(IOC)
- unity学习日记:音频可视化
- 常见压缩工具和算法的压缩比率及资源消耗情况
- uva 11490
- Hibernate连接MySQL数据
- hdu2686 最大费用流
- Spring Data Redis -----笔记4
- RG_5