ZOJ 3715 Kindergarten Election
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 cicandies 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 wantsEXCEPT 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 <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <algorithm>
#include <math.h> using namespace std;
#define MAX 10000000
struct Node
{
int pos;
int value;
}c[105],v[105][105];
int n;
int cmp(Node a,Node b)
{
return a.value<b.value;
}
int f[105];
int num[105];
int tag[105];
int ans;
int s[105];
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
memset(num,0,sizeof(num));
for(int i=2;i<=n;i++)
{
scanf("%d",&f[i]);
v[f[i]][++num[f[i]]].pos=i;
}
for(int i=2;i<=n;i++)
{scanf("%d",&c[i].value);c[i].pos=i;}
for(int i=2;i<=n;i++)
for(int j=1;j<=num[i];j++)
v[i][j].value=c[v[i][j].pos].value;
for(int i=2;i<=n;i++)
sort(v[i]+1,v[i]+1+num[i],cmp);
sort(c+1,c+1+n,cmp);
ans=MAX;
for(int p=num[1];p<=n;p++)
{
int l=p-num[1];
int sum1=0;
int sum2=0;
memset(s,0,sizeof(s));
for(int i=2;i<=n;i++)
{
if(num[i]>=p)
{
int m=num[i]-p+1;
for(int j=1;j<=m;j++) {sum2+=v[i][j].value;s[v[i][j].pos]=1;}
sum1+=m;
}
}
if(sum1>l)
continue;
int m2=l-sum1;int cot=0;
for(int i=2;i<=n;i++)
{
if(cot>=m2)
break;
if(!s[c[i].pos]&&f[c[i].pos]!=1){ sum2+=c[i].value;cot++;}
}
ans=min(ans,sum2);
}
printf("%d\n",ans);
}
return 0;
}
ZOJ 3715 Kindergarten Election的更多相关文章
- Kindergarten Election
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3715 题意:有N个孩子投票选举leader,不能自己选自己.Sheldon ...
- ZOJ - 3715贪心
ZOJ - 3715KindergartenElection 题目大意:幼儿园里正在举办班长选举,除1号小朋友外每个人都会投他最好的朋友,但1号小朋友可以贿赂别人(小伙子有丶想法),被贿赂的小朋友就会 ...
- 2012-2014 三年浙江 acm 省赛 题目 分类
The 9th Zhejiang Provincial Collegiate Programming Contest A Taxi Fare 25.57% (166/649) (水 ...
- 【转载】图论 500题——主要为hdu/poj/zoj
转自——http://blog.csdn.net/qwe20060514/article/details/8112550 =============================以下是最小生成树+并 ...
- [译]ZOOKEEPER RECIPES-Leader Election
选主 使用ZooKeeper选主的一个简单方法是,在创建znode时使用Sequence和Ephemeral标志.主要思想是,使用一个znode,比如"/election",每个客 ...
- ZOJ People Counting
第十三届浙江省大学生程序设计竞赛 I 题, 一道模拟题. ZOJ 3944http://www.icpc.moe/onlinejudge/showProblem.do?problemCode=394 ...
- ZOJ 3686 A Simple Tree Problem
A Simple Tree Problem Time Limit: 3 Seconds Memory Limit: 65536 KB Given a rooted tree, each no ...
- ZOJ Problem Set - 1394 Polar Explorer
这道题目还是简单的,但是自己WA了好几次,总结下: 1.对输入的总结,加上上次ZOJ Problem Set - 1334 Basically Speaking ac代码及总结这道题目的总结 题目要求 ...
- ZOJ Problem Set - 1392 The Hardest Problem Ever
放了一个长长的暑假,可能是这辈子最后一个这么长的暑假了吧,呵呵...今天来实验室了,先找了zoj上面简单的题目练练手直接贴代码了,不解释,就是一道简单的密文转换问题: #include <std ...
随机推荐
- [svc]nginx-module-vts第三方模块安装配置
参考: https://github.com/vozlt/nginx-module-vts#installation https://github.com/kubernetes/ingress-ngi ...
- Hadoop在线分析处理(OLAP)
数据处理与联机分析处理 ( OLAP ) 联机分析处理是那些为了支持商业智能,报表和数据挖掘与探索等业务而开展的工作.这类工作的样例有零售商按地区和季度两个维度计算门店销售额,银行按语言和月份两个维度 ...
- python 获取当前时间的用法
1.先导入库:import datetime 2.获取当前日期和时间:now_time = datetime.datetime.now() 3.格式化成我们想要的日期:strftime() 比如:“2 ...
- python 使用urllib.urlopen超时问题的解决方法
准备写一个python脚本抓取网页数据,前面抓了几个都没有什么问题,但总会抓取不完整,在中间过程中没有反应,发现执行urlopen的地方总是提示超时,百度了一下,因为我使用的是urllib不是urll ...
- Windows 10 开发人员预览版中的新增功能(转自 IT之家)
Windows 10 开发人员预览版中的新增功能 在Win10预览版中安装工具与SDK后,即可着手创建Windows通用应用或先浏览目前的环境与此前相比都发生了什么变化. 应用建模 文件资源管理器: ...
- JS中同步与异步的理解
你应该知道,javascript语言是一门“单线程”的语言,不像java语言,类继承Thread再来个thread.start就可以开辟一个线程,所以,javascript就像一条流水线,仅仅是一条流 ...
- Ubuntu设置中文-以及-安装拼音输入法
2017-11-12更新 安装搜狗拼音: http://blog.csdn.net/iamplane/article/details/70447517 ------------------------ ...
- FreeRTOS 中断优先级配置(重要)
以下转载自安富莱电子: http://forum.armfly.com/forum.php NVIC 的全称是 Nested vectored interrupt controller,即嵌套向量中断 ...
- java Web监听器导图详解
监听器是JAVA Web开发中很重要的内容,其中涉及到的知识,可以参考下面导图: Web监听器 1 什么是web监听器? web监听器是一种Servlet中的特殊的类,它们能帮助开发者监听web中的特 ...
- 转载:vim的复制,删除,粘贴,块操作以及快速替换功能
掌握如下命令有什么好办法,我可以告诉你唯手熟尔!!多看多练对于VIM而言,复制,删除,粘贴的操作应该是非常多的.这次也做一个总结,关于处理VIM下的复制,删除,粘贴等操作.学会使用帮助文件,命令的帮助 ...