【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 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 ;
}

 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/440428.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

修改DNS的Linux脚本,Shell脚本-配置网络

原先学习过shll但是只是了解一下它的语法而已&#xff0c;在平时中并没有使用&#xff0c;在暑假的时候又想起了shell&#xff0c;所以又回顾了一下&#xff0c;学习后一定要用&#xff0c;这样才能掌握。最近实验室刚装好机子&#xff0c;什么都要配置一下&#xff0c;包括网络…

【ZOJ - 3872】Beauty of Array(思维,计算贡献,枚举)

题干&#xff1a; Edward has an array A with N integers. He defines the beauty of an array as the summation of all distinct integers in the array. Now Edward wants to know the summation of the beauty of all contiguous subarray of the array A. Input There…

linux 使用gzip压缩打包的文件,linux常用的解压,压缩,打包

gzip zcat[rootlinux ~]# gzip [-cdt#] 文件名[rootlinux ~]# zcat 文件名.gz参数&#xff1a;-c : 将压缩的数据输出到屏幕上&#xff0c;可通过数据流重导向来处理-d &#xff1a;解压缩参数-t &#xff1a; 可以用来检验一个压缩文件的一致性&#xff5e;看看文件有无错误-#…

【POJ - 1836】Alignment(dp,LIS,最长上升子序列类问题)

题干&#xff1a; In the army, a platoon is composed by n soldiers. During the morning inspection, the soldiers are aligned in a straight line in front of the captain. The captain is not satisfied with the way his soldiers are aligned; it is true that the …

Windows共享Linux打印机,linux – 如何为cups客户端构建windows共享打印机的url

从this page开始&#xff1a;smbThis backend sends print files to printers shared by a Windows host. Examplesof CUPS device-URIs that may be used includes:smb://workgroup/server/printersharenamesmb://server/printersharenamesmb://username:passwordworkgroup/se…

【HDU - 1080】Human Gene Functions(dp,可编辑距离类问题)

题干&#xff1a; 给你两个DNA序列&#xff08;长度不一定相同&#xff09;&#xff0c;你可以在其中任意位置上加入空格&#xff0c;使得最终他俩长度相同&#xff0c;最终相同长度的两个DNA序列会有个相似度比较&#xff08;每个字符相对应的比较&#xff09;&#xff0c;问…

linux 对硬盘重新分区,硬盘重新分区后,linux的硬盘表的重新设置

硬盘重新分区后&#xff0c;linux的硬盘表的重新设置发布时间:2007-12-29 16:04:19来源:红联作者:Alwaysfirm会硬盘分区后uuid会变动&#xff0c;导致linux挂载硬盘出错。-----什么是uuid?UUID&#xff0c;全称Universally Unique Identifier它是一个128位&#xff0c;16字节的…

【POJ - 1837】Balance(dp及其优化)

题干&#xff1a; Gigel has a strange "balance" and he wants to poise it. Actually, the device is different from any other ordinary balance. It orders two arms of negligible weight and each arms length is 15. Some hooks are attached to these arm…

linux awk 时间范围,linux下使用awk命令按时间段筛选日志

需求很多时候我们需要按照时间段来进行日志的分析&#xff0c;比如说查看上午的&#xff0c;或者某月某日的的具体日志&#xff0c;就不能简单实用tail -f或者head -n命令了。这个时候我们需要借用awk。命令zcat com.log20160529.gz | grep dianping_reply.log| awk {split($4,…

【CodeForces - 1153D】Serval and Rooted Tree(树形dp)

题干&#xff1a; Now Serval is a junior high school student in Japari Middle School, and he is still thrilled on math as before. As a talented boy in mathematics, he likes to play with numbers. This time, he wants to play with numbers on a rooted tree. …

【OpenJ_Bailian - 4117】简单的整数划分问题(dp)

题干&#xff1a; 将正整数n 表示成一系列正整数之和&#xff0c;nn1n2…nk, 其中n1>n2>…>nk>1 &#xff0c;k>1 。 正整数n 的这种表示称为正整数n 的划分。正整数n 的不同的划分个数称为正整数n 的划分数。 Input 标准的输入包含若干组测试数据。每组测试…

【ZOJ - 3778】Talented Chef(贪心)

题干&#xff1a; As we all know, Coach Gao is a talented chef, because he is able to cook M dishes in the same time. Tonight he is going to have a hearty dinner with his girlfriend at his home. Of course, Coach Gao is going to cook all dishes himself, in …

实验楼Linux基础挑战2答案,实验楼-Linux基础-实验二 Linux的基本概念及操作

一、实验介绍1.1 实验内容实验楼环境介绍常用 Shell 命令及快捷键Linux 使用小技巧1.2 实验知识点Linux 基本命令通配符的使用查看帮助文档二、桌面环境1.Linux 桌面环境介绍相对于现在的 Windows 系统&#xff0c;UNIX/Linux 本身是没有图形界面的&#xff0c;我们通常在 UNIX…

【ZOJ - 3780】Paint the Grid Again(拓扑排序,图论,证明性质)

题干&#xff1a; Leo has a grid with N N cells. He wants to paint each cell with a specific color (either black or white). Leo has a magical brush which can paint any row with black color, or any column with white color. Each time he uses the brush, the…

linux bin su,linux – su:/ bin / bash:资源暂时不可用

无法将用户切换为postgres.postgres用户的ulimit设置设置了合理的限制.我们没有达到最高限度./ var / log / messages中没有错误.BETA -bash-4.2# sudo su - postgressu: /bin/bash: Resource temporarily unavailable设置&#xff1a;BETA -bash-4.2# ps -auxww | grep -i pos…

*【ZOJ - 3781】Paint the Grid Reloaded(dfs求连通块缩点,bfs求最短路,建图技巧)

题干&#xff1a; Leo has a grid with N rows and M columns. All cells are painted with either black or white initially. Two cells A and B are called connected if they share an edge and they are in the same color, or there exists a cell C connected to both…

c语言程序图片马赛克,关于c语言的图像均值滤波 请问大神为什么我的结果都是马赛克...

该楼层疑似违规已被系统折叠 隐藏此楼查看此楼#include #include #include #include #include #include #include "stdlib.h"#include "string.h"#define width 256#define higth 256//原图象的宽度和高度int lvbo(unsigned char D[]){int a;a(D[0]D[1]D[2…

【HDU - 1533】Going Home(网络流,二分图最优匹配,KM算法)

题干&#xff1a; On a grid map there are n little men and n houses. In each unit time, every little man can move one unit step, either horizontally, or vertically, to an adjacent point. For each little man, you need to pay a $1 travel fee for every step h…

c语言程序设计现代方法快速排序,C语言实现快速排序改进版

利用三者取中法改进快速排序&#xff0c;具体内容如下实现取数组中第一个,中间和最后一个元素的中间元素作为划分元素(否则将这些元素排除在划分过程之外).大小为11或更小的数组在划分过程中被忽略,然后使用插入排序来完成排序.#include #include #include #include #include #…

c语言一个数组后添加元素append,jQuery 追加元素、拼接元素的方法总结(append、html、insertBefore、before等)...

1. append & appendTo 的功能均为&#xff1a;在被选元素结尾(仍在元素内部)插入指定内容&#xff0c;但是内容和选择器的位置不同(1) append()方法&#xff1a;$("#test").append("测试"); //在id为test元素内部末尾插入测试(2) appendTo()方法&…