POJ2531 Network Saboteur

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

翻译:大学网络由N台电脑组成。系统管理员收集有关节点之间流量的信息,并仔细将网络分为两个子网,以最大限度地减少部件之间的流量。一位心怀不满的计算机科学系学生Vasya在被大学开除后决定报复。他侵入了大学网络,并决定重新分配计算机以最大化两个子网络之间的流量。不幸的是,他发现计算这种最坏的细分是他作为一个学生未能解决的那些问题之一。所以他问你,一个更成功的CS学生,帮助他。交通数据以矩阵C的形式给出,其中Cij是第i个和第j个节点之间发送的数据量(Cij = Cji,Cii = 0)。目标是将网络节点分成两个不相关的子集A和B,以便使ΣCij(i∈A,j∈B)的和最大。第一行输入包含许多节点N(2 <= N <= 20)。以下N行包含N个空格分隔的整数,分别代表流量矩阵C(0 <= Cij <= 10000)。


输出文件必须包含一个整数 - 子网之间的最大流量。


输出必须包含一个整数 - 子网之间的最大流量。


题意求其中一边所有集合每个单位到另一个集合所有单位的距离之和最大。于是使用二进制全排列的方法枚举使之分为两边。虽然该题分类在搜索中,但实际上DFS实质只是一种递归枚举的策略。而BFS实质是一种求解状态转移的最短步骤的策略。


#include <stdio.h>
#include <string.h> 
int value[30][30],book[30];
int sum=0,n;
void dfs(int k){
	int i,j,msum=0;
	if (k==n){
		for (i=0;i<n;i++){
			if (book[i]==0)	continue;//所有的1和0累加 
			for (j=0;j<n;j++){
				if (book[j]==0){
					msum+=value[i][j];
				}
			}
		}
		if (msum>sum){
			sum=msum;
		}
		return;
	}
	
	book[k]=1;
	dfs(k+1);//这里便是枚举的核心
	book[k]=0;
	dfs(k+1);
}
int main ()
{
	scanf ("%d",&n);
	int i,j;
	for (i=0;i<n;i++){
		for (j=0;j<n;j++){
			scanf ("%d",&value[i][j]);
		//	printf ("%d %d\n",i,j);
		}
	}
	dfs(0);
	printf ("%d\n",sum);
	memset(book,0,sizeof(book));
	memset(value,0,sizeof(value));
	sum=0;
	return 0;
} 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值