ZOJ 3715 投票选国王 (枚举+贪心+模拟)


题意:

                      在幼儿园里..每个小朋友投一票选举领导...得票最多的小朋友成为领导(若有多个..则多个领导)..现在有个小朋友相当唯一的领导..于是准备贿赂一些小朋友..让他们把票投给自己...每个小朋友要买通..必须满足给他的糖数...问这个小朋友最少用多少的糖使得他成为唯一的leader~

             题解:

                      今天做的练习赛里的...比赛中各种没想法..想DP..但状态无法表示...想贪心..但一直没找到一个明确的方法...好乱...

                      这种模式的题目感觉没做过什么...由于数据范围不大...先枚举咱们这个小朋友是拿的多少个糖赢的..记为t...然后让其他的小朋友糖数小于t(大于t的.则减少到t-1)..而改变这些必然把选票加到自己头上来..寻则所需糖数最少的来操作...当所有小朋友的票数<t了..看咱这个1号小朋友..若他得到的票数大于了t..枚举失败..继续往更大的t进行枚举..若等于t..那么就已经得到了当前枚举条件下的最佳方案..若<t...则需要将还未买通的小朋友中糖数需求最小的几个给买通..让其票数正好等于t...

                      这道题还有个问题是1号小朋友要向别人投票...向票数最少的投就行了..又不可能所有的小朋友票数为t-1( t+(t-1)*(N-1)=N无整数解)...所以这个其实不用考虑..




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 ≤ 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 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,p,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));
				p=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);
						for(j=1;j<=sum[i]-(m-1);j++) {
							p+=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++) p+=c[num[i]];
				ans=min(ans,p);
			}
			printf("%d\n",ans);
		}
	return 0;
}






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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值