题目:

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

题意:

第一行输入n然后输入n×n的矩阵dis[i][j]代表i到j需要消耗的数值,大致意思是把n个点分成A和B两个集合,求A集合中所有点到B集合中所有点消耗数值的和的最大值。

分析:

dfs从1号结点开始搜索,val记录当前情况消耗的数值。可以用vis数组表示两个集合0/1,当把一个元素从0集合移动到1集合时,这个集合的vis从0变为1,它要加上所有初它自己外它到0集合中元素的数值,它要减去所有初它自己外它到1集合中元素的数值(因为改元素原本在0集合中,val中含有它到1元素数值之和,当它变为1元素后要减去)

代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn = 25;
int dis[maxn][maxn],vis[maxn],ans = 0,n;
void dfs(int u,int val){
vis[u] = 1;
int tmp = val;
for (int i = 1; i <= n; i++){
if (!vis[i]) tmp += dis[u][i];
else tmp -= dis[u][i];
}
ans = max(ans,tmp);
for (int i = u+1; i <= n; i++){
dfs(i,tmp),vis[i] = 0;
}
}
int main(){
while (~scanf("%d",&n)){
ans = 0;
memset(vis,0,sizeof vis);
for (int i = 1; i <= n; i++){
for (int j = 1; j <= n; j++){
scanf("%d",&dis[i][j]);
}
}
dfs(1,0);
printf("%d\n",ans);
}
return 0;
}

POJ-2561 Network Saboteur(DFS)的更多相关文章

  1. poj 2531 Network Saboteur( dfs )

    题目:http://poj.org/problem?id=2531 题意:一个矩阵,分成两个集合,求最大的 阻碍量 改的 一位大神的代码,比较简洁 #include<stdio.h> #i ...

  2. poj 2531 Network Saboteur(经典dfs)

    题目大意:有n个点,把这些点分别放到两个集合里,在两个集合的每个点之间都会有权值,求可能形成的最大权值.   思路:1.把这两个集合标记为0和1,先默认所有点都在集合0里.             2 ...

  3. POJ 2531 Network Saboteur

    http://poj.org/problem?id=2531 题意 :有N台电脑,每两台电脑之间都有个通信量C[i][j]; 问如何将其分成两个子网,能使得子网之间的通信量最大. 也就是说将所有节点分 ...

  4. POJ 2531 Network Saboteur 位运算子集枚举

    题目: http://poj.org/problem?id=2531 这个题虽然是个最大割问题,但是分到dfs里了,因为节点数较少.. 我试着位运算枚举了一下,开始超时了,剪了下枝,1079MS过了. ...

  5. poj 2531 Network Saboteur 解题报告

    题目链接:http://poj.org/problem?id=2531 题目意思:将 n 个点分成两个部分A和B(也就是两个子集啦), 使得子集和最大(一定很难理解吧,呵呵).举个例子吧,对于样例,最 ...

  6. Network Saboteur(dfs)

    http://poj.org/problem?id=2531 不太理解这个代码... #include <stdio.h> #include <string.h> ][],v[ ...

  7. POJ 2531 Network Saboteur (枚举+剪枝)

    题意:给你一个图,图中点之间会有边权,现在问题是把图分成两部分,使得两部分之间边权之和最大. 目前我所知道的有四种做法: 方法一:状态压缩 #include <iostream> #inc ...

  8. PKU 2531 Network Saboteur(dfs+剪枝||随机化算法)

    题目大意:原题链接 给定n个节点,任意两个节点之间有权值,把这n个节点分成A,B两个集合,使得A集合中的每一节点与B集合中的每一节点两两结合(即有|A|*|B|种结合方式)权值之和最大. 标记:A集合 ...

  9. Network Saboteur 分类: 搜索 POJ 2015-08-09 19:48 7人阅读 评论(0) 收藏

    Network Saboteur Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 10147 Accepted: 4849 Des ...

随机推荐

  1. SPOJ ANARC05H 计数DP

    给定一个数字串,问有多少种拆分方法,题目所谓的拆分,就是分成若干个子块,每个块的和 即为各个数字相加,当前块的和一定要小于等于后面的块的和 比如1117  就有这些[1-117], [1-1-17], ...

  2. Maven与nexus关系

    一.了解Maven,Maven用来干什么呢 1. 优秀的构建工具 通过简单的命令,能够完成清理.编译.测试.打包.部署等一系列过程.同时,不得不提的是,Maven是跨平台的,无论是在Windows.还 ...

  3. 怎么在一个servlet中实现多个功能 ?如何使一个Servlet处理多个请求?

    自学javaweb一直不知道一个servelt可以有多个功能!看了别人代码才知道这个可以有! 平时你建立servelt时候你会吧doget和dopost这两个勾上,要想实现多个功能,你不必要勾选dog ...

  4. Arduino - ( Uno、Nano、Promini)针脚示意图

    Uno针脚示意图 Nano针脚示意图 Promini针脚示意图

  5. 19 01 19 视图 HttpReqeust对象 GET属性 POST属性 HttpResponse对象

    ---恢复内容开始--- URLconf 义,指定URL和视图函数的对应关系. 在应用内部创建urls.py文件,指定请求地址与视图的对应关系. url(正则,'视图函数名称') 1)如示例在book ...

  6. Codeforces_449B 最短路+统计

    也是给这个题目跪了一天...时间不多了,也不多讲 首先要用 nlogn的优先队列dijstla来求最短路,n^2的会超时,不过发现SPFA好像也可以过,他的复杂度应该介于NlogN和N^2之间. 然后 ...

  7. HZNU-ACM寒假集训Day6小结 线性DP

    线性DP 考虑一组硬币面值 1,5,11 给定W,求凑出W的最少硬币个数 我们记凑出n需要用到的最少硬币数量为f(n)   我们注意到了一个很棒的性质 : f(n)只与f(n-1) f(n-5) f( ...

  8. Tomcat解压版Windows配置(运行环境非开发环境)

    tomcat官网下载的9.0.19,解压后目录如下: java官网下载的jre8 (8u131),目录如下(应该是下载的解压版): 打开tomcat9.0.19根目录下的RUNNING.txt,里面有 ...

  9. git登录账号密码错误remote: Incorrect username or password (access token)

    git提交时弹框让输入用户和密码,不小心输入错误了 再次提交 一直就提示  remote: Incorrect username or password 错误了,也不弹框要重新输入 解决方法 win1 ...

  10. 题解P4201: [NOI2008]设计路线

    发现给出了一棵树, 不是树的情况直接输出-1 考虑进行DP, 设f[i][0/1/2]为i的子树中选小于等于0/1/2条边修路的方案数, 不妨对于一个节点, 先考虑正好相等的情况, 假设当前扫到了一个 ...