题目:

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

题意:
这个题目看了好久,都不知道在讲个什么东西,百度也弄了好久,终于找到了有很好解析的解题报告;
我理解的大概就是这个意思啦!
输入一个数代表子网的数量;
例如:题目例子为3
1 2 3
1 0 50 30
2 50 0 40
3 30 40 0
总共有3个子网,第1个子网和第2个子网之间的通信量为50,第1个子网与第3个子网之间的通信量为30;
就是这个意思差不多了!!!
题目要求的是让你把这3个子网分为两个部分,使得通信量最大;
还是上面这个例子:
通信量最大的情况是把1,2,3三个子网分为{1,3}和{2};
即子网2和子网1,3之间的通信量是最大的
num_max=map[1][2]+map[3][2];
num_max=90;
我刚开始以为就只有三个子网,以为将每一行想相加就能得到最大值(笑cry),后来发现是20以内的子网数量;
那就需要用DFS啦!(看解析看了好久才理解怎么写)
那我们一开始就将所有的子网都放在一个集合中,另外一个是空的; AC代码:
#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
int map[][];
int f[];
int n;
int num_max;
void dfs(int num,int sum)
{
f[num]=; //做个标记,好知道我把谁扔空集合里面了
int total=sum; // sum之前的,total是处理之后的
for (int i=;i<=n;i++)
{
if (f[i]==)
total-=map[num][i]; //与num在一个集合里面,减去权值
else
total+=map[num][i]; //与num不在一个集合里面的,加上权值
}
num_max=num_max>total?num_max:total; //取最大值
for (int i=num+;i<=n;i++)
{
if (total>sum)
{
dfs(i,total); //这个我理解的是:比如我输入4,集合就会有{1,2},{1,3},{1,4},{2,3},{2,4},{3,4},{1,2,3},{4},{1,2,4},{3},{2,3,4},{1},{1,3,4},{2}
f[i]=; // 像第一次DFS时,num=1;这时第一个i取2,就会有{1,2}和{3,4}集合,
} //循环完第一次以后,f[0]=0,就是讲我倒回来把2从新集合中踢出去,i=3,把3丢进来,循环往复
}
}
int main()
{
while (scanf("%d",&n)==)
{
memset(f,,sizeof(f));
for (int i=;i<=n;i++)
for (int j=;j<=n;j++)
cin>>map[i][j];
num_max=-;
dfs(,); //将1放入到空集合中
cout << num_max << endl;
} return ;
}
觉得好难啊!!!主要是没怎么接触过递归!!!看了好久才理清楚的思路!!!
 

Network Saboteur (DFS)的更多相关文章

  1. poj 2531 Network Saboteur( dfs )

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

  2. Network Saboteur(搜索)

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

  3. POJ 2531-Network Saboteur(DFS)

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

  4. C——Network Saboteur (POJ2531)

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

  5. LeetCode Subsets II (DFS)

    题意: 给一个集合,有n个可能相同的元素,求出所有的子集(包括空集,但是不能重复). 思路: 看这个就差不多了.LEETCODE SUBSETS (DFS) class Solution { publ ...

  6. LeetCode Subsets (DFS)

    题意: 给一个集合,有n个互不相同的元素,求出所有的子集(包括空集,但是不能重复). 思路: DFS方法:由于集合中的元素是不可能出现相同的,所以不用解决相同的元素而导致重复统计. class Sol ...

  7. HDU 2553 N皇后问题(dfs)

    N皇后问题 Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Description 在 ...

  8. 深搜(DFS)广搜(BFS)详解

    图的深搜与广搜 一.介绍: p { margin-bottom: 0.25cm; direction: ltr; line-height: 120%; text-align: justify; orp ...

  9. 【算法导论】图的深度优先搜索遍历(DFS)

    关于图的存储在上一篇文章中已经讲述,在这里不在赘述.下面我们介绍图的深度优先搜索遍历(DFS). 深度优先搜索遍历实在访问了顶点vi后,访问vi的一个邻接点vj:访问vj之后,又访问vj的一个邻接点, ...

随机推荐

  1. python字符串——"奇葩“的内置函数

      一.前言 python编程语言里的字符串与我们初期所学的c语言内的字符串还是有一定不同的,比如python字符串里的内置函数就比语言的要多得多:字符串内的书写格式也会有一点差异,例:字符串内含有引 ...

  2. windows server 2012 安装sql server集群

    第一步:准备工作 虚拟环境下模拟创建: 准备好3台虚拟机 操作系统,WindowsServer2012R2 操作系统安装完成后,需要注意如果虚拟机是克隆出来的,后面操作集群的时候需要计算机的sid不同 ...

  3. 可视化---seaborn

    变量说明 x,y,hue 数据集变量 变量名 date 数据集 数据集名 row,col 更多分类变量进行平铺显示 变量名 col_wrap 每行的最高平铺数 整数 estimator 在每个分类中进 ...

  4. 关于tomcat启动错误:At least one JAR was scanned for TLDs yet contained no TLDs

    一.问题原因: 1.出现这个问题的原因就是Tomcat启动时会扫描大量jar包,如果含有不符合TLD规范的就会出现这个问题 2.以后基本上不会使用JSP作为视图层,所以我们可能根本不需要TLD这个东西 ...

  5. 发布订阅--SQLServer复制需要有实际的服务器名称才能连接到服务器,请指定实际的服务器名

    最近在学习SQL SERVER的高级复制技术的时候,遇到一个小问题,就是用本地SQL SERVER连接服务器的数据库时,在查看复制功能的发布服务器时,连接不上,弹出一个错误提示框架,如下: 原来在自己 ...

  6. [原]PInvoke导致栈破坏

    原, 总结, 调试, 调试案例  项目中遇到一个诡异的问题,程序在升级到.net4.6.1后会崩溃,提示访问只读内存区.大概现象如下: debug版不崩溃,release版稳定崩溃. 只有x64位的程 ...

  7. TPO1-2 The Origin of Theater

    Stories (myths) may then grow up around a ritual. Frequently the myths include representatives of th ...

  8. 部署企业本地yum源及源码包安装

    YUM命令 yum list //列出每个软件包(包括未安装和已安装) rpm -q repolist //列出所以仓库名称 info //查看软件信息 rpm -qi install //安装 rp ...

  9. 03-kubeadm初始化Kubernetes集群

    请求地址https://pc-shop.xiaoe-tech.com/appc7XFLu4K9234/video_details?id=v_5b615b9e432f5_DXDGopmB

  10. Python实现求1-1000以内的素数

    def func(): for i in range(2,1000): # count表示被整除的次数 count = 0 for j in range(1,i+1): if i%j==0: coun ...