hdu 4303 Hourai Jeweled(树形DP,5级)

Hourai Jeweled

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 163840/163840 K (Java/Others)
Total Submission(s): 808    Accepted Submission(s): 289


Problem Description
Kaguya Houraisan was once a princess of the Lunarians, a race of people living on the Moon. She was exiled to Earth over a thousand years ago for the crime of using the forbidden Hourai Elixir to make herself immortal. Tales of her unearthly beauty led men from all across the land to seek her hand in marriage, but none could successfully complete her trial of the Five Impossible Requests.

One of these requests is to reckon the value of "Hourai Jeweled (蓬莱の玉の枝)". The only one real treasure Kaguya has, in her possession. As showed in the picture, Hourai Jeweled is a tree-shaped twig. In which, each node is ornamented with a valuable diamond and each edge is painted with a briliant color (only bright man can distinguish the difference). Due to lunarians' eccentric taste, the value of this treasure is calculated as all the gorgeous roads' value it has. The road between two different nodes is said to be gorgeous, if and only if all the adjacent edges in this road has diffenrent color. And the value of this road is the sum of all the nodes' through the road.
Given the value of each node and the color of each edge. Could you tell Kaguya the value of her Hourai Jeweled?
 

Input
The input consists of several test cases.
The first line of each case contains one integer N (1 <= N <= 300000), which is the number of nodes in Hourai Jeweled.
The second line contains N integers, the i-th of which is Vi (1 <= Vi <= 100000), the value of node i.
Each of the next N-1 lines contains three space-separated integer X, Y and Z (1<=X,Y<=N, 1 <= Z <= 100000), which represent that there is an edge between X and Y painted with colour Z.
 

Output
For each test case, output a line containing the value of Hourai Jeweled.
 

Sample Input
6 6 2 3 7 1 4 1 2 1 1 3 2 1 4 3 2 5 1 2 6 2
 

Sample Output
134
Hint
gorgeous roads are : 1-2 Value: 8 1-3 Value: 9 1-4 Value:13 1-2-6 Value:12 2-1-3 Value:11 2-1-4 Value:15 2-5 Value:3 2-6 Value:6 3-1-4 Value:16 3-1-2-6 Value:15 4-1-2-6 Value:19 5-2-6 Value:7
 

Author
BUPT
 

Source
 

Recommend
zhuyuanchen520

思路:使用搜索,实际上是树形DP的思想,dp[x],以x为根的子树的结果。因此答案就是主根的DP值。

            ans[x]为以x节点为开始点的所有路径的value值,cnt[x]以x为开始点的方案数。

            dp[x]=x的所有子树的值+以x为开始点的value+以x为中间点的value值。

          最后输出主根节点的值就是答案了。

注意点:long long 过不了,要用__int64

#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
using namespace std;
const int mm=6e5+9;
class potcol
{
 public:int u,v,c;
 bool operator<(const potcol&x)const
 {
   if(x.u^u)return u<x.u;
   else return c>x.c;
 }
}f[mm];
class node
{public:int v,c,next;
}e[mm];
int head[mm],n,facol[mm],edge;
__int64 ans[mm],val[mm],cnt[mm];
bool leaf[mm];
void swap(int&x,int&y)
{int z=x;x=y;y=z;}
void data()
{memset(leaf,0,sizeof(leaf));
 memset(head,-1,sizeof(head));
 facol[1]=-1;
 edge=0;
}
void add(int u,int v,int c)
{
 e[edge].v=v;e[edge].c=c;e[edge].next=head[u];head[u]=edge++;
}
__int64 dfs(int u,int fa)
{
  __int64 ret=0,cur_ans=0,cur_cnt=0,tmp_ans=0,tmp_cnt=0;
  int rt=-1,v;
  ans[u]=val[u];cnt[u]=1;leaf[u]=1;
  for(int i=head[u];~i;i=e[i].next)
  {if(e[i].v==fa)continue;
   leaf[u]=0;///not leaf
   v=e[i].v;
   facol[v]=e[i].c;
   ret+=dfs(v,u);
   if(facol[v]^facol[u])///can connected
   {
    ans[u]+=ans[v]+val[u]*cnt[v];cnt[u]+=cnt[v];
   }
   if(facol[u]^facol[v]||leaf[v])///u is start point
   {
   ret+=ans[v]+val[u]*cnt[v];
   }
   if(facol[v]^rt)
   {
    rt=facol[v];cur_ans+=tmp_ans;cur_cnt+=tmp_cnt;
    tmp_ans=ans[v];tmp_cnt=cnt[v];
   }
   else tmp_ans+=ans[v],tmp_cnt+=cnt[v];///u is middle point
   ret+=cur_ans*cnt[v]+cur_cnt*ans[v]+cur_cnt*cnt[v]*val[u];
  }
  return ret;
}
int main()
{
 while(~scanf("%d",&n))
 {
  for(int i=1;i<=n;++i)
  scanf("%I64d",&val[i]);
  for(int i=1;i<n;++i)
  {scanf("%d%d%d",&f[i].u,&f[i].v,&f[i].c);
   f[i+n-1]=f[i];
   swap(f[i].u,f[i].v);
  }
  data();
  sort(f+1,f+n+n-2);
  for(int i=1;i<=n+n-2;++i)
  {add(f[i].u,f[i].v,f[i].c);
  }
  printf("%I64d\n",dfs(1,-1));
 }
 return 0;
}







posted @ 2013-05-30 09:31  剑不飞  阅读(201)  评论(0编辑  收藏  举报