hdu 3848 CC On The Tree
2011-08-06 15:46
429 查看
貌似还是树状DP,貌似我还不会,还是用搜索搜的。
如果纯粹用优先队列搜,会超时的。需要标记到达某个节点后的最小长度,如果下一次搜到这个点长度比这个点存的大,就不需要入队了,入了也白入= =。。
如果纯粹用优先队列搜,会超时的。需要标记到达某个节点后的最小长度,如果下一次搜到这个点长度比这个点存的大,就不需要入队了,入了也白入= =。。
#include <queue> #include <stack> #include <math.h> #include <stdio.h> #include <stdlib.h> #include <iostream> #include <limits.h> #include <string.h> #include <string> #include <algorithm> #define MID(x,y) ( ( x + y ) >> 1 ) #define L(x) ( x << 1 ) #define R(x) ( x << 1 | 1 ) #define BUG puts("here!!!") using namespace std; const int MAX = 10010; struct NODE{ int len,to; NODE* next;}; struct A{int len,u;}; NODE node[MAX*2],*p[MAX]; int a[MAX],cnt; int b[MAX]; bool used[MAX]; void init() { cnt = 0; fill(b,b+MAX,INT_MAX); memset(used,false,sizeof(used)); memset(p,0,sizeof(p)); memset(a,0,sizeof(a)); memset(node,0,sizeof(node)); } void Add(int u,int v,int len) { node[cnt].to = v; node[cnt].len = len; node[cnt].next = p[u]; p[u] = &node[cnt++]; } priority_queue<A> q; bool operator<(A a,A b) { return a.len > b.len; } int BFS(int x) { while( !q.empty() ) q.pop(); A tmp; tmp.u = x; tmp.len = 0; q.push(tmp); while( !q.empty() ) { int now = q.top().u; int len = q.top().len; q.pop(); if( a[now] == 1 && now != x ) return len; NODE *head = p[now]; while( head != NULL ) { A tmp; tmp.u = head->to; tmp.len = len+head->len; if( tmp.len < b[head->to] ) { b[head->to] = tmp.len; q.push(tmp); } head = head->next; } } return INT_MAX; } int main() { int n,x,y,len; while( ~scanf("%d",&n) && n ) { init(); for(int i=0; i<n-1; i++) { scanf("%d%d%d",&x,&y,&len); a[x]++; a[y]++; Add(x,y,len); Add(y,x,len); } int mmin = INT_MAX; for(int i=1; i<=n; i++) if( a[i] == 1 ) { b[i] = 0; used[i] = true; int ans = BFS(i); mmin = min(mmin,ans); } printf("%d\n",mmin); } return 0; }
相关文章推荐
- HDU - 3848 CC On The Tree [多端BFS]
- HDU 3848 CC On The Tree 树形DP
- HDU 3848 CC On The Tree
- hdu 3848 CC on the tree 简单树形dp
- [HDU 3848]CC On The Tree[dfs]
- hdu 3848 CC On The Tree
- HDU 3848 CC On The Tree(树形dp)
- HDU 3848 CC On The Tree
- 【HDU 3848 CC On The Tree】+ 递归
- hdu 3848 CC On The Tree
- HDOJ 题目3848 CC On The Tree(BFS)
- 【HDU】4912 Paths on the tree 离线LCA+贪心
- HDU 4836 The Query on the Tree lca || 欧拉序列 || 动态树
- HDU3848 CC On The Tree, 树形DP
- HDU 4718 The LCIS on the Tree(树链剖分+线段树)
- HDU 4010 Query on the tree LCT模板题
- hdu_4718_The LCIS on the Tree(树链剖分+线段树合并)
- lca(HDU 4718,The LCIS on the Tree)
- hdu_4718_The LCIS on the Tree(树链剖分+线段树合并)
- hdu 4912 Paths on the tree (LCA+贪心)