POJ 2531 Network Saboteur 【DFS】

本文介绍了一个关于网络流量分配的问题,即如何通过重新分配大学网络中的计算机来最大化两个子网络间的流量。文章提供了完整的代码实现,并详细解释了问题背景及解决思路。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

 

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;

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值