【ZOJ - 3715】Kindergarten Election(枚举得票数,贪心)

版权声明:欢迎学习我的博客,希望ACM的发展越来越好~ https://blog.csdn.net/qq_41289920/article/details/89398508

题干:

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.

题目大意:

在学期开始的幼儿园,n个小孩(1到n)在课堂上需要选择他们的新领导。
第一个孩子会投票给他最好的朋友fi(其中1≤fi≤n,这太可惜了,自己投票,所以fi≠i)。 

获得最多票数的孩子将是领袖。 如果有一个以上的孩子获得最多的票数,在新学期将有多个领导人。

小谢尔顿(下标为1)想成为唯一的领导者。 (这意味着他得到的票数应该严格大于任何其他。)

如果他给ci糖果给第i个孩子,第i个孩子会认为谢尔顿作为新的最好的朋友,当然投票谢尔顿。

请帮助谢尔顿成为唯一的领导者,最低的糖果成本。 顺便说一下,谢尔顿应该投票支持自己想要的任何一个人。

解题报告:

如果1号选手已经指定投票给哪一位选手,那很显然和codeforce上的那个C题Elections一模一样,n^2就可以解决。但是这题既然数据范围是100,说明肯定有坑,我本来的想法是先不让1号选手投票,最后再让他直接投给最小的那个人,但是发现这样好像不太行,所以干脆就直接枚举他投票给哪一个人就可以了。这样复杂度O(n^3),,反正也说得过去。就这么xjb写了,,刚开始wa了无数发还以为思路有问题,,结果发现写了cmp函数但是忘了sort了、、、

AC代码:

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<queue>
#include<map>
#include<vector>
#include<set>
#include<string>
#include<cmath>
#include<cstring>
#define ll long long
#define pb push_back
#define pm make_pair
using namespace std;
const int MAX = 2e5 + 5;
int n;
int bk[105];
struct Node {
	int f,c;
} p[MAX];
bool cmp(Node a,Node b) {
	return a.c < b.c;
}
int cnt[105];
int main() {
	int t;
	cin>>t;
	while(t--) {
		scanf("%d",&n);
		memset(cnt,0,sizeof cnt);
		for(int i = 2; i<=n; i++) scanf("%d",&p[i].f);
		for(int i = 2; i<=n; i++) scanf("%d",&p[i].c);
		int ans = 0x3f3f3f3f;
		for(int i = 2; i<=n; i++) cnt[p[i].f]++;
		sort(p+2,p+n+1,cmp);
		for(int qq = 2; qq<=n; qq++) {
			p[1].f = qq;
			for(int piao = 1; piao <= n; piao++) {
				memset(cnt,0,sizeof cnt);
				memset(bk,0,sizeof bk);
				for(int i = 1; i<=n; i++) cnt[p[i].f]++;
				int chu = cnt[1];
				int cost = 0;
				for(int i = 2; i<=n; i++) {
					if(p[i].f != 1 && cnt[p[i].f ] >= piao) {
						cost += p[i].c;
						chu++;
						cnt[p[i].f]--;
						bk[i] = 1;
					}
				}
				for(int i = 2; i<=n; i++) {
					if(p[i].f !=1 && chu < piao && bk[i] == 0) {
						chu++;
						cost += p[i].c;
					}
				}
				ans = min(ans,cost);
			}
		}
		printf("%d\n",ans);
	}
	return 0 ;
}

猜你喜欢

转载自blog.csdn.net/qq_41289920/article/details/89398508
ZOJ