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, theith 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.
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.
For each test case, print the minimal cost of candies to help Sheldon become theONLY leader.
2 4 1 1 2 1 10 100 3 3 2 1 10
0 11
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.
题意:有个小屁孩想当班级扛把子 他可以用糖果收买别人 最后要求他的票数是最多的,其他人都比他少 。
我们就能枚举假设他K票当选 则其他人票数一定小于K。我们让超过K票的人票数变为K-1 多的票给1号 。
若是现在都小于K然而1的票还是少于K则从花费最少糖果且未被标记的人那边拿票直至等于K;
至于会不会出现他是K其余都是k-1 然后他投给别人使存在俩个K是不可能的 因为 假设每个人都投给自己则每人都是1 小屁孩至少是从2 开始枚举 说明至少有一个人为0票
#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<string.h>
#include<stdlib.h>
#include<time.h>
#include<math.h>
#include<queue>
#include<map>;
#include<stack>
#include <iomanip>
#define INF 0x3f3f3f3f
using namespace std;
int v[120];//标记
int num[120];//每个人获得票数
int num1[120];
struct node
{
int ki;//i值
int ci;//收买代价
int fi;//投给谁
}p[120];
int cmp(node x,node y)
{
return x.ci<y.ci;
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
memset(num,0,sizeof(num));
int n;
scanf("%d",&n);
for(int i=1;i<n;i++)
{
scanf("%d",&p[i].fi);
p[i].ki=i;
num[p[i].fi]++;
}
for(int i=1;i<n;i++)
{
scanf("%d",&p[i].ci);
}
sort(p+1,p+n,cmp); //花费由小到大排序
int o=max(2,num[1]);//想成为扛把子最少要俩票 若是在俩票以上则从已有票数开始枚举
int mi=INF;
for(int k=o;k<=n;k++)
{
int ans=0;
for(int i=1;i<=n;i++)
num1[i]=num[i];
memset(v,0,sizeof(v));
for(int i=1;i<n;i++)
{
if(p[i].fi!=1&&num1[p[i].fi]>=k&&!v[p[i].ki]) //若小朋友不支持1号但是支持的人票数大于等于K 而且没有被贿赂过
{
v[p[i].ki]=1;
ans+=p[i].ci;
num1[1]++;
num1[p[i].fi]--;
}
}
if(num1[1]<k)
{
for(int i=1;i<n;i++)
{
if(!v[p[i].ki])
{
ans+=p[i].ci;
num1[1]++;
}
if(num1[1]>=k)
break;
}
}
if(ans<mi)
mi=ans;
}
printf("%d\n",mi);
}
}