博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
ZOJ - 3715 Kindergarten Election 枚举+贪心
阅读量:3904 次
发布时间:2019-05-23

本文共 2806 字,大约阅读时间需要 9 分钟。

题目:

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

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.

思路:

枚举1号的初始状态i,然后去除大于等于i票的人选中的一部分票,使所有的人的票数都小于i,将这个票都加到1号,如果总票数大于n的话,则去除这种情况,如果小于,则从未被贿赂的人且不投1号的人中选,另1号票数满足于等于i。

挑选人的时候统一贿赂花费小的优先级高。

代码如下:

#include 
#include
#include
#include
#include
using namespace std;const int maxn=105;const int INF=0x3f3f3f3f;int t;int n;struct child{ int cho,need;};child a[maxn];int num[maxn];int compare (child a,child b){ return a.need
i-1) sum+=num[j]-(i-1); if(sum+num[1]>n-1) continue; huo=sum+num[1]; ok=1; memset (vis,0,sizeof(vis)); for (int j=2;j<=n;j++) { if(num[j]<=i-1) continue; int len=0; child b[maxn]; for (int k=1;k<=n-1;k++) { if(a[k].cho==j) { b[len].need=a[k].need; b[len++].cho=k; } } sort(b,b+len,compare); int ci=num[j]-(i-1); for (int k=0;k

 

转载地址:http://tkoen.baihongyu.com/

你可能感兴趣的文章
C/C++/VC++的好网站
查看>>
用WEB标准进行开发
查看>>
[译]关于Android图形系统的一些事实真相
查看>>
J2ME下的Zlib/Gzip/Zip压缩相关
查看>>
Android 模拟器中AVD路径的修改(WIN7)
查看>>
Cygwin 的安装配置
查看>>
Cygwin基本命令的使用方法
查看>>
Java本地接口(JNI)编程指南和规范(第二章)
查看>>
工欲善其事,必先利其器之—使用Atom来写markdown
查看>>
尽量少使用全局变量或全局单例,特别在Android开发过程中
查看>>
使用Visual stuido 2005 的 命令行 工具 CL 编译生成程序
查看>>
使用LayoutInflater的inflate方法的注意事项
查看>>
前期准备:JDK源码下载
查看>>
前期准备:了解下Open JDK与Oracle JDK的区别
查看>>
android:layout_gravity 和 android:gravity 的区别
查看>>
工欲善其事,必先利其器之—使用ImageMagick处理图片
查看>>
工欲善其事,必先利其器之—使用PlantUML画UML图
查看>>
Android开发填坑之setUseWideViewPort
查看>>
前期准备:搭建代码阅读环境(Mac上搭建OpenGrok查看JDK源码)
查看>>
有关使用xsl输出csv格式文档的实践小结
查看>>