您的位置:首页 > 其它

ZOJ 3715 Kindergarten Election 幼儿园的选举(枚举贪心)

2014-04-17 11:20 351 查看
点击打开链接

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.

Author: OUYANG, Jialin
Contest: The 10th Zhejiang Provincial Collegiate Programming Contest
在幼儿园里面,有n个小朋友投票选举谁当leader,不能投给自己,最后谁的票数最多,谁就当leader,如果票数相同,则都当leader。1号小朋友想自己一个人当leader,他可以通过给别的小朋友一些糖从而使得他们改变原来的投票者转而投向1号,问1号最少需要送多少糖果使得他成为leader。
n最大是100,所以可以枚举1号得到票数t。然后将其它人得到的票数大于等于t的减少为t-1,减少的票数转为投向1,当然减少的是所需糖果数最少的,判断1得到的票数是不是等于t,如果大于t,则不否和情况;如果等于t,在判断剩下的人中,是否存在所得票数小于等于t-2的,因为1号也要投票;如果小于t,则从剩余的没有投给1号的人中选取所需糖果数最少的投给1号,直到1号得到的票数等于t。
//30ms 172k
#include<stdio.h>
#include<string.h>
#include<algorithm>
#define M 107
#define inf 0x3f3f3f3f
using namespace std;
int vote[M],candy[M],x[M];
int vote1[M];
bool vis[M],vis1[M];
int countt,n;
int ok(int t)
{
for(int i=2;i<=n;i++)
{
if(vote1[i]>=t)//如果第i个人的糖果数大于t
{
while(vote1[i]!=t-1)//当第i个人的糖果数不等于t-1
{
int minn=inf,pos=0;//minn记录最小的糖果数,pos记录投i的所需的最少糖果数的人
for(int j=2;j<=n;j++)
if(x[j]==i&&!vis1[j]&&candy[j]<minn)//如果j投票给i,且j的糖果数小于最小,且j没有被访问过
{
pos=j;
minn=candy[j];
}
vote1[1]++;//1的票数+1
vote1[i]--;//i的票数-1
vis1[pos]=true;//投i的人不能在投了
countt+=minn;//总糖果数+最少的投i的人的糖果数
}
}
}
return vote1[1];
}
int main()
{
int t,ans;
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
memset(vote,0,sizeof(vote));//初始化
memset(candy,0,sizeof(candy));
memset(vis,0,sizeof(vis));
for(int i=2;i<=n;i++)
{
scanf("%d",&x[i]);
vote[x[i]]++;//x[i]的票数+1
if(x[i]==1)vis[i]=true;//判断这个人是不是把票投给1
}
for(int i=2;i<=n;i++)
scanf("%d",&candy[i]);
int t=max(1,vote[1]);
ans=inf;
for(;t<n;t++)//枚举所有可能票数,从里面找需要糖果数最少的
{
countt=0;
for(int i=1;i<=n;i++){vote1[i]=vote[i];vis1[i]=vis[i];}//维护数组vote和vis
int res=ok(t);//求第一个人得到的票数
if(res==t)//如果得到的票数等于t
{
bool flag=false;
for(int i=2;i<=n;i++)
if(vote1[i]<=t-2)//判断存不存在某个人的票数<=t-2
{
flag=true;   //如果存在
break;       //则结束
}
if(flag)ans=min(ans,countt);//更新最少糖果数
}
else if(res<t)//如果得到的票数小于t
{
while(res<t)//从剩余没有投给1的人里面找所需糖果数最少的人让他投给1
{
int minn=inf,pos=0;//minn标记最少糖果数,pos标记投给1的人的编号
for(int i=2;i<=n;i++)//遍历剩余没有投给1的人
{
if(!vis1[i]&&candy[i]<minn)
{
pos=i;
minn=candy[i];
}
}
res++;//1号得到的票数+1
vis1[pos]=true;//标记这个人已经访问过
countt+=minn;//加上最少的糖果数
}
ans=min(ans,countt);//更新最少糖果数
}
}
printf("%d\n",ans);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: