Power Station POJ 4045
题意:给你一棵树,让你求一点,使该点到其余各点的距离之和最小。如果这样的点有多个,则按升序依次输出。
树型dp
#include <cstdio>
#include <cstring>
#include <vector>
#include <set>
using namespace std;
const int maxn=50010;
typedef __int64 LL;
vector<int>tree[maxn];// to save the relation
LL f[maxn],g[maxn],dp[maxn];//f[u] u as root to all his sons' distance g[u]the number of u'sons
set<int> myqueue;
void dfs(int u,int pa){
/* 以1为根,所有的子树到他们的子节点的和 */
if(tree[u].size() == 1 && u != 1){
g[u]=1;
f[u]=0;
return;
}
for(int v=0;v < tree[u].size();v++){
if(tree[u][v]!=pa){
dfs(tree[u][v],u);
g[u]+=g[tree[u][v]];//the son's sons' number
f[u]+=f[tree[u][v]]+g[tree[u][v]];
}
}
g[u]++;// himself
}
void dfs2(int u,int pa){// to sum the way from his father
//再考虑从父节点来的
if(tree[u].size()==1 && u!=1){
dp[u]=dp[pa]+g[1]-(g[u]<<1);
return;
}
for(int v=0;v<tree[u].size();v++){
if(tree[u][v]!= pa){
//到某节点的距离的和=该节点子树的距离和(在dfs1中获得)+从父亲那一支子树获得的和(此时,父节点那一支看成子树)。dp[儿子]=dp[父节点]-dp[儿子]-g[儿子](儿子到父节点这条路被减了子树的子节点数的次数) + dp[儿子]+(g[1]-g[儿子])(父树上的所有节点)
dp[tree[u][v]]=dp[u]+g[1]-(g[tree[u][v]]<<1);
dfs2(tree[u][v],u);//先算了之后再跑子树
}
}
}
int main(){
int t;
int n,I,R,a,b;
scanf("%d",&t);
LL mmin;
while(t--){
scanf("%d%d%d",&n,&I,&R);
for(int i=0;i<=n;i++){
tree[i].clear();
}
for(int i=2;i<=n;i++){
scanf("%d%d",&a,&b);
tree[a].push_back(b);
tree[b].push_back(a);
}
// for(int i=0;i<=n;i++){
// for(int j=0;j<tree[i].size();j++){
// printf("%d ",tree[i][j]);
// }
// printf("\n");
// }
memset(f,0,sizeof(f));
memset(g,0,sizeof(g));
dfs(1,-1);
dp[1]=f[1];
dfs2(1,-1);
mmin=dp[1];
myqueue.clear();
myqueue.insert(1);
for(int i=2;i<=n;i++){
if(dp[i]<mmin){
mmin=dp[i];
myqueue.clear();
myqueue.insert(i);
}
if(dp[i]==mmin) myqueue.insert(i);
}
//warning : output long long should be I64d
printf("%I64d\n",I*I*R*mmin);//use set not num but the op
for(set<int>::iterator it=myqueue.begin();it!=myqueue.end();++it){
printf("%d ",*it);
}
printf("\n\n");
}
}
Power Station POJ 4045的更多相关文章
- Power Network (poj 1459 网络流)
Language: Default Power Network Time Limit: 2000MS Memory Limit: 32768K Total Submissions: 23407 ...
- Power Network - poj 1459 (最大流 Edmonds-Karp算法)
Time Limit: 2000MS Memory Limit: 32768K Total Submissions: 24788 Accepted: 12922 Description A ...
- Building a Space Station POJ - 2031
Building a Space Station POJ - 2031 You are a member of the space station engineering team, and are ...
- Power Strings POJ - 2406
Power Strings POJ - 2406 时限: 3000MS 内存: 65536KB 64位IO格式: %I64d & %I64u 提交 状态 已开启划词翻译 问题描述 Gi ...
- poj - 4045 - Power Station
题意:一棵有n个结点的树,要取其中的一个结点,使得该结点到其他所有结点的距离和dis最小,即损耗I * I * R * dis最小,输出最小损耗和该结点(有多个的话按结点编号从小到大输出)(3 < ...
- ( KMP 求循环节的个数)Power Strings -- poj -- 2406
链接: http://poj.org/problem?id=2406 Power Strings Time Limit:3000MS Memory Limit:65536KB 64bi ...
- (最小生成树) Building a Space Station -- POJ -- 2031
链接: http://poj.org/problem?id=2031 Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 6011 ...
- Building a Space Station POJ 2031 【最小生成树 prim】
http://poj.org/problem?id=2031 Description You are a member of the space station engineering team, a ...
- Power Strings (poj 2406 KMP)
Language: Default Power Strings Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 33205 ...
随机推荐
- dorado问题查询&快捷键重命名
重命名还有一个快捷键 F2 有关dorado的问题可以进 www.bsdn.org提问,而且更好
- ArcSDE for oracle10g安装后post的时候出现错误
The Post Installation Setup can not locate required Oracle files in your path.Check your Oracle inst ...
- The C in C++
1 unnamed arguments in the argument list of the function definition (Page 114) In c++, an argument m ...
- linux 安装mysql后修改密码出现问题
新安装的mysql 执行命令时候出现错误: 一 错误信息: ERROR 1045 (28000): Access denied for user 'mysql'@'localhost' (using ...
- javascript 代码优化工具 UglifyJS
安装: 1. 安装 node.js 环境 (这个不用我教了吧,网上教程一大堆哦.) 2. 进入 https://github.com/mishoo/UglifyJS 右上角 “Download” Z ...
- 简单风格 在线音乐播放器(支持wav,MP3等)
找了两天终于找到了,支持wav,MP3,其他格式没有测试. 1.修复了jQuery判断ie的bug, 2.修复播放循环 下载地址: http://pan.baidu.com/s/1o6upwHs
- js分割文件快速上传
<?php header('Content-type:text/html;charset=UTF-8'); ?> <?php if ($_FILES) { if(!file_exis ...
- hdu 4679 Terrorist’s destroy 树形DP
链接:http://acm.hdu.edu.cn/showproblem.php?pid=4679 题意:给定一颗树,每条边有一个权值w,问切掉哪条边之后,分成的两颗树的较大的直径*切掉边的权值最小? ...
- Xubuntu 安装mentohust
对于路由器上网到用户来说,自动分配IP上网。 对于校园网用户,首先下载mentohust_0.3.4-1_i386.deb,双击安装程序 然后在命令窗口中输入sudo -s 密码:user来获得roo ...
- 基于C#利用金山取词组件实现屏幕取词功能
这个程序在网上有很多例子,近期要做的项目中有和这个有某些一点点相似的地方,就练练,发现在本机上(Win 7 64位)不能实现其功能,可能原因是API组件太老了吧,毕竟金山大佬公布他的组件是2005年, ...