• 最小生成树


    题目描述

    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.

    输入描述:

    The first line contains the single integer N. The following N lines contain the N team IDs.

    输出描述:

    Output the maximum possible number of points that can be scored in the Superbull.

    输入

    4
    3
    6
    9
    10
    

    输出

    37
    

    说明

    OUTPUT DETAILS: One way to achieve 37 is as follows: FJ matches teams 3 and 9, and decides that 9 wins, so teams 6, 9, and 10 are left in the tournament. He then matches teams 6 and 9, and lets team 6 win. Teams 6 and 10 are then left in the tournament. Finally, teams 6 and 10 face off, and team 10 wins. The total number of points scored is (3 XOR 9) + (6 XOR 9) + (6 XOR 10) = 10 + 15 + 12 = 37.

    备注:

    NOTE: The bitwise exclusive OR operation, commonly denoted by the ^ symbol, is a bitwise operation that performs the logical exclusive OR operation on each position in two binary integers. The result in each position is 1 if only the first bit is 1 or only the second bit is 1, but is 0 if both bits are 0 or both are 1. For example: 10100 (decimal 20) XOR 01100 (decimal 12) = 11000 (decimal 24)

    i, j 两点花费是是s[i]^s[j],求最大生成树,由于是n*n的稠密图,选用prim算法,

    #include<bits/stdc++.h>
    using namespace std;
    inline char gc() {
        static char buf[100000],*L=buf,*R=buf;
        return L==R&&(R=(L=buf)+fread(buf,1,100000,stdin),L==R)?EOF:*L++;
    }
    template<typename T>
    inline void read(T&x) {
        int f=1;
        char ch=gc();
        x=0;
        while (ch<'0'||ch>'9') {
            if(ch=='-')f=-1;
            ch=gc();
        }
        while (ch>='0'&&ch<='9')x=(x<<3)+(x<<1)+ch-48,ch=gc();
        x*=f;
    }
    typedef long long ll;
    const int M=2010;
    ll v[M][M],n,dist[M],s[M];
    bool vised[M]= {0};
    ll prim() {
        memset(dist,-0x3f3f3f3f,sizeof(dist));
        dist[1]=0;
        ll ans=0,k;
        for(int i=1; i<=n; ++i) {
            k=-1;
            for(int j=1; j<=n; ++j)
                if(!vised[j]&&(k==-1||dist[j]>dist[k]))
                //不在树中且距离比当前大
                    k=j;
            vised[k]=1;
            ans+=dist[k];
            for(int j=1; j<=n; ++j)//更新距离
                if(!vised[j]&&dist[j]<v[k][j])
                    dist[j]=v[k][j];
        }
        return ans;
    }
    int main() {
        freopen("in.txt","r",stdin);
        read(n);
        for(int i=1; i<=n; ++i)read(s[i]);
        for(int i=1; i<=n; ++i)
            for(int j=i; j<=n; ++j)
                v[j][i]=v[i][j]=s[i]^s[j];//预处理,O(n^2)
        cout<<prim();
        return 0;
    }
    
  • 相关阅读:
    非常适合新手的一个Python爬虫项目: 打造一个英文词汇量测试脚本!
    代码遇到异常怎么办?不要慌,来看看这个!
    老司机要开车了!用Selenium+PhantomJS来抓取煎蛋网妹子图
    Python基础之格式化输出函数format()功能详解
    Python基础之常用格式化输出字符详解
    新手学python,如何才能更快升职加薪,迎娶白富美,走上人生巅峰
    ASP.NET MVC学习笔记 第二天
    ASP.NET MVC学习笔记 第一天
    ActiveMQ相关:
    WPF中使用定时器 DispatcherTimer 做TCP连接中的心跳 HeartBeat
  • 原文地址:https://www.cnblogs.com/foursmonth/p/14136287.html
Copyright © 2020-2023  润新知