Time Limit: 2000MS |
|
Memory Limit: 65536K |
Total Submissions: 13748 |
|
Accepted: 6700 |
Descriptionnode
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).
Inputios
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 Inputide
3
0 50 30
50 0 40
30 40 0
Sample Outputgrunt
90
Sourcespa
翻譯:
大學網絡由N臺電腦組成。系統管理員收集有關節點之間流量的信息,並仔細將網絡分爲兩個子網,以最大限度地減小部件之間的流量。
一位心懷不滿的計算機科學系學生Vasya在被大學開除後決定報復。他侵入了大學網絡,並決定從新分配計算機以最大化兩個子網絡之間的流量。
不幸的是,他發現計算這種最壞的細分是他做爲一個學生未能解決的那些問題之一。
因此他問你,一個更成功的CS學生,幫助他。交通數據以矩陣C的形式給出,其中Cij是第i個和第j個節點之間發送的數據量(Cij = Cji,Cii = 0)。目標是將網絡節點分紅兩個不相關的子集A和B,以便使ΣCij(i∈A,j∈B)的和最大。
第一行輸入包含許多節點N(2 <= N <= 20)。如下N行包含N個空格分隔的整數,分別表明流量矩陣C(0 <= Cij <= 10000)。
輸出文件必須包含一個整數 - 子網之間的最大流量。
輸出必須包含一個整數 - 子網之間的最大流量。
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
int n,ans;
int mp[25][25];
bool flag[25],A=1,B;
void dfs(int pos,int now){
if(pos>n){
if(now>ans)
ans=now;
return ;
}
int sum=0;
flag[pos]=A;
for(int i=1;i<pos;i++)
if(flag[i]==B) sum+=mp[pos][i];
dfs(pos+1,now+sum);
sum=0;flag[pos]=B;
for(int j=1;j<pos;j++)
if(flag[j]==A) sum+=mp[pos][j];
dfs(pos+1,now+sum);
}
int main(){
while(scanf("%d",&n)!=EOF){
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
scanf("%d",&mp[i][j]);
ans=0;
dfs(1,0);
printf("%d\n",ans);
}
}