题目链接:  HDU 4118 Holiday's Accommodation

分析: 可以知道每条边要走的次数刚好的是这条边两端的点数的最小值的两倍。

代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<stack>
using namespace std;
const int maxn=100000+10; struct node{
int to, dix, next;
}tree[maxn<<1];
int head[maxn],g[maxn],ptr;
bool vis[maxn]; void Init(){
ptr=1;
memset(vis,false,sizeof(vis));
memset(head,-1,sizeof(head));
}
void AddEdge(int a,int b,int c){
tree[ptr].to=b;
tree[ptr].dix=c;
tree[ptr].next=head[a];
head[a]=ptr++;
}
void DFS(){
vis[1]=true;
stack<int>M;
M.push(1);
int rt=head[1];
while(true){
if(rt==-1){
int a=M.top(); M.pop();
if(M.empty()) break;
g[M.top()]+=g[a];
}
rt=head[M.top()];
while(rt!=-1){
if(!vis[tree[rt].to]){
vis[tree[rt].to]=true;
M.push(tree[rt].to);
break;
}
rt=tree[rt].next;
}
}
}
int main(){
int T,cas=1;
scanf("%d",&T);
while(T--){
Init();
int n; scanf("%d",&n);
for(int i=1;i<n;++i){
int a,b,c;
scanf("%d%d%d",&a,&b,&c);
AddEdge(a,b,c);
AddEdge(b,a,c);
g[i]=1;
}
g[n]=1;
DFS();
__int64 ans=0;
for(int i=1;i<ptr;i+=2){
int m=min(g[tree[i].to],g[tree[i+1].to]);
ans+=2*min(n-m,m)*(__int64)tree[i].dix;
}
printf("Case #%d: %I64d\n",cas++,ans);
}
return 0;
}

HDU 4118 树形DP Holiday's Accommodation的更多相关文章

  1. hdu 4118 树形dp

    思路:其实就是让每一条路有尽量多的人走. #pragma comment(linker, "/STACK:1024000000,1024000000") #include<m ...

  2. hdu 4123 树形DP+RMQ

    http://acm.hdu.edu.cn/showproblem.php? pid=4123 Problem Description Bob wants to hold a race to enco ...

  3. HDU 1520 树形dp裸题

    1.HDU 1520  Anniversary party 2.总结:第一道树形dp,有点纠结 题意:公司聚会,员工与直接上司不能同时来,求最大权值和 #include<iostream> ...

  4. HDU 1561 树形DP入门

    The more, The Better Time Limit: 6000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Oth ...

  5. HDU 2196树形DP(2个方向)

    HDU 2196 [题目链接]HDU 2196 [题目类型]树形DP(2个方向) &题意: 题意是求树中每个点到所有叶子节点的距离的最大值是多少. &题解: 2次dfs,先把子树的最大 ...

  6. HDU 1520 树形DP入门

    HDU 1520 [题目链接]HDU 1520 [题目类型]树形DP &题意: 某公司要举办一次晚会,但是为了使得晚会的气氛更加活跃,每个参加晚会的人都不希望在晚会中见到他的直接上司,现在已知 ...

  7. codevs 1380/HDU 1520 树形dp

    1380 没有上司的舞会 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 钻石 Diamond 题解 查看运行结果 回到问题 题目描述 Description Ural大学有N个职员 ...

  8. HDU 5834 [树形dp]

    /* 题意:n个点组成的树,点和边都有权值,当第一次访问某个点的时候获得利益为点的权值 每次经过一条边,丢失利益为边的权值.问从第i个点出发,获得的利益最大是多少. 输入: 测试样例组数T n n个数 ...

  9. hdu 4267 树形DP

    思路:先dfs一下,找出1,n间的路径长度和价值,回溯时将该路径长度和价值清零.那么对剩下的图就可以直接树形dp求解了. #include<iostream> #include<al ...

随机推荐

  1. jquery选择器的使用方式

    1.基本选择器   选择器 描述 返回 示例 代码说明 1 id选择器 根据指定的id匹配元素 单个元素 $("#one").css("background", ...

  2. 八、C# 值类型

    结构.枚举.装箱.拆箱 自定义值类型 如何利用结构来定义新的值类型,并使之具有与大多数预定义 类型相似的行为,这里的关键在于,任何 新定义的值类型都有它们自己的数据和方法. 一般用枚举来定义常量值集合 ...

  3. IOI1994 北京2008的挂钟 迭代加深

    总的来讲,这是一道很⑨的题,因为: (1)题目中有⑨个挂钟 (2)有⑨种操作方案 (3)这题因为解空间太小所以可以直接⑨重循环!! 这题可以用迭代加深搜索高效求解,剪枝的策略也很显然: >所求的 ...

  4. 334. Increasing Triplet Subsequence My Submissions Question--Avota

    问题描述: Given an unsorted array return whether an increasing subsequence of length 3 exists or not in ...

  5. IOS DLNA开发(CyberLink和PlatinumKit)

    1.CyberLink 和 PlatinumKit 两者的比较 CyberLink大概在2010年之后功能就没有更新,部分功能不够完善,网上有下载地址 http://www.pudn.com/down ...

  6. 【POJ2886】【线段树】Who Gets the Most Candies?

    Description N children are sitting in a circle to play a game. The children are numbered from 1 to N ...

  7. CentOS6.5升级为CentOS7.0

    CentOS6.5升级为CentOS7.0 CentOS6.5升级为CentOS7 升级前: [root@localhost ~]# cat /proc/version  Linux version ...

  8. PHPMailer发匿名邮件及Extension missing: openssl的解决

    原文链接:http://www.tongfu.info/phpmailer%E5%8F%91%E5%8C%BF%E5%90%8D%E9%82 %AE%E4%BB%B6%E5%8F%8Aextensio ...

  9. Java线程运行轨迹-代码追踪与定位

    今天在写程序时,想到一个问题,当我的程序出异常的时候,控制台输出信息中,明确指出了出现异常的位置,并详细列举了函数的调用层次关系,它是怎么做到的. 竟然想到了这个问题,就去查看了源代码,不过没点几下, ...

  10. JS和jQuery获取节点的兄弟,父级,子级元素

    原文转自http://blog.csdn.net/duanshuyong/article/details/7562423 先说一下JS的获取方法,其要比JQUERY的方法麻烦很多,后面以JQUERY的 ...