您的位置:首页 > 其它

ZOJ-3715 Kindergarten Election(贪心+枚举)

2017-04-08 14:08 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.

题解:
题目大意:投票选举一个人做领导,然后有一个邪恶的人想通过收卖别人把票投给自己,当然贿赂每个人都需要付出糖果的代价(毕竟都是小孩吗)。现在我们要帮那个邪恶的人当上领导,就是让他的票比任何一个人都多,而且每个人的票不能投给自己。问最少消耗多少个糖果能让这个邪恶的人当上领导。

我的思路:因为数据比较小,所以我们可以枚举这个人获得的票数,然后比他票数多的人的票都减少到比他小,当然减少的票数都要加到这个人的头上。

下面是代码:

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
int f[111],c[111],num[111],sum[111],vis[111];
const int inf = 0x3f3f3f3f;
bool cmp(int t1,int t2)
{
return c[t1]<c[t2];
}
int main()
{
int T,i,j,m,n,k,temp,ans;
scanf("%d",&T);
while(T--)
{
ans=inf;
memset(sum,0,sizeof(sum));
scanf("%d",&n);
for(i=2; i<=n; i++)
{
scanf("%d",&f[i]);
sum[f[i]]++;
}
for(i=2; i<=n; i++) scanf("%d",&c[i]);
for(m=max(sum[1],1); m<n; m++)
{
memset(vis,0,sizeof(vis));
temp=0;
int have=sum[1];
for(i=2; i<=n; i++) //第i个小朋友得到的选票
{
if(sum[i]>=m)
{
have+=sum[i]-(m-1);
if(have>m) break;
for(j=2,k=0; j<=n; j++) //j号小朋友投的票
{
if(f[j]==i) num[++k]=j;
}
sort(num+1,num+1+k,cmp); // 这里的语句和sort(num+1, num+1+k);不同,具体看cmp函数的定义和实现
for(j=1; j<=sum[i]-(m-1); j++) //收买票数比自己多的小朋友的票
{
temp+=c[num[j]];
vis[num[j]]=true;
}
}
}
if(have>m) continue;
for(k=0,i=2; i<=n; i++)
{
if(!vis[i]&&f[i]!=1) num[++k]=i;
}
sort(num+1,num+1+k,cmp);
for(i=1; i<=m-have; i++) temp+=c[num[i]]; // 如果还不到m票,就从其他人那收买比较便宜的票
ans=min(ans,temp);
}
printf("%d\n",ans);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  ZOJ-3715 贪心+枚举