hdu6060 RXD and dividing 贪心
/**
题目:hdu6060 RXD and dividing
链接:http://acm.hdu.edu.cn/showproblem.php?pid=6060
题意:贪心
给定一颗树,n个节点,编号为1~n。将2~n编号的节点分成k份。
每一份分别和编号1的节点取并集。然后求每一份的节点连通的最小边权和;
然后k份获得的边权和加起来;问:求可以获得的k份边权和的总和的最大值。 思路:通过画树容易发现,假如k无穷大,如果节点x为根的子树有num个节点,那么x与x的父节点相连的那条边权最多加num次。 所以每个节点x与父节点相连的边的权值w的贡献为min(num,k)*w; num为以x为根的子树的总结点数。 */
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<map>
#include<vector>
#include<queue>
#include<set>
#include<cstring>
#include<time.h>
#include<random>
#include<cmath>
using namespace std;
typedef pair<int,int> P;
typedef long long LL;
const int mod = 1e9+;
const int INF = 0x3f3f3f3f;
const int maxn = 1e6+;
int n, k;
vector<P> G[maxn];
struct node
{
int sum;
int w;
}t[maxn];
void dfs(int r,int f)
{
int len = G[r].size();
t[r].sum = ;
for(int i = ; i< len; i++){
if(G[r][i].first==f) continue;
dfs(G[r][i].first,r);
t[G[r][i].first].w = G[r][i].second;
t[r].sum += t[G[r][i].first].sum;
}
}
int main()
{
while(scanf("%d%d",&n,&k)==)
{
for(int i = ; i <= n; i++) G[i].clear();
int u, v, w;
for(int i = ; i < n; i++){
scanf("%d%d%d",&u,&v,&w);
G[u].push_back(P(v,w));
G[v].push_back(P(u,w));
}
dfs(,-);
LL ans = ;
for(int i = ; i <= n; i++){
ans = (ans+(LL)min(t[i].sum,k)*t[i].w);
}
printf("%lld\n",ans);
}
return ;
}
hdu6060 RXD and dividing 贪心的更多相关文章
- 2017 Multi-University Training Contest - Team 3 hdu6060 RXD and dividing
地址:http://acm.split.hdu.edu.cn/showproblem.php?pid=6060 题目: RXD and dividing Time Limit: 6000/3000 M ...
- HDU-6060 RXD and dividing
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6060 多校的题目,每次只能写两道SB题,剩下的要么想不到,要么想到了,代码不知道怎么实现,还是写的 ...
- HDU 6060 RXD and dividing(dfs 思维)
RXD and dividing Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 524288/524288 K (Java/Other ...
- HDU 6060 - RXD and dividing | 2017 Multi-University Training Contest 3
/* HDU 6060 - RXD and dividing [ 分析,图论 ] | 2017 Multi-University Training Contest 3 题意: 给一个 n 个节点的树, ...
- HDU 6060 17多校3 RXD and dividing(树+dfs)
Problem Description RXD has a tree T, with the size of n. Each edge has a cost.Define f(S) as the th ...
- 2017ACM暑期多校联合训练 - Team 3 1005 RXD and dividing
题目链接 Problem Description RXD has a tree T, with the size of n. Each edge has a cost. Define f(S) as ...
- 2017 Multi-University Training Contest - Team 3 RXD and dividing(树)
题解: 其实贪心地算就可以了 一个最优的分配就是每条边权贡献的值为min(k, sz[x]),sz[x]是指子树的大小 然后最后加起来就是答案. #include <iostream> # ...
- HDU 6060 RXD and dividing(思维+计算贡献值)
http://acm.hdu.edu.cn/showproblem.php?pid=6060 题意: 给定一棵 n 个节点的树,1 为根.现要将节点 2 ~ n 划分为 k 块,使得每一块与根节点形成 ...
- HDU 6060 RXD and dividing(LCA)
[题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=6060 [题目大意] 给一个n个节点的树,要求将2-n号节点分成k部分, 然后将每一部分加上节点1, ...
随机推荐
- Java笔记17:导出可执行jar包
这里介绍Java程序的两种导出可执行jar包的方式,一种是图形界面打包,另一种是命令行打包. 一.图形界面打包 1 建立名为JarDemo的JavaProject,在src目录下建立com.abc的P ...
- java程序计算数独游戏
兴趣来了,写了个简单的数独游戏计算程序,未做算法优化. 通过文件来输入一个二维数组,9行,每行9个数组,数独游戏中需要填空的地方用0来表示.结果也是打印二维数组. import java.io.Fil ...
- 2017.12.27 sqlSessionFactory和sqlSession(to be continued)
参考来自:<深入浅出MyBatis技术原理与实践-第6章 > 1.SqlSessionFactory SqlSessionFactory是一个接口,最重要的功能是提供SqlSession. ...
- has been modified since the precompiled header地图错误
添加高德地图的一个错误 has been modified since the precompiled header 解决方案: xcode---product----clean 清理重新编译一下就 ...
- Android常用酷炫控件(开源项目)github地址汇总
转载一个很牛逼的控件收集帖... 第一部分 个性化控件(View) 主要介绍那些不错个性化的 View,包括 ListView.ActionBar.Menu.ViewPager.Gallery.Gri ...
- 【转】maven常见问题问答
转自:http://www.iteye.com/topic/973166 前言 Maven,发音是[`meivin],"专家"的意思.它是一个很好的项目管理工具,很早就进入了我的必 ...
- IOS Exception 1(libc++abi.dylib: terminating with uncaught exception of type NSException)
2014-08-05 22:18:46.455 SwiftUI[1329:40871] -[_TtC7SwiftUI14MViewControler clickMe]: unrecognized se ...
- H5 Canvas | 画布中变量作用域与setInterval方法同步执行探究
Demo - 随机绘制圆环 实现思路: 将一个圆环的绘制分成100份,setInterval()方法定义每隔时间n绘制一段新的,每份的开始路径都是上一次的结束路径,实现步进绘制. 通过Math.ran ...
- Getting started with Chrome Dev Editor
转自:https://github.com/GoogleChrome/chromedeveditor/blob/master/doc/GettingStarted.md Installation In ...
- 欧洲顶级音频播放软件AIMP
http://zmingcx.com/europes-top-software-aimp-audio-player.html 音频播放软件众多,耳熟能详的Winamp.Foobar2000.千千静听. ...