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 ...
随机推荐
- Google的小秘密
google有计算器的功能,例如在google中搜索25*25.lg(13)等,看会出现什么样的结果. http://www.google.com/microsoft 微软风格的入口 http: ...
- bower——库管理工具
bower了解: 随着网页功能的复杂化,各种网页效果的实现,现在单一的一个或两个库文件或许已经不能够满足我们的需要,但当有很多的库文件的时候,手动编辑已经不能胜任,对于引入的库文件而言,往往都是牵一发 ...
- Unity学习笔记(3):获取对象
在上一篇文章中(Unity映射注册)中概要介绍了Unity中的映射机制,本节主要介绍对象获取,包括默认获取,通过名称获取,获取全部对象,同时通过加载配置文件,然后再获取对象. 通过代码获取对象 方式1 ...
- listView异步处理图片下载缓存
package cn.wangmeng.test; import java.io.File;import java.io.FileOutputStream;import java.io.IOExcep ...
- 通过 SuperObject 生成 json string
(* { "name": "Henri Gourvest", /* this is a comment */ "vip": true, &q ...
- sizeof的用法的一些归纳
1 sizeof 是运算符,不是函数 2 sizeof 不能求得void类型的长度,能求得 void*类型的指针的长度 sizeof(void) 会导致编译错误.因为声明一个变量的最重要的作用就是告诉 ...
- Nested Loop,Sort Merge Join,Hash Join
三种连接工作方式比较: Nested loops 工作方式是从一张表中读取数据,访问另一张表(通常是索引)来做匹配,nested loops适用的场合是当一个关联表比较小的时候,效率会更高. Merg ...
- make问题:make[1] entering directory
执行make distclean命令.
- .NET安装和配置Oracle数据访问组件(ODAC)
Many ASP.NET applications access Oracle database for the data source. Oracle supports the .NET with ...
- [译] ASP.NET 生命周期 – ASP.NET 请求生命周期(二)
ASP.NET 请求生命周期 全局应用类也可以用来跟踪每个独立请求的生命周期,包括请求从 ASP.NET 平台传递到 MVC 框架.ASP.NET 框架会创建一个定义在 Global.asax 文件中 ...