HDU 6060 RXD and dividing【DFS】
来源:互联网 发布:2016年ac尼尔森数据 编辑:程序博客网 时间:2024/06/05 19:46
RXD and dividing
Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 524288/524288 K (Java/Others)Total Submission(s): 1344 Accepted Submission(s): 569
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
对于每一个子树,将子树中的每一个节点分到不同的集合的时候,所需要的边权是最大的,所以考虑每一课子树它的节点数和k的大小,这个值就是这个子树连接它父亲的边的经过的总次数。
问题的实质就是转化为求每个点的子孙节点个数,然后算出每条边要重复计算的次数即可。
#include<iostream>#include<cstdio>#include<cstring>#include<string>#include<cmath>#include<queue>#include<stack>#include<vector>#include<map>#include<set>#include<algorithm>using namespace std;#define ll long long#define ms(a,b) memset(a,b,sizeof(a))#define maxn 510const int M=1e6+10;const int inf=0x3f3f3f3f;const int mod=1e9+7;const double eps=1e-10;ll n,m,k;struct node{ int x,val;};vector<node>v[M];ll d[M],sz[M];void dfs(int u,int fa){ sz[u]=1; for(int i=0;i<v[u].size();i++){ node cur=v[u][i]; if(cur.x==fa)continue; d[cur.x]=cur.val; dfs(cur.x,u); sz[u]+=sz[cur.x]; }}int main(){ while(~scanf("%lld%lld",&n,&k)){ for(int i=0;i<n-1;i++){ int a,b,c; scanf("%d%d%d",&a,&b,&c); v[a].push_back(node{b,c}); v[b].push_back(node{a,c}); } dfs(1,-1); ll ans=0; for(int i=2;i<=n;i++) ans+=d[i]*min(k,sz[i]); printf("%lld\n",ans); for(int i=1;i<=n;i++)v[i].clear(); } return 0;}
#include<iostream>#include<cstdio>#include<cstring>#include<string>#include<cmath>#include<queue>#include<stack>#include<vector>#include<map>#include<set>#include<algorithm>using namespace std;#define ll long long#define ms(a,b) memset(a,b,sizeof(a))#define maxn 510const int M=1e7+10;const int inf=0x3f3f3f3f;const int mod=1e9+7;const double eps=1e-10;int n,m,k;struct Edge{int v,nxt,w;}edge[M*2];int tot,head[M],sz[M];ll ans;void add(int u,int v,int w){edge[tot]=(Edge){v,head[u],w};head[u]=tot++;edge[tot]=(Edge){u,head[v],w};head[v]=tot++;}void dfs(int u,int fa){sz[u]=1;for(int i=head[u];~i;i=edge[i].nxt){int v=edge[i].v,w=edge[i].w;if(v==fa)continue;dfs(v,u);sz[u]+=sz[v];ll tol=sz[v]<k?sz[v]:k;ans+=tol*w;}}int main(){while(~scanf("%d%d",&n,&k)){ ms(head,-1); tot=ans=0; for(int i=1;i<n;i++){ int v,u,c; scanf("%d%d%d",&u,&v,&c); add(u,v,c); } dfs(1,-1); printf("%lld\n",ans);} return 0;}
阅读全文
1 0
- HDU 6060 RXD and dividing 思维 + dfs
- 【HDU 6060 RXD and dividing】+ DFS
- HDU 6060 RXD and dividing【DFS】
- hdu 6060 RXD and dividing(dfs)
- -----dfs+思维 hdu 6060-RXD and dividing
- hdu 6060 RXD and dividing (dfs)
- hdu 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
- 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
- bzoj3996[TJOI2015]线性代数 网络流
- 为什么需要DTO(数据传输对象)(转载)
- [ Activiti ] Spring 整合Activiti工作流开发
- Activity启动模式(launchMode)详解
- PHP中+与array_merge()的区别
- HDU 6060 RXD and dividing【DFS】
- JAVA中的字符串及常用方法
- 写在前面,白话 Yaf 探秘与深入
- 学习OpenCL开发(一)架构
- hook
- [转载]设计支持第三方登录方式的数据表
- Linux线程同步机制一--互斥锁mutex
- java项目配置dll,so动态库文件方式
- 【c#】控制台程序的显示和隐藏