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 ...
随机推荐
- Rust 1.7.0 处理命令行參数
std是 Rust 标准函数库: env 模块提供了处理环境函数. 在使用标准函数库的时候,使用 use 导入对应的 module . 一.直接输出 use std::env; fn main(){ ...
- Altera Quartus II下载中途失败
1.Altera 的quartus II 下载程序时中途失败,有可能是程序写的不对(缺少初始状态,else补全等等) 2.下载程序pof最好也用英文命名,再下载.
- angular多个控制器如何共享数据
多个控制器之间共享数据,通常两种方式,一种是在控制器里通过$scope.$$prevSibling或$scope.$$nextSibling获得另一个控制器的作用域对象. 第二种是通过服务的方式,也是 ...
- 线程相关函数(7)-sem_post(), sem_wait() 信号量
sem_tsem_initsem_waitsem_trywaitsem_timedwaitsem_postsem_destroy 生产者消费者实例: #include <stdlib.h> ...
- Django--缓存、信号、序列化
一 Django的缓存机制 1.1 缓存介绍 1.缓存的简介 在动态网站中,用户所有的请求,服务器都会去数据库中进行相应的增,删,查,改,渲染模板,执行业务逻辑,最后生成用户看到的页面. 当一个网站的 ...
- 使用【单独】的一个<script>进行js文件的引用
刚才用jQuery的时候,总是发现js代码不被执行...后来发现我的代码是这么写的: <script type="text/javascript" src="htt ...
- 时间同步linux和window
windows和linux都可以通过ntp服务,同步时间.
- Linux动态库开发
http://blog.csdn.net/qq_33850438/article/details/52014399 ### 导出符号------------------------------ 默认所 ...
- [uboot]MLO和uboot-spl.bin, uboot.img和uboot.bin
前段时间使用TI的am4378芯片,发现系统在SD卡启动的时候,启动文件使用的是MLO和uboot.img:而Norflash和eMMC启动的时候使用的是 uboot-spl.bin和uboot.bi ...
- 一只青蛙一次可以跳上1级台阶,也可以跳上2级……它也可以跳上n级。求该青蛙跳上一个n级的台阶总共有多少种跳法。
// test14.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include<iostream> #include< ...