首页 > 代码库 > POJ 2531 暴力深搜

POJ 2531 暴力深搜

Network Saboteur
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 13494   Accepted: 6543

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
抄自 http://blog.csdn.net/martin31hao/article/details/8098302

题目大意:有n个点,把这些点分别放到两个集合里,在两个集合的每个点之间都会有权值,求可能形成的最大权值。

 思路:1、把这两个集合标记为0和1,先默认所有点都在集合0里。

            2、依次枚举每个点id,把每个点都放到集合1里去,这个时候就要调整集合的权值了,原来和id都在集合0里的点,要把权值加上;而在集合1里的点,要把权值减去。

            3、权值调整完毕后,和ans比较,如果比ans要大, 调整ans。

            4、如果放到集合1中,调整节点后的权值比放在集合0中要大,那么就默认这个点在集合1中,继续枚举下面的点进行DFS。最终是可以把最有状态都枚举出来的。

AC代码

 1 #include <stdio.h>
 2 #include <math.h>
 3 #include <string.h>
 4 #include <stdlib.h>
 5 #include <iostream>
 6 #include <sstream>
 7 #include <algorithm>
 8 #include <string>
 9 #include <queue>
10 #include <vector>
11 #define maxn 100005
12 #define maxm 50000
13 using namespace std;
14 typedef long long ll;
15 int g[25],a[25][25];
16 int ans,n;
17 void dfs(int k,int temp)
18 {
19   g[k]=1;
20   int t=temp;
21   for(int i=1;i<=n;i++)
22   {
23     if(g[i]==1)
24       t-=a[k][i];
25     else
26       t+=a[k][i];
27   }
28   if(t>ans)
29     ans=t;
30   for(int i=k+1;i<=n;i++)
31   {
32     if(t>temp)
33     {
34       dfs(i,t);
35       g[i]=0;
36     }
37   }
38 }
39 int main(int argc, char const *argv[])
40 {
41   while(scanf("%d",&n)!=EOF)
42   {
43     memset(g,0,sizeof(g));
44     for(int i=1;i<=n;i++)
45     {
46       for(int j=1;j<=n;j++)
47       {
48         cin>>a[i][j];
49       }
50     }
51     ans=0;
52     dfs(1,0);
53     cout<<ans<<endl;
54   } 
55   return 0;
56 }

这题貌似最大割

POJ 2531 暴力深搜