【BZOJ3943】【Usaco2015 Feb】SuperBull 最大生成树 Prim

链接:

#include <stdio.h>
int main()
{
    puts("转载请注明出处[vmurder]谢谢");
    puts("网址:blog.csdn.net/vmurder/article/details/44961149");
}

题意:

给n个数,然后每次可以选择一对尚存活的数,将其异或和加和到答案中,然后删掉其中一个数,直到只剩一个数为止。

题解:

花样教人理解最小生成树,一片苦心啊,不会最小生成树的可以从这开始理解2333。

对了,数据范围有点大,完全图 kruscal 多个 log 估计过不去。

代码:

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#define N 2050
#define inf 0x3f3f3f3f
using namespace std;

int n,a[N];
int dist[N];
bool vis[N];
long long prim()
{
    memset(dist,0xef,sizeof dist),dist[1]=0;
    long long ret=0;
    int i,j,x,t;
    for(i=1;i<=n;i++)
    {
        t=-inf;
        for(j=1;j<=n;j++)
            if(!vis[j]&&dist[j]>t)
                t=dist[j],x=j;
        vis[x]=1;
        ret+=dist[x];
        for(j=1;j<=n;j++)
            if(!vis[j])dist[j]=max(dist[j],(a[x]^a[j]));
    }
    return ret;
}

int main()
{
    freopen("test.in","r",stdin);

    int i,j,k;
    scanf("%d",&n);
    for(i=1;i<=n;i++)scanf("%d",&a[i]);
    cout<<prim()<<endl;

    return 0;
}
时间: 2024-10-07 19:36:14

【BZOJ3943】【Usaco2015 Feb】SuperBull 最大生成树 Prim的相关文章

bzoj3943[Usaco2015 Feb]SuperBull*

bzoj3943[Usaco2015 Feb]SuperBull 题意: n头牛进行锦标赛,每场比赛的好看程度是两头牛的编号异或和,并总有一方被淘汰.求安排比赛(可以决定比赛胜负)可以得到的最大总好看程度是多少.n≤2000 题解: 先求出牛两两之间的异或和,然后发现可以把比赛看做连边,且共有n-1场比赛,所以求最大生成树就行了.神犇们用的都是Prim,蒟蒻不会,用Kruscal结果时间排倒数. 代码: 1 #include <cstdio> 2 #include <cstring>

最大生成树 BZOJ3943 [Usaco2015 Feb]SuperBull

Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 374  Solved: 217[Submit][Status][Discuss] Description Bessie and her friends are playing hoofball in the annual Superbull championship, and Farmer John is in charge of making the tournament as exciting

[bzoj3943][Usaco2015 Feb]SuperBull_Kruskal

SuperBull bzoj-3943 Usaco-2015 Feb 题目大意:贝西和她的朋友们在参加一年一度的“犇”(足)球锦标赛.FJ的任务是让这场锦标赛尽可能地好看.一共有N支球队参加这场比赛,每支球队都有一个特有的取值在1-230-1之间的整数编号(即:所有球队编号各不相同).“犇”锦标赛是一个淘汰赛制的比赛——每场比赛过后,FJ选择一支球队淘汰,淘汰了的球队将不能再参加比赛.锦标赛在只有一支球队留下的时候就结束了.FJ发现了一个神奇的规律:在任意一场比赛中,这场比赛的得分是参加比赛两队

BZOJ 3943 Usaco2015 Feb SuperBull Prim

题目大意:给定n个数,每次选择两个数,将两数的异或值计入答案,并删掉其中一个,反复如此直到只剩一个数为止,求答案的最大值 每次将选择的两个数连边,那么显然会得到一棵树 用Prim算法求最大生成树即可 #include <cstdio> #include <cstring> #include <iostream> #include <algorithm> #define M 2020 using namespace std; int n,a[M]; long

bzoj3942: [Usaco2015 Feb]Censoring

AC自动机.嗯bzoj3940弱化版.水过去了(跑的慢啊QAQ.想了想可以用hash写.挖坑 #include<cstdio> #include<cstring> #include<iostream> #include<algorithm> #include<queue> using namespace std; #define rep(i,s,t) for(int i=s;i<=t;i++) #define clr(x,c) memset

BZOJ 3942: [Usaco2015 Feb]Censoring

3942: [Usaco2015 Feb]Censoring Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 404  Solved: 221[Submit][Status][Discuss] Description Farmer John has purchased a subscription to Good Hooveskeeping magazine for his cows, so they have plenty of material

bzoj 3940: [Usaco2015 Feb]Censoring -- AC自动机

3940: [Usaco2015 Feb]Censoring Time Limit: 10 Sec  Memory Limit: 128 MB Description Farmer John has purchased a subscription to Good Hooveskeeping magazine for his cows, so they have plenty of material to read while waiting around in the barn during

Bzoj3940 [Usaco2015 Feb]Censoring

Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 391  Solved: 183 Description Farmer John has purchased a subscription to Good Hooveskeeping magazine for his cows, so they have plenty of material to read while waiting around in the barn during milking

【Usaco2015 FEB】Cow Hopscotch (Gold)

Just like humans enjoy playing the game of Hopscotch, Farmer John's cows have invented a variant of the game for themselves to play. Being played by clumsy animals weighing nearly a ton, Cow Hopscotch almost always ends in disaster, but this has surp