题意:

给出一棵边带权的树,求距离最近的一对叶子。

分析:

通过DFS计算出\(min(u)\):以\(u\)为根的子树中最近叶子到\(u\)的距离。

然后维护一个前面子树\(v_i\)中叶子到\(u\)距离的最小值,就可以用这个最小值+当前子树中叶子到\(u\)的最短距离来更新答案。

如果根节点也是叶子节点的话,再用\(min(root)\)更新一下答案。

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std; const int maxn = 10000 + 10;
const int INF = 0x3f3f3f3f; struct Edge
{
int v, w, nxt;
Edge() {}
Edge(int v, int w, int nxt): v(v), w(w), nxt(nxt) {}
}; int ecnt, head[maxn];
Edge edges[maxn * 2]; void AddEdge(int u, int v, int w) {
edges[ecnt] = Edge(v, w, head[u]); head[u] = ecnt++;
edges[ecnt] = Edge(u, w, head[v]); head[v] = ecnt++;
} int n; int ans;
int minv[maxn];
int child[maxn]; void dfs(int u, int p) {
minv[u] = INF;
int minp = INF;
child[u] = 0;
for(int i = head[u]; ~i; i = edges[i].nxt) {
int v = edges[i].v;
if(v == p) continue;
child[u]++;
int w = edges[i].w;
dfs(v, u);
minv[u] = min(minv[u], minv[v] + w);
ans = min(ans, minp + minv[v] + w);
minp = min(minp, minv[v] + w);
}
if(!child[u]) minv[u] = 0;
} int main()
{
while(scanf("%d", &n) == 1 && n) {
ecnt = 0;
memset(head, -1, sizeof(head));
for(int i = 1; i < n; i++) {
int u, v, w; scanf("%d%d%d", &u, &v, &w);
AddEdge(u, v, w);
} ans = INF;
dfs(1, 0);
if(child[1] == 1) ans = min(ans, minv[1]);
printf("%d\n", ans);
} return 0;
}

HDU 3848 CC On The Tree 树形DP的更多相关文章

  1. HDU 3848 CC On The Tree(树形dp)

    http://acm.hdu.edu.cn/showproblem.php?pid=3848 题意: 求一棵树上两个叶子结点之间的最短距离. 思路: 两个叶子节点之间一定会经过非叶子节点,除非只有两个 ...

  2. 熟练剖分(tree) 树形DP

    熟练剖分(tree) 树形DP 题目描述 题目传送门 分析 我们设\(f[i][j]\)为以\(i\)为根节点的子树中最坏时间复杂度小于等于\(j\)的概率 设\(g[i][j]\)为当前扫到的以\( ...

  3. HDU 1520.Anniversary party 基础的树形dp

    Anniversary party Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others ...

  4. HDU 5682 zxa and leaf 二分 树形dp

    zxa and leaf 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5682 Description zxa have an unrooted t ...

  5. hdu6035 Colorful Tree 树形dp 给定一棵树,每个节点有一个颜色值。定义每条路径的值为经过的节点的不同颜色数。求所有路径的值和。

    /** 题目:hdu6035 Colorful Tree 链接:http://acm.hdu.edu.cn/showproblem.php?pid=6035 题意:给定一棵树,每个节点有一个颜色值.定 ...

  6. hdu-5834 Magic boy Bi Luo with his excited tree(树形dp)

    题目链接: Magic boy Bi Luo with his excited tree Time Limit: 8000/4000 MS (Java/Others)    Memory Limit: ...

  7. CF 461B Appleman and Tree 树形DP

    Appleman has a tree with n vertices. Some of the vertices (at least one) are colored black and other ...

  8. codeforces 161D Distance in Tree 树形dp

    题目链接: http://codeforces.com/contest/161/problem/D D. Distance in Tree time limit per test 3 secondsm ...

  9. HDU 3586 Information Disturbing(二分+树形dp)

    http://acm.split.hdu.edu.cn/showproblem.php?pid=3586 题意: 给定一个带权无向树,要切断所有叶子节点和1号节点(总根)的联系,每次切断边的费用不能超 ...

随机推荐

  1. spring boot 监控与管理(actuator)

    Spring POMs 中提供了一个特殊的依赖模块,即spring-boot-starter-actuator,我们只需要在我们的POM中添加依赖即可 <!-- 监控 管理 --> < ...

  2. magento新增商品属性以及将属性加入Flat table

    magento的EAV模型非常强大且灵活,但是如果不做优化的话,性能会非常低,因为attributes都存放在附表里,要获取一个entity的attribute,需要表联结一次,如果需要获取多条att ...

  3. HDU4352 XHXJ's LIS(LIS 状压)

    题意 题目链接 Sol 刚开始的思路是\(f[i][j]\)表示到第\(i\)位,LIS长度为\(j\)的方案. 然而发现根本不能转移,除非知道了之前的状态然后重新dp一遍.. 题解,,,挺暴力的把, ...

  4. oracle 清空数据库缓存

    oracle 清除数据库缓存: alter system flush shared_pool ; alter system flush BUFFER_CACHE ;

  5. git学习(一)

    提:       远程的主机名(远程仓库服务器名):  origin   本地的主分支: master(本地master分支)      远程的主分支: maste(远程仓库的master分支) gi ...

  6. 数据类型 -- uint32_t 类型

    整型的每一种都有无符号(unsigned)和有符号(signed)两种类型(float和double总是带符号的),在默认情况下声明的整型变量都是有符号的类型(char有点特别),如果需声明无符号类型 ...

  7. 联动选择通过ajax获取选择对应的数据

    网站有时候需要这种联动然后获取到想对应的数据 思路: 这种的话就是你每次选择哪一个就将这个设置一个标注 表示你现在选择的是哪一个 然后每选择一次就进行一次ajax查询,ajax里面有一个data里面添 ...

  8. getline()读入一整行

    string line; getline(cin, line); cin不能读入空行,用getline可以读入空行.

  9. 最简单的基于FFMPEG的转码程序 —— 分析

    模块:  libavcodec    - 编码解码器         libavdevice   - 输入输出设备的支持         libavfilter   - 视音频滤镜支持         ...

  10. linux 命令——49 at (转)

    在windows系统中,windows提供了计划任务这一功能,在控制面板 -> 性能与维护 -> 任务计划, 它的功能就是安排自动运行的任务. 通过'添加任务计划'的一步步引导,则可建立一 ...