【思维】hdu 6060 RXD and dividing
来源:互联网 发布:淘宝助理5.7 编辑:程序博客网 时间:2024/06/04 18:24
数组模拟链表:http://blog.csdn.net/baidu_35643793/article/details/76850687
RXD and dividing
Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 524288/524288 K (Java/Others)Total Submission(s): 1660 Accepted Submission(s): 713
RXD has a tree T, with the size of n. Each edge has a cost.
Define f(S) as the the cost of the minimal Steiner Tree of the set S on tree T.
he wants to divide 2,3,4,5,6,…n into k parts S1,S2,S3,…Sk,
where and for all different i,j , we can conclude that
Then he calulates
He wants to maximize the res.
1≤k≤n≤106
the cost of each edge∈[1,1e5]
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
Input
There are several test cases, please keep reading until EOF.
For each test case, the first line consists of 2 integer n,k, which means the number of the tree nodes , and k means the number of parts.
The next n−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 means n≤100.
Output
For each test case, output an integer, which means the answer.
Sample Input
5 4
1 2 3
2 3 4
2 4 5
2 5 6
Sample Output
27
题意:有一个n各节点的树,现将节点2-n分为k组,定义每组的权值为该组内所有点加编号为1的节点相互连接所经过的边的权值之和,求k组点集最大的和;
思路:思考方向:每条边的贡献,(发现这个关于贡献的思考方向很重要 )
题解思路;
dif ( i ) : i 节点子树内不同的标号数目;
sz( i ) : i 节点子树的大小;
(x,fax) : 节点x和它的父亲所组成的边;
代码:
#include<cstdio>#include<cstring>#include<algorithm>using namespace std;typedef long long ll;const int N=2001000;int head[N+10];struct node{ int v,w; int next;} edge[N+10];int n,m,cnt=0;ll ans=0;void add_edge(int from,int to,int val) //创建邻接表{ edge[++cnt].v=to; edge[cnt].w=val; edge[cnt].next=head[from]; head[from]=cnt;}int dfs(int fa,int x,int val) //dfs求各子树的大小;{ int sum=1; for(int i=head[x];i;i=edge[i].next) { int to=edge[i].v; int val=edge[i].w; if(to!=fa) sum+=dfs(x,to,val); } if(sum>m) ans+=(ll)m*val; else ans+=(ll)sum*val; return sum;}int main(){ while(scanf("%d%d",&n,&m)!=EOF) { cnt=0; memset(head,0,sizeof(head)); for(int i=1; i<=n-1; i++) { int u,v,w; scanf("%d%d%d",&u,&v,&w); add_edge(u,v,w); add_edge(v,u,w); } ans=0; dfs(0,1,0); // printf("%I64d\n",ans); } return 0;}
参考:http://blog.csdn.net/your_eyes_see_sea/article/details/76578077、http://blog.csdn.net/kkkkahlua/article/details/76562559
- 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
- URL发送post请求
- 工具类Date SimpleDateFormat Canlendar
- 存储过程
- leetcode--Two Sum
- Total Commander之添加两边同时进行文件夹切换操作
- 【思维】hdu 6060 RXD and dividing
- C++工程中使用caffe
- leetcode--Add Two Numbers
- 第一章课后的几个编程题——数据结构与问题描述Java语言描述(第3版)
- Java 中的堆和栈
- color/box-flex/transition/inline-block/outline/:not()
- dapper-fastcrud
- 携程酒店自动化360度质量保障体系
- 感染力的六个原则