POJ-2561 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:
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)的更多相关文章
- poj 2531 Network Saboteur( dfs )
题目:http://poj.org/problem?id=2531 题意:一个矩阵,分成两个集合,求最大的 阻碍量 改的 一位大神的代码,比较简洁 #include<stdio.h> #i ...
- poj 2531 Network Saboteur(经典dfs)
题目大意:有n个点,把这些点分别放到两个集合里,在两个集合的每个点之间都会有权值,求可能形成的最大权值. 思路:1.把这两个集合标记为0和1,先默认所有点都在集合0里. 2 ...
- POJ 2531 Network Saboteur
http://poj.org/problem?id=2531 题意 :有N台电脑,每两台电脑之间都有个通信量C[i][j]; 问如何将其分成两个子网,能使得子网之间的通信量最大. 也就是说将所有节点分 ...
- POJ 2531 Network Saboteur 位运算子集枚举
题目: http://poj.org/problem?id=2531 这个题虽然是个最大割问题,但是分到dfs里了,因为节点数较少.. 我试着位运算枚举了一下,开始超时了,剪了下枝,1079MS过了. ...
- poj 2531 Network Saboteur 解题报告
题目链接:http://poj.org/problem?id=2531 题目意思:将 n 个点分成两个部分A和B(也就是两个子集啦), 使得子集和最大(一定很难理解吧,呵呵).举个例子吧,对于样例,最 ...
- Network Saboteur(dfs)
http://poj.org/problem?id=2531 不太理解这个代码... #include <stdio.h> #include <string.h> ][],v[ ...
- POJ 2531 Network Saboteur (枚举+剪枝)
题意:给你一个图,图中点之间会有边权,现在问题是把图分成两部分,使得两部分之间边权之和最大. 目前我所知道的有四种做法: 方法一:状态压缩 #include <iostream> #inc ...
- PKU 2531 Network Saboteur(dfs+剪枝||随机化算法)
题目大意:原题链接 给定n个节点,任意两个节点之间有权值,把这n个节点分成A,B两个集合,使得A集合中的每一节点与B集合中的每一节点两两结合(即有|A|*|B|种结合方式)权值之和最大. 标记:A集合 ...
- Network Saboteur 分类: 搜索 POJ 2015-08-09 19:48 7人阅读 评论(0) 收藏
Network Saboteur Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 10147 Accepted: 4849 Des ...
随机推荐
- 【STM32H7教程】第52章 STM32H7的LTDC应用之点阵字体和字符编码(重要)
完整教程下载地址:http://www.armbbs.cn/forum.php?mod=viewthread&tid=86980 第52章 STM32H7的LTDC应用之点阵字体和 ...
- tensorflow学习笔记--dataset使用,创建自己的数据集
数据读入需求 我们在训练模型参数时想要从训练数据集中一次取出一小批数据(比如50条.100条)做梯度下降,不断地分批取出数据直到损失函数基本不再减小并且在训练集上的正确率足够高,取出的n条数据还要是预 ...
- JavaScript 对象所有API解析【2020版】
JavaScript 对象所有API解析[2020版] 写于 2019年08月20日,虽然是2019年写的文章,但现在2020年依旧不过时,现在补充了2019年新增的ES10 Object.fromE ...
- sqlserver插入图片数据
-- 插入 insert into [CHOLPOR].[dbo].[t_image](id, name) select '1', BulkColumn from openrowset(bulk N' ...
- 九十八、SAP中ALV事件之十一,查看图片
一.输入事务代码OAER 二.可以看到相关的图片文件了
- 设置进程用指定IE版本
function IsWOW64: BOOL; begin Result := False; if GetProcAddress(GetModuleHandle(kernel32), 'IsWow64 ...
- sql select 0 字段 某字段是不在指定的表
sql select 0 字段 转自 关于 select 语句中 0 某字段名字,的意思是:该某字段是不在指定的表中的,那么如果要在子查询中利用这个指定的表,且带有这个字段,那么就用这个方式来添加 ...
- 使用Oracle VM VirtualBox创建虚拟机教程
使用Oracle VM VirtualBox创建虚拟机教程 ...
- multi-layer perceptrons, MLP)模型,CvANN_MLP。
#include <opencv2/core/core.hpp> #include <opencv2/highgui/highgui.hpp> #include <ope ...
- 二、在SAP中创建一个程序
一.我们来到SE38 二.添加一个程序的名字,需要以Y或者Z开头,点击创建就可以了 三.我们输入hello Sap,然后选择可执行程序,然后保存 四.创建对象目录时,可以选择把这个加入到包中,或者选择 ...