枚举加二分找最优解Kindergarten Election
来源:互联网 发布:浙江省政府数据平台 编辑:程序博客网 时间:2024/06/05 19:22
At the beginning of the semester in kindergarten, then 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 friendfi (where 1 ≤ fi ≤ n, and it's too shame to vote for yourself, sofi ≠ 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 theONLY leader. (That means the number of votes he gets should strictly larger than any other.) Soon Sheldon found that if he giveci 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 theONLY leader with minimum cost of candies. By the way, Sheldon should vote for any one he wantsEXCEPT himself.
Input
There are multiple test cases. The first line of input contains an integerT (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 withfi.
The third line contains n-1 integers: ci (1 ≤ ci ≤ 1000, and 2 ≤ i ≤ n) -- represents that if Sheldon gaveci candies to the ith kid, the ith kid would vote Sheldon, instead of their old best friendfi, as the new semester leader.
Output
For each test case, print the minimal cost of candies to help Sheldon become theONLY leader.
Sample Input
241 1 21 10 10033 21 10
Sample Output
011
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<cstdlib>#include<cstring>#include<algorithm>#include<cmath>#include<queue>#include<map>#include<string>#define LL long long int#define inf 0x3f3f3f3f#define N 1000010using namespace std;struct node{ int c,f;} a[105],b[105];int cnt[105],c[105],n,book[105];int cmp(node ll,node rr){ return ll.c<rr.c;}int main(){ int t; scanf("%d",&t); while(t--) { int n; scanf("%d",&n); memset(cnt,0,sizeof(cnt)); for(int i=2; i<=n; i++) { scanf("%d",&a[i].f); cnt[a[i].f]++; } for(int i=2; i<=n; i++) scanf("%d",&a[i].c); int ans=inf; for(int k=max(2,cnt[1]); k<=n; k++) { memset(book,0,sizeof(book)); int sum=cnt[1],tans=0; for(int i=2; i<=n; i++) { if(cnt[i]>=k) { sum+=cnt[i]-k+1; if(sum>k)break; int len=1; for(int j=2; j<=n; j++) { if(a[j].f==i) b[len].c=a[j].c,b[len++].f=j; } sort(b+1,b+len,cmp); for(int j=1;j<=cnt[i]-k+1;j++) { tans+=b[j].c; book[b[j].f]=1; } } } if(sum>k) continue; int len=1; for(int i=2;i<=n;i++) { if(!book[i]&&a[i].f!=1)c[len++]=a[i].c; } sort(c+1,c+len); for(int i=1;i<=k-sum;i++) { tans+=c[i]; } ans=min(ans,tans); } printf("%d\n",ans); }}第一次接触枚举答案的题,也是看题解想了好久的;
- 枚举加二分找最优解Kindergarten Election
- Kindergarten Election----枚举加贪心
- Kindergarten Election(枚举+贪心)
- zoj3715 Kindergarten Election 【枚举+贪心】
- ZOJ 3715 - Kindergarten Election 枚举后贪心...
- ZOJ 3715 Kindergarten Election(枚举+贪心)
- ZOJ 3715 Kindergarten Election(枚举+贪心)
- zoj H Kindergarten Election (枚举+贪心)
- ZOJ 3715 Kindergarten Election(枚举、贪心)
- ZOJ 3715 Kindergarten Election(枚举+贪心)
- ZOJ-3715 Kindergarten Election(贪心+枚举)
- ZOJ 3715 Kindergarten Election 幼儿园的选举(枚举贪心)
- ZOJ-3715-Kindergarten Election(贪心 枚举 模拟)
- 贪心加暴力(ZOJ 3715,Kindergarten Election)
- ZOJ 3715 Kindergarten Election
- zoj 3715 Kindergarten Election
- Kindergarten Election(ZOJ3715)
- ZOJ 3715 Kindergarten Election
- 单机运行Spark Shell遇到的一个低级错误
- 【读过的书,留下的迹】数据库系统概念
- 过滤器,拦截器,监听器具体应用上的区别
- 分数槽与整数槽优缺点
- 黑龙江大学校内图书馆自动预约抢座脚本~~
- 枚举加二分找最优解Kindergarten Election
- PCA主成分分析Python实现
- django.core.exceptions.ImproperlyConfigured: No DjangoTemplates backend is configured.
- 问题 Y: 字母图形
- JavaScript | 鼠标悬停动态弹出浮动窗口显示图片 | clientX, clientY, scrollLeft, scrollTop
- Mac电脑使用:Mac电脑如何用自带的Safari浏览器,一键翻译英文网页为中文网页
- 第94篇ES之图像识别OCR及识别的文字ES检索
- canvas中的缩放示例
- Ubuntu下PHP+MySQL+Apache+PHPStorm的安装和配置