ZOJ 3715 Kindergarten Election(枚举、贪心)

题目地址:点击打开链接


做的时候根本硬是想不出怎么去贪心或者dp。 其实数据量很小,n<=100,我们可以枚举1号获胜所需的票数,然后再

去贪心计算得到这个数量的票数且没人的票数>=他,所需的最小花费。


贴下别人的题解 : 


             题意:

                      在幼儿园里..每个小朋友投一票选举领导...得票最多的小朋友成为领导(若有多个..则多个领导)..现在有个小朋友相当唯一的领导..于是准备贿赂一些小朋友..让他们把票投给自己...每个小朋友要买通..必须满足给他的糖数...问这个小朋友最少用多少的糖使得他成为唯一的leader~

             题解:

                      今天做的练习赛里的...比赛中各种没想法..想DP..但状态无法表示...想贪心..但一直没找到一个明确的方法...好乱...

                      这种模式的题目感觉没做过什么...由于数据范围不大...先枚举咱们这个小朋友是拿的多少个糖赢的..记为t...然后让其他的小朋友糖数小于t(大于t的.则减少到t-1)..而改变这些必然把选票加到自己头上来..寻则所需糖数最少的来操作...当所有小朋友的票数<t了..看咱这个1号小朋友..若他得到的票数大于了t..枚举失败..继续往更大的t进行枚举..若等于t..那么就已经得到了当前枚举条件下的最佳方案..若<t...则需要将还未买通的小朋友中糖数需求最小的几个给买通..让其票数正好等于t...

                      这道题还有个问题是1号小朋友要向别人投票...向票数最少的投就行了..又不可能所有的小朋友票数为t-1( t+(t-1)*(N-1)=N无整数解)...所以这个其实不用考虑..


摘自:点击打开链接



#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn = 1e3+5;
const int INF = 0x3f3f3f3f;
int n, ans, sum[maxn], id[maxn], v[maxn], f[maxn];
bool used[maxn];
bool cmp(int a, int b) { return v[a] < v[b]; }

int main(void)
{
    int t;
    cin >> t;
    while(t--)
    {
        scanf("%d", &n);
        memset(sum, 0, sizeof(sum));
        for(int i = 2; i <= n; i++) scanf("%d", &f[i]), sum[f[i]]++;
        for(int i = 2; i <= n; i++) scanf("%d", &v[i]);
        ans = INF;
        for(int t = max(sum[1], 1); t < n; t++)
        {
            int tans = 0, tsum1 = sum[1];
            memset(used, 0, sizeof(used));
            for(int i = 2; i <= n; i++)
            {
                if(sum[i] >= t)
                {
                    tsum1 += sum[i]-(t-1);
                    int num = 0;
                    for(int j = 2; j <= n; j++)
                        if(f[j] == i) id[num++] = j;
                    sort(id, id+num, cmp);
                    for(int j = 1; j <= sum[i]-(t-1); j++)
                        tans += v[id[j-1]], used[id[j-1]] = 1;
                }
            }
            //票数大于了t
            if(tsum1 > t) continue;
            int num = 0;
            for(int j = 2; j <= n; j++)
                if(!used[j]) id[num++] = j;
            sort(id, id+num, cmp);
            //还不够t票,从其他人里收买
            for(int j = 1; j <= t-tsum1; j++)
                tans += v[id[j-1]];
            ans = min(ans, tans);
        }
        printf("%d\n", ans);
    }
    return 0;
}


Kindergarten Election

Time Limit: 2 Seconds       Memory Limit: 65536 KB

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 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.

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值