多校第三场 1005 HDU 6060 思维贪心+dfs+一维建树
来源:互联网 发布:薛之谦和李雨桐 知乎 编辑:程序博客网 时间:2024/05/21 23:32
RXD and dividing
Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 524288/524288 K (Java/Others)Total Submission(s): 1353 Accepted Submission(s): 244
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
#include<iostream>#include<algorithm>#include<cstdio>#include<cstring>#include<vector>#include<map>#include<string>#include<cmath>#include<vector>#define maxlen 1000000+7using namespace std;vector<pair<int, int> >nextpoint[maxlen];bool vis[maxlen];/*考虑每条边对答案的贡献,很容易可以知道能用到这条边的最多的生成树是从这条边往下的点的数量,即每个下面的点都是独立的点集的时候。但是由于最多只有k个集合 所以答案就是w[i]*min(k,sz[i])对i求和建树用vector+pair建统计下面节点数量用dfs + vis数组注意结果中间运算用long long维护*/int n, k;long long ans;int dfs(int x){int cnt = 1;for(int i = 0; i < nextpoint[x].size(); i++){int newpos = nextpoint[x][i].first;if(vis[newpos])continue;vis[newpos] = 1;int amount = dfs(newpos);//cout<<amount<<endl;ans += (long long)min(amount, k) * (long long)nextpoint[x][i].second;cnt += amount;}return cnt;}int main(){//freopen("test.txt","r",stdin);while(scanf("%d %d", &n, &k) != EOF){for(int i = 1; i <= n; i++)nextpoint[i].clear();memset(vis, 0, sizeof vis);ans = 0;for(int i = 0; i < n - 1; i++){int x;int y;int val;scanf("%d %d %d", &x, &y, &val);nextpoint[x].push_back(pair<int, int>(y, val));nextpoint[y].push_back(pair<int, int>(x, val));}vis[1]=1;dfs(1);printf("%lld\n", ans);}return 0;}
阅读全文
0 0
- 多校第三场 1005 HDU 6060 思维贪心+dfs+一维建树
- hdu 6060 RXD and dividing 2017多校第三场第五题(思维+dfs)
- hdu6060 一次就懂 dfs+思维 多校第三场1005
- 2017多校第一场 HDU 6044 Limited Permutation 思维,计数,DFS
- HDU 多校第三场
- HDU 6060-RXD and dividing(多校训练第三场->DFS)
- 多校第三场 1006 hdu 5323 Solve this interesting problem(dfs)
- hdu多校第三场 Solve this interesting problem (5323 线段树模拟 + DFS)
- hdu5762(2016多校第三场,思维题)
- hdu6058 链表+思维 2017多校第三场1003
- hdu 4628 Pieces 多校第三场
- hdu 5317 多校第三场
- 2016多校第三场 HDU 5755
- [dfs]多校联合第三场 K Work
- 2017多校训练赛第三场 HDU 6058 (组合计数+思维)
- 【2017多校第二场】HDU 6075 Questionnaire【思维】
- HDU 6098 Inversion (思维 17多校第六场)
- 多校第十场 hdu 6180 Schedule (multiset)/(思维)
- 销售类网站必须具备三大特性
- 《JavaScript高级程序设计》读书笔记二:在HTML中使用JavaScript
- 华为OJ——成绩排序
- window下MySql定时弹出Installer Console的窗口
- java程序计算两个大整数相乘
- 多校第三场 1005 HDU 6060 思维贪心+dfs+一维建树
- android开发板
- HDU 3579 扩展中国剩余定理
- Cocos2d-x3.2 Animate帧动画
- MyBatis学习总结
- Java并发主要操作
- 走进java_虚拟机体系结构
- java开发中的异常总结
- UI设计中最常用的颜色