题目链接:  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. linq学习笔记:将List<T> 转换为 Dictionary<T Key,T Value>

    运用Linq,将List<T> 转换为 Dictionary<T Key,T Value> 即:List<T>  ToDictionary<T Key,T V ...

  2. 推荐Asp.net WebApi入门教程

    Web API 强势入门指南; Web API 入门指南 - 闲话安全; 实例快速上手 -ASP.NET 4.5新特性WebAPI从入门到精通; Asp.net WebApi 项目示例(增删改查).

  3. linux常用编辑器

    管理员在进行系统操作的时候,不可避免地会对文本进行修改,如进行各种服务程序配置文件的改动,使程序对用户提供不同的服务效果.在本章我们向大家介绍Linux上常见的编辑器ed.vi.emacs,同时以vi ...

  4. oracle数组学习资料

    --oracle数组,所谓数组就是  字段的 个数,数组应该很有用 --可变数组 declare  type v_ar is varray(10) of varchar2(30);   my_ar v ...

  5. cocos2d中如何使用图片纹理图集的加载来实现一个动画的功能

    cocos2d中要实现一个动画,一般采用纹理图集的方式,也就是说把几个连续动作的图片挨个显示切换这样就是动画 一: 首先先看下今天要实现的具体的目的,打飞机的时间屏幕上会有一个喷火的小飞机,飞机的尾部 ...

  6. 【HDU4391】【块状链表】Paint The Wall

    Problem Description As a amateur artist, Xenocide loves painting the wall. The wall can be considere ...

  7. U盘美化(更换U盘logo和页面背景软件)

    U盘内新建txt文本后,输入 [autorun] ICON=ooopic_1459309050.ico 保存的文件名包括后缀更改为autorun.inf 必须为icon图标

  8. 【Linux】vi编辑器命令

       1)进入vi的命令 vi filename :             打开或新建文件,并将光标置于第一行首 vi +n filename :          打开文件,并将光标置于第n行首 ...

  9. memcache 操作类

    <?php /** * memcache 操作实现 * @author timeless */ class Memcache_manage { //CI原始的信息 private $_ci; p ...

  10. 优化MYSQL FILESORT

    用Explain分析SQL语句的时候,经常发现有的语句在Extra列会出现Using filesort,根据mysql官方文档对他的描述: 引用 MySQL must do an extra pass ...