hdu 3848 CC On The Tree
来源:互联网 发布:snow拍照软件下载 编辑:程序博客网 时间:2024/05/17 22:46
寻找两个叶子结点最近的距离为多少
#include <cstdio>#include <cstring>#define INF 20000000int cnt;int head[10010],next[20010][3],dp[10010][2];void add(int u,int v,int w){ next[cnt][1]=v; next[cnt][2]=w; next[cnt][0]=head[u]; head[u]=cnt++;}int min(int x,int y){ return x>y?y:x;}void trajan(int cur,int fa){ int flag=0; for(int i=head[cur];i!=-1;i=next[i][0]) { int v=next[i][1]; if(v!=fa) { ++flag; trajan(v,cur); int sum=next[i][2]+dp[v][0]; if(sum<dp[cur][0]) { dp[cur][1]=dp[cur][0]; dp[cur][0]=sum; } else if(dp[cur][1]>sum) dp[cur][1]=sum; } } if(!flag) dp[cur][0]=0; if(fa==-1&&flag==1) cnt=min(cnt,dp[cur][0]); cnt=min(cnt,dp[cur][0]+dp[cur][1]);}int main(){ // freopen("in.txt","r",stdin); int n; while(scanf("%d",&n)!=EOF) { if(!n) break; for(int i=0;i<=n;++i) head[i]=-1,dp[i][0]=dp[i][1]=INF; int u,v,w; cnt=0; for(int i=0; i<n-1; ++i) { scanf("%d%d%d",&u,&v,&w); add(u-1,v-1,w); add(v-1,u-1,w); } cnt=INF; trajan(0,-1); printf("%d\n",cnt); } return 0;}
- HDU 3848 CC On The Tree
- HDU 3848 CC On The Tree
- hdu 3848 CC On The Tree
- hdu 3848 CC On The Tree
- hdu 3848 CC On The Tree
- [HDU 3848]CC On The Tree[dfs]
- 【HDU 3848 CC On The Tree】+ 递归
- hdu 3848 CC on the tree 简单树形dp
- hdu3848 CC On The Tree
- HDOJ 题目3848 CC On The Tree(BFS)
- HDU3848 CC On The Tree, 树形DP
- HDU3848--CC On The Tree【BFS】
- Hdu 4836 The Query on the Tree
- HDU 4718 The LCIS on the Tree
- [CC]Prime Distance On Tree
- HDU 4912 Paths on the tree
- hdu 4912 Paths on the tree
- hdu 4912Paths on the tree (LCA)
- 找工作小记
- 双通道低功耗音频编解码器
- javascriptmvc—steal.dev
- hdu 3853 LOOPS
- QT全局变量声明及使用
- hdu 3848 CC On The Tree
- hdu 4696Answers
- 七航在线考试系统
- vim Multiple Registers
- 常用DOM方法和属性 小结
- 标题、关键词和描述的独特性在于品牌词和用户
- hdu 4704Sum
- 数据结构之双数组trie树
- test