POJ 2531 Network Saboteur 【DFS】


Network Saboteur
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 10084   Accepted: 4819

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


题意: 这题是我做过的最难理解的一道题,我把题意讲一下,估计大家都会做了,如下:

假设有 4 台电脑,则有 1,2,3.4; 在这4台电脑中拿 n 个电脑方进 A中,其他的放在B中,A中的电脑不能有连接,B内的电脑也不能有连接,要算的就是A中的每一个电脑与B中的每一个电脑连接,算这些连接的总流量,如何划成两部分使得值最大;

假设 A { 1 , 2 ,3 } ,B { 4 } ;那么要算的就是 【1,4】,【2,4 】,【3,4】;当然,可能的划分不止这种,找出划分的值最大的方案,然后输出那个值;



#include<iostream>
#include<string>
#include<cstring>
#include<cstdio>
#include<cstdlib>
#include<time.h>
#include<algorithm>
#include<cmath>
#include<stack>
#include<list>
#include<queue>
#include<map>

#define E exp(1)
#define PI acos(-1)
#define mod (ll)(1e9+9)
#define INF 0x3f3f3f3f;
#define MAX 40000
#define compare 0.00000001
#define exps 1e-8

//#define _CRT_SECURE_NO_WARNINGS
//#define LOCAL
using namespace std;

typedef long long ll;
typedef long double lb;

int a[22][22], res, n, book[22];

void dfs(int sum,int cur,int last) {
	int tmp = sum;
	
	for(int i = 0 ; i < n ; ++i) {
		if(cur == -1) break;
		if(book[i] == 0) tmp+=a[cur][i];
		else {
			if(last > -1)
			tmp-=a[i][cur]; // 与上面的正好相反 cur & i 
		}	
	}
	if(res < tmp) res = tmp;
	
	for(int i = cur + 1; i < n ; ++i) {
		book[i] = 1;
		dfs(tmp,i,cur);
		book[i] = 0;
	} 
	return ;
}


int main(void)
{
#ifdef LOCAL
	freopen("data.in.txt", "r", stdin);
	freopen("data.out.txt", "w", stdout);
#endif
	//ios::sync_with_stdio(false); cin.tie(0);
	while (scanf("%d",&n) != EOF) {
		for (int i = 0; i < n; ++i) {
			for (int j = 0; j < n; ++j) {
				scanf("%d", &a[i][j]);
			}
		}
		memset(book,0,sizeof(book));
		res = 0;
		dfs(0,-1,-1);
		printf("%d\n", res);
	}
	//	end = clock();
	//	cout << "using tmie:" << (double)(end - start) / CLOCKS_PER_SEC * (1000) << "ms" << endl;
	//system("pause");
	return 0;















猜你喜欢

转载自blog.csdn.net/godleaf/article/details/80427934
今日推荐