C - Network Saboteur

Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%lld & %llu

Description

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<stdio.h>
#include<cstring>
using namespace std;
int A[],B[],C[][],N,sum,tp=,tp2=;
void bfs(int tp,int l);
int main()
{
memset(A,,);
//memset(B,-1,21); while(scanf("%d",&N)!=EOF){
for(int i=;i<N;i++)
for(int j=;j<N;j++)
scanf("%d",&C[i][j]);
bfs(,);
printf("%d\n",sum);
} return ;
} void bfs(int tp,int l)
{
if(tp==N||tp==-) return;
int s=;
A[tp]=l;
for(int i=;i<N;i++)
{
if(A[i]==)
for(int j=;j<N;j++)
{
if(A[j]==)
s=s+C[i][j]; }
}
if(sum<s) sum=s;
bfs(tp+,);
bfs(tp+,);
}

CSU-ACM2016暑假集训训练2-DFS(C - Network Saboteur)的更多相关文章

  1. 2016HUAS暑假集训训练题 G - Oil Deposits

    Description The GeoSurvComp geologic survey company is responsible for detecting underground oil dep ...

  2. 2016huasacm暑假集训训练五 H - Coins

    题目链接:http://acm.hust.edu.cn/vjudge/contest/126708#problem/H 题意:A有一大堆的硬币,他觉得太重了,想花掉硬币去坐的士:的士司机可以不找零,但 ...

  3. 2016huasacm暑假集训训练五 J - Max Sum

    题目链接:http://acm.hust.edu.cn/vjudge/contest/126708#problem/J 题意:求一段子的连续最大和,只要每个数都大于0 那么就会一直增加,所以只要和0 ...

  4. 2016huasacm暑假集训训练五 G - 湫湫系列故事——减肥记I

    题目链接:http://acm.hust.edu.cn/vjudge/contest/126708#problem/G 这是一个01背包的模板题 AC代码: #include<stdio.h&g ...

  5. 2016huasacm暑假集训训练五 F - Monkey Banana Problem

    题目链接:http://acm.hust.edu.cn/vjudge/contest/126708#problem/F 题意:求至上而下一条路径的所经过的值得和最大值,这题比赛时就出了 但当时看不懂题 ...

  6. 2016huasacm暑假集训训练五 E - What Is Your Grade?

    题目链接:http://acm.hust.edu.cn/vjudge/contest/126708#problem/E 题意:给做出的题目个数,5个的100分,4个的前n/2的同学95,后n/2的90 ...

  7. 2016huasacm暑假集训训练五 C-Common Subsequence

    题目链接:http://acm.hust.edu.cn/vjudge/contest/126708#problem/C 题意:这是一道求字符串的公共子串的最大长度的题目,用dp动态方程即可 if(a[ ...

  8. 2016huasacm暑假集训训练四 DP_B

    题目链接:http://acm.hust.edu.cn/vjudge/contest/125308#problem/M 题意:有N件物品和一个容量为V的背包.第i件物品的费用是体积c[i],价值是w[ ...

  9. 2016huasacm暑假集训训练四 数论_B

    题目链接:http://acm.hust.edu.cn/vjudge/contest/125308#problem/G 题意:求有多少x(1<=x<=n),使得gcd(x,n)>=m ...

随机推荐

  1. delete table 和 truncate table

    delete table 和 truncate table 使用delete语句删除数据的一般语法格式: delete [from] {table_name.view_name} [where< ...

  2. linux集群时间同步

    说明:由于hadoop集群对时间要求很高,所以集群内主机要经常同步.本文档适合ubuntu.redhat系列. 注:很多内容是在网上摘录,然后试验后总结,如有疑问可留言探讨. 1.设置主机时间准确(任 ...

  3. 字母A-Z写法

    #大写的a-z,ASCII编码 65..90|%{[char]$_}     #小写的A-Z  97..122|%{[char]$_}    方法二: ([char[]](97..122) -as [ ...

  4. js调试技巧 Firefox调试技巧汇总

    Firebug入门指南        :  http://www.ruanyifeng.com/blog/2008/06/firebug_tutorial.html Firebug控制台详解: htt ...

  5. jQuery的自定义事件——滚轮

    这个案例类似于在地图上滚动滚轮,能缩小或者放大地图,分别用zoomIn和zoomOut来命名. 代码如下: //JS部分:<script src="jquery-1.10.2.min. ...

  6. oracle 基础SQL语句 多表查询 子查询 分页查询 合并查询 分组查询 group by having order by

    select语句学习 . 创建表 create table user(user varchar2(20), id int); . 查看执行某条命令花费的时间 set timing on: . 查看表的 ...

  7. Enable HTTPS in Spring Boot

    Spring-boot-enable-ssl Enable HTTPS in Spring Boot APRIL 14, 2015DRISS AMRI This weekend I answered ...

  8. 如何在C#中循环一个枚举

      在C#中要想迭代循环一个枚举,最容易想到的办法是直接进行循环,如下代码所示:   public enum Suit { Spades, Hearts, Clubs, Diamonds } publ ...

  9. tomcat服务器使用简介

    tomcat服务器的应用与部署:1:下载tomcat服务器可以到http://tomcat.apache.org/下载apache服务器,左侧有各种版本的服务器,可以根据自己的需要下载,如果是是Lin ...

  10. 关于SpringAOP的XML方式的配置

    AOP(XML)[理解][应用][重点] 1.AOP基础实例 A.导入jar包 核心包(4个)         日志(2个)             AOP(4个) Spring进行AOP开发(1个) ...