个人心得:对于深搜的使用还是不到位,对于递归的含义还是不太清楚!本来想着用深搜构成一个排列,然后从一到n分割成俩个数组,然后后面发现根本实现不了,思路太混乱。后来借鉴了网上的思想,发现用数组来标志,当值等于一时就属于A数组,等于0时属于B数组,这样就可以构成递归,即下一个数只有在A数组和不在A数组,发现这个思想真的挺好的。

DFS心得:若从第二步开始,动作变得程序化,像地图,只有上下左右,这个是只有为0不为0是就可以用深搜递归思想解决。

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
解题思路:题目大意:给定一个邻接矩阵,要求将顶点分为A,B两个集合,使得A集合中的所有顶点到B集合所有顶点的距离之和为最大。
首先两个集合的表示用一个一维数组A[],其中A[i]=1,表示节点i在集合A中,为0则在集合B中。
二位数组C[][]存储邻接矩阵,
由于每一个数字有两种选择0和1,结构适用深度优先:从第一个数开始A[0]=1(假设0号顶点一定在集合A中),
对0来说第2个顶点有两种情况,依次类推,结构就出来了。递归出口就是到达第n-1号顶点。
求和用两个for循环加上对存在集合的判定,记录最大值,每次求和结果与最大值比较,如果更大则修改最大值。
 #include<iostream>
#include<cstring>
#include<cstdio>
#include<queue>
using namespace std;
int n;
int sum;
int a[][];
int A[];
void dfs(int step)
{
if(step>n)
{
int t=;
for(int i=;i<=n;i++)
{
if(A[i]==)
for(int j=;j<=n;j++)
{
if(A[j]==)
t+=a[i][j]; }
}
if(sum==) sum=t;
if(sum<t) sum=t;
return ;
}
A[step]=;
dfs(step+);
A[step]=;
dfs(step+);
return ;
}
int main()
{
cin>>n;
sum=;
memset(A,,);
for(int i=;i<=n;i++)
for(int j=;j<=n;j++)
cin>>a[i][j];
dfs();
cout<<sum<<endl;
return ; }

Network Saboteur (深搜递归思想的特殊使用)的更多相关文章

  1. 剑指 Offer 13. 机器人的运动范围 + 深搜 + 递归

    剑指 Offer 13. 机器人的运动范围 题目链接 package com.walegarrett.offer; /** * @Author WaleGarrett * @Date 2020/12/ ...

  2. HDU-5423 Rikka with Tree。树深搜

    Rikka with Tree 题意:给出树的定义,给出树相似的定义和不同的定义,然后给出一棵树,求是否存在一颗树即和其相似又与其不同.存在输出NO,不存在输出YES. 思路:以1号节点为根节点,我们 ...

  3. ural 1353. Milliard Vasya's Function(背包/递归深搜)

    1353. Milliard Vasya's Function Time limit: 1.0 second Memory limit: 64 MB Vasya is the beginning ma ...

  4. ACM 海贼王之伟大航路(深搜剪枝)

    "我是要成为海贼王的男人!" 路飞他们伟大航路行程的起点是罗格镇,终点是拉夫德鲁(那里藏匿着"唯一的大秘宝"--ONE PIECE).而航程中间,则是各式各样的 ...

  5. 1016-Prime Ring Problem,素数环,深搜!

    Prime Ring Problem                                                                                   ...

  6. DFS-BFS(深搜广搜)原理及C++代码实现

    深搜和广搜是图很多算法的基础,很多图的算法都是从这两个算法中启发而来. 深搜简单地说就是直接一搜到底,然后再回溯,再一搜到底,一直如此循环到没有新的结点. 广搜简单地说就是一层一层的搜,像水的波纹一样 ...

  7. 利用深搜和宽搜两种算法解决TreeView控件加载文件的问题。

    利用TreeView控件加载文件,必须遍历处所有的文件和文件夹. 深搜算法用到了递归. using System; using System.Collections.Generic; using Sy ...

  8. CSU-ACM2016暑假集训训练2-DFS(C - Network Saboteur)

    C - Network Saboteur Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%lld & %llu ...

  9. hdu 5648 DZY Loves Math 组合数+深搜(子集法)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5648 题意:给定n,m(1<= n,m <= 15,000),求Σgcd(i|j,i&am ...

随机推荐

  1. range基础

    collapse这个方法是把结束位置抛弃掉,并不是简单的设置到开始位置. 结束位置被抛弃掉以后,只要没有给它重新设置位置,它就一直都会等 于开始位置.即使你修改了开始位置,结束位置还是会在修改后的开始 ...

  2. asp.net Cookie 用户登陆时记住我

    /// <summary> /// 判断Cookie中存储的数据 /// </summary> protected void CheckUserCookie() { //先判断 ...

  3. Linux文件系统管理 swap分区及作用

    概述 在安装系统的时候已经建立了 swap 分区.swap 分区是 Linux 系统的交换分区,当内存不够用的时候,我们使用 swap 分区存放内存中暂时不用的数据.也就是说,当内存不够用时,我们使用 ...

  4. python__Django 分页

    自定义分页的类: #!/usr/bin/env python # -*- coding: utf-8 -*- # Created by Mona on 2017/9/20 from django.ut ...

  5. 跨平台移动开发_Windows Phone 8 使用 PhoneGap 方法

    第一步,下载Windows Phone SDK 8.0 http://www.microsoft.com/zh-cn/download/details.aspx?id=35471 离线下载地址(推荐使 ...

  6. arguments解析

    js中并没有函数重载的概念,但函数的arguments参数能帮助我们模拟重载. arguments并不是真正的数组,但拥有length(参数数目),且能通过数组下标的方式进行访问,例如argument ...

  7. linux中获取堆栈空间大小的方法

    #include   <stdio.h> #include   <stdlib.h> #include   <sys/time.h> #include   < ...

  8. 一步一步粗谈linux文件系统(三)----超级块(superblock)【转】

    本文转载自:https://blog.csdn.net/fenglifeng1987/article/details/8302921 超级块是来描述整个文件系统信息的,可以说是一个全局的数据结构,可以 ...

  9. 【转载】丑数humble numbers

    转载地址:http://blog.csdn.net/qwerty_xk/article/details/12749961 题:只有2 3 5 这三个因子的数,求第1500个   设1为第一个丑数,求第 ...

  10. RHCE学习笔记 管理1 (第一、二章)

    第一章 命令行访问 1.Ctrl+alt+F2~F6 切到虚拟控制台,ctrl+alt+F1 回到图形界面 2.格式 : 命令 选项 参数 [] 为可选项目            ...表示该项目任意 ...