BZOJ 3943 [Usaco2015 Feb]SuperBull

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 as possible. A total of N (1 <= N <= 2000) teams are playing in the Superbull. Each team is assigned a distinct integer team ID in the range 1…2^30-1 to distinguish it from the other teams. The Superbull is an elimination tournament – after every game, Farmer John chooses which team to eliminate from the Superbull, and the eliminated team can no longer play in any more games. The Superbull ends when only one team remains.Farmer John notices a very unusual property about the scores in matches! In any game, the combined score of the two teams always ends up being the bitwise exclusive OR (XOR) of the two team IDs. For example, if teams 12 and 20 were to play, then 24 points would be scored in that game, since 01100 XOR 10100 = 11000.Farmer John believes that the more points are scored in a game, the more exciting the game is. Because of this, he wants to choose a series of games to be played such that the total number of points scored in the Superbull is maximized. Please help Farmer John organize the matches.貝西和她的朋友們在參加一年一度的“犇”(足)球錦標賽。FJ的任務是讓這場錦標賽儘可能地好看。一共有N支球隊參加這場比賽,每支球隊都有一個特有的取值在1-230-1之間的整數編號(即:所有球隊編號各不相同)。“犇”錦標賽是一個淘汰賽制的比賽——每場比賽過後,FJ選擇一支球隊淘汰,淘汰了的球隊將不能再參加比賽。錦標賽在只有一支球隊留下的時候就結束了。FJ發現了一個神奇的規律:在任意一場比賽中,這場比賽的得分是參加比賽兩隊的編號的異或(Xor)值。例如:編號爲12的隊伍和編號爲20的隊伍之間的比賽的得分是24分,因爲 12(01100) Xor 20(10100) = 24(11000)。FJ相信比賽的得分越高,比賽就越好看,因此,他希望安排一個比賽順序,使得所有比賽的得分和最高。請幫助FJ決定比賽的順序


【題目分析】
Prim求最大生成樹


【代碼】

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
int map[2001][2001],a[2001],dis[2001],n,vis[2001];
long long ans;
void prim()
{
    vis[1]=1;
    for (int i=1;i<=n;++i) dis[i]=map[1][i];
    for (int i=1;i<n;++i)
    {
        int tmp=0;
        for (int j=1;j<=n;++j) if (!vis[j]&&dis[j]>dis[tmp]) tmp=j;
        ans+=dis[tmp];vis[tmp]=1;
        for (int j=1;j<=n;++j) if (!vis[j]) dis[j]=max(dis[j],map[tmp][j]);
    }
}
int main()
{
    scanf("%d",&n);
    for (int i=1;i<=n;++i) scanf("%d",&a[i]);
    for (int i=1;i<=n;++i)
        for (int j=1;j<=n;++j)
            map[i][j]=a[i]^a[j];
    prim();
    printf("%lld\n",ans);
}
發佈了505 篇原創文章 · 獲贊 4 · 訪問量 12萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章