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的一个邻接点, ...
随机推荐
- c#学习笔记05——数组&集合
数组 声明数组 .一维数组的定义: 数据类型[] 数组名=new 数据类型[大小]; eg: ]; ,,,,}; ]; .多维数组的定义 ,];//定义二维数组 ,,];//定义三维数组 多维数组可以 ...
- Kali 安装 Google 中文输入法
前言 安装了 Linux 并设置中文后,为了操作起来更便捷准备安装一个中文输入法 之前安装搜狗输入法,由于搜狗输入法基于 qt4,估计短期是 GG 了 所以这次选择了 Google 输入法 以下是安装 ...
- SQL Link Oracle
转自:http://www.2cto.com/database/201107/96105.html 做项目过程中常用到数据库同步,现把前一段时间做的一个项目部分,同步过程贴出来,供分享与自己参考! 本 ...
- cocoaPods安装使用亲体验
一. cocoaPods的安装. 终端中输入: $ sudo gem install cocoapods 注意:直接在terminal中输入这个是安装不成功的,因此,我们可以通过淘宝的Ruby镜像来访 ...
- spark-shell使用指南. - 韩禹的博客
在2.0版本之前,Spark的主要编程接口是RDD(弹性分布式数据集),在2.0之后,则主推Dataset,他与RDD一样是强类型,但更加优化.RDD接口仍然支持,但为了更优性能考虑还是用Datase ...
- MTSP问题
问题描述:m个旅行商去旅游 n个城市,规定都必须从同一个出发点出发,而且返回原出发点,需要将所有的城市遍历完毕,每个城市只能游历一次,但是为了路径最短可以路过这个城市多次.这个就是多旅行商问题.是在T ...
- G - Radar Scanner Gym - 102220G(中位数~~)
zThere are n rectangle radar scanners on the ground. The sides of them are all paralleled to the axe ...
- TZOJ-STL系列题
C++实验:STL之vector #include <bits/stdc++.h> using namespace std; void Input(vector<int>&am ...
- http接口与webservice接口的区别
常见的API接口有两类:http接口和webservice接口. http接口走http协议,通过路径来区分调用方法,请求报文一般是key-value形式的,返回报文一般是json串,常用的是get和 ...
- Xming+SecureCRT的安装与使用
博主本人平和谦逊,热爱学习,读者阅读过程中发现错误的地方,请帮忙指出,感激不尽 Xming下载地址:https://xming.en.softonic.com/ 安装完后打开文件位置: 一.Xming ...