2017杭电多校联赛第三场-RXD and dividing (hdu6060)最小生成树
来源:互联网 发布:unity3d人物控制脚本 编辑:程序博客网 时间:2024/06/05 04:53
RXD and dividing
Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 524288/524288 K (Java/Others)Total Submission(s): 807 Accepted Submission(s): 339
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个节点的树,要求将2-n号节点分成k部分,然后再将每一部分加上1号节点,定义每一部分的val为其中的点在原图上的最小斯坦纳树,问总的val最大可能是多少。
解题思路:我们可以先将1看成整颗树的根,然后将余下的2-n号节点分为k部分,具体怎么分呢,我们可以将每颗子树的子树分为k部分,如果子树大于k的话,否则就分为子树的数量的部分,目的就是要得到尽可能大的总值,因为在算每一部分的值的时候都需要经过这颗子树和它的父亲,所以我们将每颗子树尽可能多的分块,然后将每一块的值相加就可以了。答案是∑x=2nw[x][fax]∗min(szx,k) 。
代码使用dfs写的。
具体看代码:
#include<iostream>#include<cstdio>#include<cstring>#include<cmath>#include<algorithm>using namespace std;typedef long long LL;const int INF=0x3f3f3f3f;const LL MOD=1e9+7;const int MAXN=1e6+7;int head[MAXN],tot;int sz[MAXN],f[MAXN],weight[MAXN];struct Edge{ int from,to,cost,nxt;}e[MAXN*2];void addedge(int u,int v,int w){ e[tot].from=u; e[tot].to=v; e[tot].cost=w; e[tot].nxt=head[u]; head[u]=tot++;}void dfs(int u,int fa){ sz[u]=1; for(int i=head[u];i!=-1;i=e[i].nxt) { int to=e[i].to; if(to==fa) continue; f[to]=u; weight[to]=e[i].cost; dfs(to,u); sz[u]+=sz[to]; }}int main(){ int n,k; while(scanf("%d%d",&n,&k)!=EOF) { memset(sz,0,sizeof(sz)); memset(head,-1,sizeof(head)); tot=0; int u,v,w; for(int i=1;i<=n-1;++i) { scanf("%d%d%d",&u,&v,&w); addedge(u,v,w); addedge(v,u,w); } dfs(1,-1); LL ans=0; for(int i=2;i<=n;i++) { ans+=(LL)weight[i]*min(sz[i],k); } printf("%lld\n",ans); } return 0;}
题目链接:点击打开链接http://acm.hdu.edu.cn/showproblem.php?pid=6060
阅读全文
1 0
- 2017杭电多校联赛第三场-RXD and dividing (hdu6060)最小生成树
- Hdu6060 RXD and dividing(2017多校第3场)
- 2017杭电多校第三场1005 RXD and dividing(最大化k个斯坦纳树分块的最大权值和)HDU6060
- HDU6060-RXD and dividing
- HDU6060 RXD and dividing
- hdu6060 RXD and dividing
- hdu6060 RXD and dividing
- HDU6060-RXD and dividing
- hdu6060 RXD and dividing
- hdu6060 RXD and dividing
- HDU6063 2017杭电多校联赛第三场-RXD and math
- 2017年多校赛第三场 1005 RXD and dividing(DFS)
- 【HDU6060】RXD and dividing(dfs)
- HDU6060 RXD and dividing【DFS】
- 2017杭电多校联赛第三场-RXD and math (hdu6063) 找规律快速幂
- 2017多校训练Contest3: 1005 RXD and dividing hdu6060
- 多校3 hdu6060 RXD and dividing
- hdu 6060 RXD and dividing 2017多校第三场第五题(思维+dfs)
- python进阶强化-5
- 【Linux】bash shell scripts
- Python分词练习
- LeetCode——627. Swap Salary(数据库,sql)
- UVA156 Ananagrams(字符串处理)
- 2017杭电多校联赛第三场-RXD and dividing (hdu6060)最小生成树
- [Unity]调用GL.IssuePluginEvent()后dll中UnityRenderEvent()不执行的问题
- ocp Oracle.1z0-053 711--123
- HDU 3038 带权并查集
- HDU 6051原根 + 欧拉函数
- Python程序打包成exe可执行文件
- Git和SVN切换
- 黑卡地面分辨率
- window.opener的用法