首页 > 代码库 > poj 2531

poj 2531

Network Saboteur
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 9101 Accepted: 4259

Description

A university network is composed of N computers. System administrators gathered information on the traffic between nodes, and carefully divided the network into two subnetworks in order to minimize traffic between parts. 
A disgruntled computer science student Vasya, after being expelled from the university, decided to have his revenge. He hacked into the university network and decided to reassign computers to maximize the traffic between two subnetworks. 
Unfortunately, he found that calculating such worst subdivision is one of those problems he, being a student, failed to solve. So he asks you, a more successful CS student, to help him. 
The traffic data are given in the form of matrix C, where Cij is the amount of data sent between ith and jth nodes (Cij = Cji, Cii = 0). The goal is to divide the network nodes into the two disjointed subsets A and B so as to maximize the sum ∑Cij (i∈A,j∈B).

Input

The first line of input contains a number of nodes N (2 <= N <= 20). The following N lines, containing N space-separated integers each, represent the traffic matrix C (0 <= Cij <= 10000). 
Output file must contain a single integer -- the maximum traffic between the subnetworks. 

Output

Output must contain a single integer -- the maximum traffic between the subnetworks.

Sample Input

3
0 50 30
50 0 40
30 40 0

Sample Output

90

Source

Northeastern Europe 2002, Far-Eastern Subregion

题目意思是要把n个点随机分成两组,求一组中每个点到另外一组中每个点的距离和的最大值。

枚举每个分组情况,计算就行了。

AC代码:

#include<iostream>
#include<vector>
using namespace std;
int a[30][30];
vector <int> v1;
vector <int> v2;
int main(){
    int n;
    while(cin>>n){
        for(int i=1;i<=n;i++)
            for(int j=1;j<=n;j++)
                cin>>a[i][j];
        int ans=0;
        for(int k=1;k<(1<<n)-1;k++){     //分组情况为 1000..0——1111..0 其中0为一组,1为另一组。
            if(!v1.empty())
                v1.clear();
            if(!v2.empty())
                v2.clear();
            int tmp=k;
            for(int i=1;i<=n;i++){
                if(tmp&1)
                    v1.push_back(i);       //把1组的点保存在v1中
                else
                    v2.push_back(i);       //把0组的点保存在v2中
                tmp=tmp>>1;
            }
            int s=0;
            for(int i=0;i<v1.size();i++)          //求出1组的点到0组的点的距离和
                for(int j=0;j<v2.size();j++)
                    s+=a[v1[i]][v2[j]];           
            if(s>ans)
                ans=s;
        }
        cout<<ans<<endl;
    }
    return 0;
}