题目:
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个点,每两个点之间都有一段距离,现在要把这些点分为两部分,使得一个部分的点到另一个部分所有的点的距离和最大。(学过离散的就知道这是一个完全图)比如题中的例子:就是一个K3图(3个顶点的完全图,一个三角形),设顶点为A,B,C.由题意可知:A到B的距离为30,B到C为40,C到A为50.当把A放一边,B,C放一边时,总距离为:A到B的距离(30)+A到C的距离(50)=80.当把C放一边,A,B一边时,总距离为AC+BC=90,另一种为30+40=70.所以最大的距离和为90.
看懂题目的可能都知道要用枚举来找最大距离和,但是怎么枚举呢?用for循环?肯定行不通的。这里就用”二进制枚举“来枚举所有情况并找到最大值。 二进制枚举(非递归):
用数组的值为0或1来把所有的点来分为两部分。
 int a[];
memset(a,,sizeof(a)); //清零。
for (int i=;i<n;i++) //n:枚举的次数。
{
a[]++; //第一位加一。
for (int j=;j<n;j++)
if (a[j]==)
{
a[j]=; //当这一位等于2时,又清零
a[j+]++; //并使下一位加一。
}
}

代码可能不好理解。下面模拟过程。
开始时的数组: 00000
a[0]++;后 10000
a[0]++等于2后,a[0]=0;a[0+1]++;01000
接下来是:
11000
00100
10100
01100
......
11111
这样就把所有的情况都枚举出来了。
下一步就只要把所有枚举中0或1的所有对应值求和,并找到最大值就可以了。
全部代码如下:
#include <iostream>
#include <cstring>
#include <cmath>
using namespace std;
int a[][],tab[]; int main()
{
int n,i,j,k,ans=,sum=,z;
cin >>n;
for (i=;i<=n;i++)
for (j=;j<=n;j++)
cin >>a[i][j];
k=pow(,n-); //枚举的情况总数。 for (i=;i<=k;i++)
{
tab[]++;
sum=;
for (j=;j<=n;j++)
if (tab[j]==)
{
tab[j]=;
tab[j+]++;
}
else break;
for (j=;j<=n;j++)
{
if (tab[j]==)continue; //找到每一个为1的点。
for (z=;z<=n;z++)
if (tab[z]==) //并把这个1到所有0的距离求和。
sum+=a[j][z];
}
ans=max(ans,sum); //找到最大的的和。
}
cout <<ans<<endl;
return ;
}

												

C——Network Saboteur (POJ2531)的更多相关文章

  1. Network Saboteur(搜索)

    Network Saboteur POJ2531 Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 10351   Accept ...

  2. Network Saboteur (DFS)

    题目: A university network is composed of N computers. System administrators gathered information on t ...

  3. poj 2531 Network Saboteur( dfs )

    题目:http://poj.org/problem?id=2531 题意:一个矩阵,分成两个集合,求最大的 阻碍量 改的 一位大神的代码,比较简洁 #include<stdio.h> #i ...

  4. 网络层-network layer(下):网络互连、子网掩码计算方法、Ipv4报头解析

    第五章 网络层-Network Layer(下) 上一章讲了网络层的任务.提供的两种服务.五个重要的路由算法.以及网络层的拥塞控制和服务质量问题.这一部分主要讲一讲网络互连问题和Internet的网络 ...

  5. Neural Network学习(二)Universal approximator :前向神经网络

    1. 概述 前面我们已经介绍了最早的神经网络:感知机.感知机一个非常致命的缺点是由于它的线性结构,其只能做线性预测(甚至无法解决回归问题),这也是其在当时广为诟病的一个点. 虽然感知机无法解决非线性问 ...

  6. Neural Network学习(一) 最早的感知机:Perceptron of Rosenblatt

    1. Frank Rosenblatt 首先介绍的是神经网络的开山祖师,先放张图拜拜 Frank Rosenblatt出生在纽约,父亲是医生,其1956年在Cornell大学拿到博士学位后,留校任教, ...

  7. POJ 2531-Network Saboteur(DFS)

    Network Saboteur Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 9435   Accepted: 4458 ...

  8. Network Saboteur (深搜递归思想的特殊使用)

    个人心得:对于深搜的使用还是不到位,对于递归的含义还是不太清楚!本来想着用深搜构成一个排列,然后从一到n分割成俩个数组,然后后面发现根本实现不了,思路太混乱.后来借鉴了网上的思想,发现用数组来标志,当 ...

  9. POJ 2531 Network Saboteur (枚举+剪枝)

    题意:给你一个图,图中点之间会有边权,现在问题是把图分成两部分,使得两部分之间边权之和最大. 目前我所知道的有四种做法: 方法一:状态压缩 #include <iostream> #inc ...

随机推荐

  1. fpm 制作 rpm 包

    支持的 源类型包 ① dir : 将目录打包成所需要的类型, 可用于源码编译安装软件包 ② rpm : 对 rpm 包进行转换 ③ gem : 对 rubygem 包进行转换 ④ python : 将 ...

  2. 常用DOM API

    Node Node是一个接口,中文叫节点,很多类型的DOM元素都是继承于它,都共享着相同的基本属性和方法.常见的Node有 element,text,attribute,comment,documen ...

  3. 数组 list互转

    数组 list互转 String str[] = list.toArray(new String[]{}); List list= java.util.Arrays.asList(String str ...

  4. 如何退出 Vim

    点击 Esc 键,; Vim 进入命令模式.然后输入: :q  退出(这是 :quit 的缩写) :q! 不保存退出(这是  :quit! 的缩写) :wq 写入文件并退出:(这是 :writequi ...

  5. shell中source与sh区别

    shell中使用source conf.sh,是直接运行conf.sh的命令,不创建子shell,类似与html中include,而sh是则创建子shell, 子shell里面 的变量父shell无法 ...

  6. DDD理论学习系列(8)-- 应用服务&领域服务

    DDD理论学习系列--案例及目录 1. 引言 单从字面理解,不管是领域服务还是应用服务,都是服务.而什么是服务?从SOA到微服务,它们所描述的服务都是一个宽泛的概念,我们可以理解为服务是行为的抽象.从 ...

  7. 个人开源项目testall 持续更新中···

    项目在GitHub上:https://github.com/x113773/testall ,喜欢的给个星星呀,亲~ 打算把用到过的和学习过的,所有前后端技术都集成到这个项目里,并在issues里配以 ...

  8. 数据结构(C语言版)链表相关操作算法的代码实现

    这次实现的是带头结点的单链表的初始化.遍历.创建.插入.删除.判断链表是否为空.求链表长度函数,编译环境是vs2013. 其中插入和删除函数中循环的条件目前还不太明白. #include<ios ...

  9. [UWP]浅谈按钮设计

    一时兴起想谈谈UWP按钮的设计. 按钮是UI中最重要的元素之一,可能也是用得最多的交互元素.好的按钮设计可以有效提高用户体验,构造让人眼前一亮的UI.而且按钮通常不会影响布局,小小的按钮无论怎么改也不 ...

  10. var与let的区别

    var与let的区别 前言: 在没接触Es6之前,我们在js中声明都是通过var来声明变量的,var声明变量虽说方便,但是,又有一些自己的诟病,下边来说一说,这三个的区别! var var相信大家都不 ...