Network Saboteur(搜索)

Network Saboteur POJ2531
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 10351   Accepted: 4968

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
题目大意:给定x个元素的图,将这些点集合分成两部分A,B,然后取A中所有元素到B中所有元素的权值之和,求最大值。
搜索出所有的情况然后取最大值。对搜索还不是熟悉,看的题解做的。
 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <algorithm>
 5 using namespace std;
 6 int map[21][21];
 7 int n,Max,sum;
 8 bool flag[21];
 9 int dfs(int x)
10 {
11     if(x==n)
12     {
13         sum=0;
14         for(int i=1;i<=n;i++)
15             for(int j=1;j<=n;j++)
16                 if(flag[i]&&!flag[j])
17                     sum+=map[i][j];
18         if(sum>Max)
19             Max=sum;
20         return 0;
21     }
22     else            /*不断向下展开*/
23     {
24         flag[x]=1;
25         dfs(x+1);
26         flag[x]=0;
27         dfs(x+1);
28     }
29     return 0;
30 }
31 int main()
32 {
33     int i,j,k;
34     while(cin>>n)
35     {
36         Max=0;
37         for(i=1;i<=n;i++)
38             for(j=1;j<=n;j++)
39                 cin>>map[i][j];
40         memset(flag,0,sizeof(flag));
41         dfs(1);
42         cout<<Max<<endl;
43     }
44 }


原文地址:https://www.cnblogs.com/a1225234/p/4902848.html