zoj3715Kindergarten Election(枚举+贪心)

来源:互联网 发布:程序编程和软件开发 编辑:程序博客网 时间:2024/05/17 07:52

At the beginning of the semester in kindergarten, the n little kids (indexed from 1 ton, for convenience) in class need to elect their new leader.

The ith kid will vote for his best friend fi (where 1≤ fi ≤ n, and it's too shame to vote for yourself, so fi ≠ i). And the kid who gets the most votes will be the leader. If more than one kids who get the largest number of votes, there will be multiple leaders in the new semester.

Little Sheldon (the kid with index 1) is extremely vain, and he would like to be theONLY leader. (That means the number of votes he gets should strictly larger than any other.) Soon Sheldon found that if he giveci candies to the ith kid, the ith kid would regard Sheldon as the new best friend, and of course vote for Sheldon.

Every kid including Sheldon loves candies. As an evil programmer, please help the evil Sheldon become theONLY leader with minimum cost of candies. By the way, Sheldon should vote for any one he wantsEXCEPT himself.

Input

There are multiple test cases. The first line of input contains an integer T (T ≤ 100) indicating the number of test cases. Then T test cases follow.

The first line of each case contains one integer: n (3 ≤ n ≤ 100) -- the number of kids in class.

The second line contains n-1 integers: fi (1 ≤ fi ≤ n, fi ≠ i, and 2 ≤ i ≤ n) -- represents that the best friend ofith kid is indexed with fi.

The third line contains n-1 integers: ci (1 ≤ ci 1000, and 2≤ i ≤ n) -- represents that if Sheldon gave ci candies to theith kid, the ith kid would vote Sheldon, instead of their old best friendfi, as the new semester leader.

Output

For each test case, print the minimal cost of candies to help Sheldon become theONLY leader.

Sample Input

241 1 21 10 10033 21 10

Sample Output

011

Hint

题目没想到很难啊,居然是枚举票数,一直想着枚举糖果,悲剧了啊,以后这样枚举的题目要从多方向考虑委屈


/*** 时间:2014-10-7** 知识点:** 题意:*/#include<iostream>#include<cstdio>#include<cstring>#include<map>#include<set>#include<cmath>#include<queue>#include<algorithm>using namespace std;struct node{    int p,v;}g[110];bool cmp(node a,node b){    return a.v<b.v;}int sum[110],n;int main(){    int t;    scanf("%d",&t);    while(t--)    {        scanf("%d",&n);        int i,j;        memset(sum,0,sizeof sum);        for(i=0;i<n-1;i++){            scanf("%d",&g[i].p);            sum[g[i].p]++;        }        for(i=0;i<n-1;i++)            scanf("%d",&g[i].v);        sort(g,g+n-1,cmp);        for(i=0,j=0;i<n-1;i++)        {            if(g[i].p!=1)            {                g[j++]=g[i];            }        }        int zz=j;        //cout<<zz<<endl;        int ans=1000000;        for(int piao=max(1,sum[1]);piao<n;piao++)        {            int s=piao-sum[1],ct=0,dp[110];            memcpy(dp,sum,sizeof dp);            bool use[110];            memset(use,0,sizeof use);            for(i=2;i<=n;i++)            {                if(dp[i]>=piao)                {                   if(s<0)break;                   for(j=0;j<zz;j++)                   {                       if(dp[i]<piao)break;                       if(g[j].p==i)                       {                           dp[i]--;                           dp[1]++;                           s--;                           use[j]=true;                           ct+=g[j].v;                       }                   }                }            }            if(s<0)continue;           // cout<<s<<" "<<ct<<" ";            if(s>0)            {                for(j=0;j<zz&&s>0;j++)                {                    if(!use[j])                    {                        s--;                        dp[1]++;                        use[j]=true;                        //cout<<j<<" "<<g[j].v<<endl;                        ct+=g[j].v;                    }                }            }            //cout<<ct<<" "<<dp[1]<<" "<<piao<<endl;            ans=ans<ct?ans:ct;        }        printf("%d\n",ans);    }    return 0;}/*10052 2 3 4100 50 40 60*/


0 0
原创粉丝点击