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): 1253 Accepted Submission(s): 527
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
Source
2017 Multi-University Training Contest - Team 3
题意:
有n个顶点,给你n-1条边,让你在把(2-n)顶点分成k个部分(也可以分成1到k-1个部分),每个部分和树根顶点1组成一颗树,求这k个树的边权制总和最大。
POINT:
n个顶点,和n-1条边,证明给你的树就是最小生成树,那么无论你怎么分,其实都是最小生成树,所以题目里的最小斯坦纳树是没什么用的。
我们要求的是总和最大,我们就让每个边使用次数最多。易得每个边(x和他的父亲节点形成的边)使用次数max=min(k,size[x])。
那么答案就是sum(每个边的权制*min(k,size[x]))。
#include <stdio.h>#include <iostream>#include <string.h>#include <math.h>using namespace std;#define LL long longconst int N = 1e6+5;int pre[N],head[N];struct node{ int v,nxt,w;}len[N<<1];int sum[N];int cnt;void add(int u,int v,int w){ cnt++; len[cnt].v=v; len[cnt].nxt=head[u]; len[cnt].w=w; head[u]=cnt;}void dfs(int u,int p){ sum[u]=1; for(int i=head[u];i!=-1;i=len[i].nxt) { if(len[i].v==p) continue; pre[len[i].v]=len[i].w; dfs(len[i].v,u); sum[u]+=sum[len[i].v]; }}int main(){ int n,k; while(~scanf("%d %d",&n,&k)) { cnt=0; memset(head,-1,sizeof head); memset(sum,0,sizeof sum); memset(pre,0,sizeof pre); for(int i=1;i<n;i++) { int u,v,w; scanf("%d %d %d",&u,&v,&w); add(u,v,w); add(v,u,w); } LL ans=0; dfs(1,-1); for(int i=2;i<=n;i++) { ans+=(LL)(1LL*pre[i]*1LL*min(k,sum[i])); } printf("%lld\n",ans); } }
阅读全文
0 0
- 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
- HDU 6060 RXD and dividing 思维 + dfs
- Hdu 6060 RXD and dividing【思维】
- HDU 6060 RXD and dividing [想法题]
- 数据结构--有序表查找
- LCS 最大公共子序列
- Scrapy+PhantomJS+Selenium动态爬虫
- 使用指向指针的指针创建任意大小的二维数组
- HDU 1251 统计难题
- HDU 6060 RXD and dividing
- win7快捷键大全
- 整数、浮点型在内存中存储
- 用批处理批量替换文件中的某个字符串(亲自验证通过!完美!哈哈哈)
- AndroidStudio 插件集合 进行高效开发
- 动态规划——Warcraft
- 【HDU 1863】畅通工程(基础最小生成树,Kruskal算法)
- 【框架学习】springMVC过滤器与servlet拦截器区别
- mysql中复制表结构的方法小结