ZOJ-3715-Kindergarten Election(贪心 枚举 模拟)

来源:互联网 发布:苏州网页美工培训 编辑:程序博客网 时间:2024/05/17 08:23

Kindergarten Election
Time Limit: 2 Seconds Memory Limit: 65536 KB
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 the ONLY 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 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 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 ≤ n, fi ≠ 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 the ONLY 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.

题意:T代表接着有T组数据,每组数据首先给N,代表有N个人,接着一行给出people[2]~people[N]共N-1个数字,代表第i个人投票给第people[i]个人,投票不允许自己投给自己,第一个人可以任意选择投给其他N-1个人。再接着一行给出num[2]~num[N]共N-1个数,代表如果贿赂给第i个人num[i]个糖果,就可以操纵此人的投票,当然操纵后的结果不能是自己投给自己。最终获得票数最高的人将会成为老大。
第一个人想成为老大,那么就意味着他的票数要高于其他所有人,即便有人和他票数相等也不行。第一个人决定去贿赂其他人来操纵选票达成目的,找出需要花费的最小糖果数量。

思路:首先按照贿赂值升序排列,并记录每个人的票数,对于这个有序序列,可以枚举第一个人胜出时的所有可能票数,对于枚举到的目标票数,有两种贪心策略使得当前实际票数大于等于目标票数。
贪心策略一:找到得票数比第一个人得票数高的人,先把给他投票的人贿赂掉。
接着贪心策略二:如果策略一得到的票数还不够,那就重新遍历此序列,由于已经升序排列此序列,只需对于遇到的每一个满足条件的选民进行贿赂即可。
策略完毕后,评判是否还有人得票比第一个人高,如果没有,那说明此次贪心是有效的,更新贿赂值即可。

对于两次贪心共同的规则,每次都要选择未选择过的选民,且该选民没有给第一个人投票。

代码

/*题意:*/#include<string.h>#include<stdio.h>#include<iostream>#include<algorithm>using namespace std;const int maxn=105;const int INF=0x3f3f3f3f;struct node{    int v,w;} edge[maxn];int num[maxn];//记录得票数int num1[maxn];int vis[maxn];//标记是否被贿赂bool cmp(node x,node y){    return x.w<y.w;}int main(){    int T;    scanf("%d",&T);    while(T--)    {        int N;        scanf("%d",&N);        memset(num,0,sizeof(num));        memset(edge,0,sizeof(edge));        for(int i=1; i<N; i++)        {            scanf("%d",&edge[i].v);            num[edge[i].v]++;        }        for(int i=1; i<N; i++)        {            scanf("%d",&edge[i].w);        }        sort(edge+1,edge+N,cmp);        int result=INF;        int flag;        for(int i=num[1]; i<N; i++)//i枚举第一个人所有可能的得票数        {            memset(vis,0,sizeof(vis));//初始化所有人都没被贿赂            memcpy(num1,num,sizeof(num));            int flag_num=num1[1];//目前状态下,第一个人的得票数            flag=0;            //下面是第一种遍历规则            for(int j=1; j<N; j++)//遍历这N个人的投票            {                if(edge[j].v!=1&&num1[edge[j].v]>=i)//第j个人没有投票给一号,且第j个人的投票对象获得的票数大于等于枚举到的i                {                    num1[edge[j].v]--;//第j个人的投票对象获得的票数减一                    flag_num++;//目前状态下,第一个人的票数加一                    vis[j]=1;//标记第j个人被贿赂                    flag+=edge[j].w;//累加贿赂值                }            }            if(flag_num<=i)//如果目前状态第一个人的得票数小于等于枚举到的票数            {                //下面是第二种遍历规则                for(int j=1; j<N&&flag_num<i; j++) //重新遍历N个人                {                    if(vis[j]==0&&edge[j].v!=1)//只要此人未被贿赂过且不投票给一号                    {                        num1[edge[j].v]--;//就贿赂此人                        flag_num++;//因为之前按照贿赂值升序排列,所以现在遍历到的一定是代价最小的                        vis[j]=1;                        flag+=edge[j].w;                    }                }                //下面判定贿赂值是否有效                int j=2;                for(; j<=N&&num1[j]>=i; j++);//有人得票数大于等于i就终止循环                if(j!=N+1)//循环到头也没找到票数比第一位大的人,说明贿赂值有效                    result=min(result,flag);//更新贿赂值            }        }        printf("%d\n",result);    }    return 0;}
0 0
原创粉丝点击