Network Saboteur (DFS)
题目:
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
Output file must contain a single integer -- the maximum traffic between the subnetworks.
Output
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)的更多相关文章
- poj 2531 Network Saboteur( dfs )
题目:http://poj.org/problem?id=2531 题意:一个矩阵,分成两个集合,求最大的 阻碍量 改的 一位大神的代码,比较简洁 #include<stdio.h> #i ...
- Network Saboteur(搜索)
Network Saboteur POJ2531 Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 10351 Accept ...
- POJ 2531-Network Saboteur(DFS)
Network Saboteur Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 9435 Accepted: 4458 ...
- C——Network Saboteur (POJ2531)
题目: A university network is composed of N computers. System administrators gathered information on t ...
- LeetCode Subsets II (DFS)
题意: 给一个集合,有n个可能相同的元素,求出所有的子集(包括空集,但是不能重复). 思路: 看这个就差不多了.LEETCODE SUBSETS (DFS) class Solution { publ ...
- LeetCode Subsets (DFS)
题意: 给一个集合,有n个互不相同的元素,求出所有的子集(包括空集,但是不能重复). 思路: DFS方法:由于集合中的元素是不可能出现相同的,所以不用解决相同的元素而导致重复统计. class Sol ...
- HDU 2553 N皇后问题(dfs)
N皇后问题 Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Description 在 ...
- 深搜(DFS)广搜(BFS)详解
图的深搜与广搜 一.介绍: p { margin-bottom: 0.25cm; direction: ltr; line-height: 120%; text-align: justify; orp ...
- 【算法导论】图的深度优先搜索遍历(DFS)
关于图的存储在上一篇文章中已经讲述,在这里不在赘述.下面我们介绍图的深度优先搜索遍历(DFS). 深度优先搜索遍历实在访问了顶点vi后,访问vi的一个邻接点vj:访问vj之后,又访问vj的一个邻接点, ...
随机推荐
- php安装swoole2.1.2
准备环境: cos7.2 & php 7.1.7 1.www.swoole.com找到对应版本2.使用git clone或则 wget 命令(下载后需解压)进行下载3.在swoole目录 ...
- js实际应用
-----------------------------获取select选中的值------------------------- 原生JS获得选中select值 var obj = documen ...
- shell时间函数
function getlastday(){ if [ $# -lt 2 ]; then echo "usage: getlastday month dayofweek" echo ...
- react webpack配置
- 二十四、SSH介绍
1.ssh介绍: SSH先对联机数据包通过加密技术进行加密处理,加密后在进行数据传输,确保了传递的数据安全.(运维的一大重视点就是要对安全敏感) 在当前的生产环境运维工作中,绝大多数企业都是SSH协议 ...
- 37)PHP,获取数据库值并在html中显示(晋级2)
下面的是上一个的改进版,我知道为啥我的那个有问题了,因为我的__construct()这个函数的里面的那个变量名字搞错了,哎,这是经常犯得毛病,傻了吧唧,气死我了. 之前的那个变量的代码样子: cla ...
- Winform下编译Dev控件时提示license.licx文件错误
有时候,用vs2005或2008,用到第3方控件的时候会自动生成licenses.licx.我用的是devexpress.在程序运行的时候总是出现dev的画面,很烦.在网上找了找,找到去掉画面的方法: ...
- java连接access的用户名、密码异常Decoding not supported解决
Java通过ucanaccess对Access数据库.accdb文件连接: public static Connection getConn() { try { String dbURL = &quo ...
- QTP基本循环异常遍历(代码方式实现)
0 环境 系统环境:win7 1 前言 在正常循环的基础下 添加异常处理遍历 一些基本操作 请看正常循环 https://www.cnblogs.com/my-ordinary/p/11739180. ...
- Rx系列---响应式编程
Rx是ReactiveX的简称,翻译过来就是响应式编程 首先要先理清这么一个问题:Rxjava和我们平时写的程序有什么不同.相信稍微对Rxjava有点认知的朋友都会深深感受到用这种方式写的程序和我们一 ...