POJ 1849 Two
来源:互联网 发布:日本汽车出口数据 编辑:程序博客网 时间:2024/06/07 10:28
传送门
题目大意:自行参考
题解:
exm???
如果必须回到根结点那么答案就是边权之和的两倍。
注意到两个人肯定是走到叶子结点,这样就少走了两人之间的链长的距离。
所以要最大化链长,所以就是求直径。
答案就是边权之和的两倍减去直径长度即可。
代码:
#include<iostream>#include<cstdio>#include<cstring>#include<vector>#define MAXN 100010using namespace std;struct node{int to,wgt;node(int _t,int _w){to=_t;wgt=_w;}};vector<node> g[MAXN];int d1[MAXN],d2[MAXN],maxd;int getdp(int s,int fa){d1[s]=d2[s]=0;for(int i=g[s].size()-1;i>=0;i--)if(g[s][i].to!=fa){getdp(g[s][i].to,s);d2[s]=max(d2[s],d1[g[s][i].to]+g[s][i].wgt);if(d2[s]>d1[s]) swap(d1[s],d2[s]);}maxd=max(maxd,d1[s]+d2[s]);return 0;}int main(){int n,s,tot;while(scanf("%d%d",&n,&s)!=EOF&&(n||s)){maxd=tot=0;for(int i=1;i<=n;i++) g[i].clear();for(int i=1;i<n;i++){int u,v,w;scanf("%d%d%d",&u,&v,&w);g[u].push_back(node(v,w));tot+=w;g[v].push_back(node(u,w));tot+=w;}getdp(s,0);printf("%d\n",tot-maxd);}return 0;}
阅读全文
0 0
- poj 1849 Two
- POJ 1849 Two
- POJ 1849 Two
- POJ 1849 Two 笔记
- POJ 1849 Two(遍历树)
- POJ 1849 Two(树的直径)
- POJ 1849 Two///树的直径
- POJ 1849 Two 树的直径
- poj 1849 Two 树形dp好题
- poj 2738 Two Ends
- Poj 3320(two pointers)
- Poj 2566(two pointers)
- Poj 3678 (two-sat)
- POJ 1849 Two(求树的直径)
- POJ 1849 Two【树的直径+树的遍历】
- Poj 2723(two-sat)
- poj 2090 Two-Stacks Solitaire
- POJ 1334 Two Mountaineers 笔记
- 第四章SpringMVC--Controller接口控制器详解 (4)
- 关雎尔:知道什么是适合自己的是一个填坑的过程
- Discuz系统邮件美化
- IntelliJ IDEA 2017 注册方法
- 时间/日期组件使用
- POJ 1849 Two
- FFT在MATLAB画图步骤
- shiro配置及使用 整体流程
- 制作 静态库.a总结
- Java并发编程:阻塞队列
- java的图片上传与前端展示实例(Servlet+Jsp)
- 命令替换的两种方式$()和``
- 开源资讯- Jeecg 在线聊天MQ插件发布
- mysql的5中查询子句之二group by分组查询