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的一个邻接点, ...
随机推荐
- Maven--仓库的分类
对于 Maven 仓库来说,仓库只分为两类:本地仓库和远程仓库. 当 Maven 根据坐标寻找构件的时候,它首先会查看本地仓库,如果本地仓库存在此构件,则直接使用:如果本地仓库不存在此构件,或者需要查 ...
- java内存区域与内存溢出异常(2)
3.本地方法栈 本地方法栈与虚拟机栈作用相同,不同的是虚拟机栈为java方法服务,本地方法栈为native方法服务,本地方法栈会抛出StackOverFlowError和OutOfMemoryErro ...
- uniapp结合小程序第三方插件“WechatSI”实现语音识别功能,进而实现终端控制
最近在用soket实现终端控制器的功能,然后就想用语音控制,这样显得更AI WechatSI在manifest.json中配置: 在vue中插入如下展示代码: <view class=" ...
- 2019-2020-1 20199324《Linux内核原理与分析》第九周作业
第八章 进程的切换和系统的一般执行过程 1.进程调度的时机 硬中断和软中断 中断:在本质上都是软件或者硬件发生了某种情形而通知处理器的行为,处理器进而停止正在运行的指令流(当前进程),对这些通知做出相 ...
- IOC&AOP
- 14 微服务电商【黑马乐优商城】:day03-springcloud(Hystix,Feign)
本项目的笔记和资料的Download,请点击这一句话自行获取. day01-springboot(理论篇) :day01-springboot(实践篇) day02-springcloud(理论篇一) ...
- [学习笔记]连通分量与Tarjan算法
目录 强连通分量 求割点 求桥 点双连通分量 模板题 Go around the Labyrinth 所以Tarjan到底怎么读 强连通分量 基本概念 强连通 如果两个顶点可以相互通达,则称两个顶点强 ...
- ionic3 发布订阅者模式实现
在ionic3 中实现订阅发布模式,需要用到Events. Events下面有三个方法 events.subscribe() 订阅 events.publish() 发布 events.unsub ...
- J - Association of Cats and Magical Lights Kattis - magicallights (树状数组+dfs序)
Rar the Cat((™)) is the founder of ACM (Association of Cats and Magical Lights). This is the story o ...
- PAT甲级——1036 Boys vs Girls
1036 Boys vs Girls This time you are asked to tell the difference between the lowest grade of all th ...