题目:

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. 了解C#

    了解C C#能编写那些程序 Windows桌面应用程序 桌面应用有自己独立的进程与操作系统进行消息通讯,操作系统对事件进行检测,传递给桌面应用进程,桌面应用进程对这些消息进行解释,处理后,把处理结果u ...

  2. .net Form 的Autoscalemode属性

    .net Form 的Autoscalemode属性如果设置成Font 将会随着系统字体的大小来改变form大小 有时候会造成布局混乱,小心使用

  3. 七、React表单详解 约束性和非约束性组件 input text checkbox radio select textarea 以及获取表单的内容

    一.约束性和非约束性组件: 非约束性组: MV: <input type="text" defaultValue="a" /> 这个 default ...

  4. 【转载】使用driver.findElement(By.id("txtPhoneNum")).getText();获取文本

    今天在写自动化测试脚本的时候要获取一个输入框中的文本写了如下脚本: getAndSwitch("http://cas.minshengnet.com:14080/register/eRegi ...

  5. mybatis配置约束config,mapper

    MyBatis 主配置文件 config.xml(这里可以自己起,不过后面带上Config.xml) <?xml version="1.0" encoding="U ...

  6. 【LeetCode】组合总和

    [问题]给定一个无重复元素的数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合. candidates 中的数字可以无限制 ...

  7. 【每日Scrum】第九天冲刺

    一.计划会议内容 尝试数据库的连接与ui应用 二.任务看板 任务看板 已完成:登录与个人界面布局实现 进行中:连接数据库,地图主界面 待进行:功能整合 三.scrum讨论照片 四.产品的状态 无 五. ...

  8. UVA_11525 树状数组的活用 二分

    我们知道1——k有K!种排列,现在给定k和n,要你按字典序输出 第n种排列的数列 而且题目给的 n是 n=S1(k-1)!+S2(k-2)!+...+Sk-1*1!+Sk*0!(0=<Si< ...

  9. LeetCode——39. 组合总和

    给定一个无重复元素的数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合. candidates 中的数字可以无限制重复被选 ...

  10. 吴裕雄--天生自然 JAVASCRIPT开发学习:语法

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...