Network Saboteur 简单dfs(), 计算怎样分两个集合 最优问题 用flag[] 数组标记
2013-10-12 16:08
323 查看
[align=left]Problem Description[/align]
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).
[align=left]Input[/align]
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.
[align=left]Output[/align]
Output must contain a single integer -- the maximum traffic between the subnetworks.
[align=left]Sample Input[/align]
3
0 50 30
50 0 40
30 40 0
[align=left]Sample Output[/align]
90
***************************************************************************************************************************dfs();wuxvjianzhi
***************************************************************************************************************************
View Code
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).
[align=left]Input[/align]
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.
[align=left]Output[/align]
Output must contain a single integer -- the maximum traffic between the subnetworks.
[align=left]Sample Input[/align]
3
0 50 30
50 0 40
30 40 0
[align=left]Sample Output[/align]
90
***************************************************************************************************************************dfs();wuxvjianzhi
***************************************************************************************************************************
#include<iostream> #include<string> #include<cstring> #include<queue> #include<cstdio> using namespace std; bool flag[1001]; int map[1001][1001]; int maxc,n; void dfs(int step) { if(step==n)//n个数遍历完时,计算结果,与最大值比较 { int sum=0; for(int i=1;i<=n;i++) for(int j=1;j<=n;j++) { if(flag[i]&&!flag[j])//分属两个不同的集合 sum+=map[i][j]; } if(maxc<sum) maxc=sum; return; } //两种情况都考虑 flag[step]=false; dfs(step+1); flag[step]=true; dfs(step+1); } int main() { while(scanf("%d",&n)!=EOF) { for(int i=1;i<=n;i++) for(int j=1;j<=n;j++) scanf("%d",&map[i][j]); maxc=-1; memset(flag,false,sizeof(flag)); dfs(1); printf("%d\n",maxc); } return 0; }
View Code
相关文章推荐
- R语言中两个数组(或向量)的外积怎样计算
- 【HDU5927 2016CCPC东北地区大学生程序设计竞赛 - 重现赛 F】【dfs序 + 线段树 or 树状数组 复杂度计算】Auxiliary Set 一个点如果是好点或是两个好点的LCA就是好
- 问题:取出两个数组相同值,最简单方法(面试陷阱)
- 再谈怎样以最简单的方法将泛型为String类型的集合或String类型的数组转化为逗号间隔字符串形式
- 交换两个变量的值的方法-----从简单问题入手
- struts2的s:iterator 可以遍历 数据栈里面的任何数组,集合等等 以下几个简单的demo:
- 两个集合合并成并去重的简单方法
- 三维数组A和一个简单c问题
- 矩阵图求最优最大值问题(简单dp)
- 题目1058 部分和问题 简单dfs
- swift 之 数组集合字典的简单使用
- javascript 简单实现对两个数组相似度的检验
- hdu1171 灵活的运用背包问题咯。。。 还有!!!! 合理的计算数组的范围!! wa了好多次!
- 计算两个有序数组的交集
- 关于VB简单计算问题
- 171116-两个简单的数组练习【连续第二十四天】
- (分治算法)两个有序数组中的中位数和Top K问题
- 两个PHP数组对比,计算新增了哪些值,删除了哪些值,共同拥有哪些值?
- 关于数组两个元素地址相减的问题
- 最优配对问题(集合上的动态规划) —— 状压DP