1. 程式人生 > >C - Network Saboteur :不熟悉

C - Network Saboteur :不熟悉

reassign assign between color fort col failed ssi grunt

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

題目意思:
有多個結點,每個結點之間有距離,把這些結點分成兩部分,A部分結點之間認為沒有距離,但與B部分每個結點都有距離,求使其最大的距離


解法:
DFS暴力枚舉的方法


 1 #include<iostream>
 2 
 3 using namespace std;
 4 
 5 const int Max = 21;
 6 const bool A = true;
 7 const bool B = false;
 8 
 9 int n, map[Max][Max], ans = 0;
10 bool set[Max];
11 
12 void dfs(int dep, int sum)
13 {
14     if(dep > n){
15         if(sum > ans)
16             ans = sum;
17         return;
18     }
19 
20     int m;
21     m = 0;
22     set[dep] = A;
23     for(int i = 1; i <= dep; i ++)
24         if(set[i] == B)
25             m += map[i][dep];
26     dfs(dep + 1, sum + m);
27 
28     m = 0;
29     set[dep] = B;
30     for(int i = 1; i <= dep; i ++)
31         if(set[i] == A)
32             m += map[i][dep];
33     dfs(dep + 1, sum + m);
34 }
35 
36 int main()
37 {
38     cin >> n;
39     for(int i = 1; i <= n; i ++)
40         for(int j = 1; j <= n; j ++)
41             cin >> map[i][j];
42     dfs(1, 0);
43     cout << ans << endl;
44     return 0;
45 }

C - Network Saboteur :不熟悉