zoj 3715 Kindergarden Election


Kindergarden Election

At the beginning of the semester in kindergarten, the n little kids (indexed from 1 to n, 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 give ci candies to the ith kid, the ithkid 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 the ONLY leader with minimum cost of candies. By the way, Sheldon should vote for any one he wants EXCEPT 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 ≤ nfi ≠ i, and 2 ≤ i ≤ n) -- represents that the best friend of ith 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 the ith kid, the ith kid would vote Sheldon, instead of their old best friend fi, as the new semester leader.

Output

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

Sample Input
2
4
1 1 2
1 10 100
3
3 2
1 10
Sample Output
0
11
Hint

In the first case,

  • If Sheldon vote for 2nd kid, the 2nd kid and Sheldon will both have 2 votes. In this case, Sheldon have to pay 100 candies to the 4th kid, and get 3 votes to win;
  • If Sheldon vote for 3rd or 4th kid, Sheldon will win with 2 votes without sacrifice any candy.


题意:

这道题的题意是1号想要当班长,班长的票数一定要比其他的人都多!

但是每个人都有自己的朋友,并且会投自己的朋友一票,一号需要給不投自己的人一定的糖果才能够改投一号;

那么求出1号能成为班长的最小花费;


思路:

思路也是同学教的...orz

假设x是现在一号所得的票数,那我们就枚举一号能够得到的各种票数,然后求所对应的最小花费,

所求即为最小花费中的最小值;具体我们贪心排序贿赂投i的人所需的糖果数(从小到大);

*注意:贪心的做法是先砍掉票数>y-1的人的票数并贪心选择花费最小的,每枚举完一种票数会可能会出现三种               情况:(1)x+kan<y,那么就贪心选择那些点还可以再砍(还未被砍掉的点);

                       (2)x+kan=y,得到最小花费;

                       (3)x+kan>y,不考虑了,因为向后枚举票数总会出现x+kan所对应的前两种情况;


代码:

#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<queue>
#include<map>
#include<vector>
#include<iostream>
#include<algorithm>
using namespace std;
#define mem(a,b) memset(a,b,sizeof(a))
typedef long long ll;
typedef pair<int ,int> P;
const int inf=1e9-1;
const int maxn=110;

int c[maxn];    //花费;
int f[maxn];    //投给;
int cnt[maxn];  //得到的票数;
int used[maxn]; //是否已砍掉;
int n;

bool cmp(const int &a,const int &b)
{
    return c[a]<c[b];
}

int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        vector<int>one,G[105];
        memset(cnt,0,sizeof(cnt));

        scanf("%d",&n);
        for(int i=2;i<=n;i++)
        {
            cin>>f[i];
            cnt[f[i]]++;
            if(f[i]!=1)
            {
                one.push_back(i);
                G[f[i]].push_back(i);
            }
        }

        for(int i=2;i<=n;i++)
            scanf("%d",&c[i]);
        sort(one.begin(),one.end(),cmp);
        for(int i=2;i<=n;i++)
            sort(G[i].begin(),G[i].end(),cmp);

        int ans=inf;
        for(int vote=2;vote<=n;vote++)
        {
            cnt[vote]++;
            for(int y=max(1,cnt[1]);y<n;y++) //枚举票数;
            {
                int d=y-cnt[1],w=0;
                mem(used,0);
                for(int i=2;i<=n;i++)  //贪心砍掉i的票数多于y-1的;
                {
                    if(cnt[i]<y) continue;
                    int t=cnt[i];
                    for(int j=0;j<G[i].size();j++)
                    {
                        d--;
                        t--;
                        w+=c[G[i][j]];
                        used[G[i][j]]=1;

                        if(d<=0||t<y)
                            break;
                    }
                    if(d<0||t>=y)
                    {
                        w=inf;
                        d=0;
                        break;
                    }
                }
                if(d>0)
                {
                    for(int i=0;d&&i<one.size();i++)
                    {
                        if(!used[one[i]])
                        {
                            w+=c[one[i]];
                            d--;
                        }
                    }
                }
                ans=min(ans,w);
            }
            cnt[vote]--;
        }
        cout<<ans<<endl;
    }
    return 0;
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值