题目:

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. fread 和fgets 函数的使用

    两个函数都是对FILE *fp 文件进行读取信息,fgets是每次读取一行,fread是一下子读完所有的文件内容. //一.fread的使用 FILE *fp; int nread; ] fp = f ...

  2. @ResponseBody与@RestController

    @ResponseBody与@RestController的作用与区别 https://blog.csdn.net/xfl4629712/article/details/78528387

  3. mysql创建视图和存储过程,变量

    创建视图 sql>create view 视图名 as select语句; 修改视图并添加别名 sql>create or replace view empvu10 (employee_n ...

  4. Debian8.8下的VIM的配置文件

    传动们:http://blog.csdn.net/gatieme/article/details/43883261?spm=5176.100239.blogcont47532.3.yXiEuB 感觉挺 ...

  5. rsync实战(二)

    加两个需求:1.增加一个模块2.每个模块不同的用户名 步骤: .修改配置文件/etc/rsyncd.conf [backup] comment = commit path = /backup auth ...

  6. remove_if 的效率测试

    #include <iostream> #include <functional> #include <vector> #include <algorithm ...

  7. Monkey日常测试命令

    一,LOG日志抓取 adb  logcat -b main -v time >log.txt --实时日志打印 adb shell monkey -p com.eeyescloud.eeyes  ...

  8. NOIP 2004 T3 合唱队形(DP、最长上升/下降子序列)

    链接:https://ac.nowcoder.com/acm/contest/1082/C来源:牛客网 题目描述 N位同学站成一排,音乐老师要请其中的(N-K)位同学出列,使得剩下的K位同学排成合唱队 ...

  9. jenkins_2

    1.jenkins pipline:一些列jenkins插件将整个CD(持续交付过程)用解释性代码Jenkinsfile来描述(之前的都是通过配置设置的,这次是通过file) 2.创建一个流水线任务 ...

  10. ionic3 修改页面切换动画

    在app.module.ts中 配置pageTransition属性 [ BrowserModule, IonicModule.forRoot(MyApp, { pageTransition: 'io ...