[POJ 2531]Network Saboteur网络破坏者 题解(DFS&&带权重的最大割问题)

POJ 2531 -- Network Saboteur

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

题意翻译

大学网络由N台计算机组成,系统管理员收集有关节点之间流量的信息,并仔细地将网络划分为两个子网,以最大程度地减少各部分之间的流量。
心怀不满的计算机科学专业学生瓦夏在被大学开除后,决定复仇。他入侵了大学网络,并决定重新分配计算机,以最大化两个子网之间的流量。
不幸的是,他发现计算这种最糟糕的细分是他作为学生未能解决的问题之一。所以他要求你,一个更成功的CS学生,帮助他。
流量数据以矩阵 C 的形式给出,其中 Cij 是第 i 个和第 j 个节点之间发送的数据量(Cij = Cji,Cii = 0)。目标是将网络节点划分为两个不相交的子集 A 和 B,以便最大化总和 ∑Cij (i∈A,j∈B)。

简单来说就是:给出一个n×n的矩阵C,Cij表示第i个点和第j个点的距离,要求将这n个点分为两个集合A和B,求集合A中各点到集合B中各点的距离和的最大值。

题解


关于最大割(无权值)

将图G的顶点集V划分为S和T,目的是使得子集S子集T之间的边数最大

注意:

(1)是将V划分为两个非空子集,并且全集V为S和T的并集S和T无公共顶点

(2)最大割:是要子集S和子集T之间的关联的边数尽可能的大,这个边是图G中真实存在的

注意:如果边带有权重,最大割问题的划分目标就是使得S和T之间关联边的权重之和最大,二者之间的目标函数只是差了个权重系数
 


AC代码

#include<cstdio>
using namespace std;

const int N=25;
int a[N][N];
bool vis[N]={0};			//对拿出的节点进行标记 

int n,ans=0;
void dfs(int m,int sum){
	
	if(m==n+1){
		return;				//访问到最后一行返回 
	}
	
	if(sum>ans){
		ans=sum;			//修改最大值 
	}
	
	int t=sum;				//记录当前值,返回时使用 
	vis[m]=1;				//标记被拿出的节点 
	for(int i=1;i<=n;i++){	
		if(vis[i]==1){
			sum-=a[m][i];   //将被拿出的节点所对的减去 
		}else{
			sum+=a[m][i];	//将未被拿出节点加上 
		}
	}
	dfs(m+1,sum);			//对下一行进行搜索 
	vis[m]=0;				//返回后将标记删除 
	dfs(m+1,t);				//继续搜索下一行 
}

int main(){
	scanf("%d",&n);
	
	for(int i=1;i<=n;i++){
		for(int j=1;j<=n;j++){
			scanf("%d",&a[i][j]);
		}
	}
	
	dfs(1,0);
	
	printf("%d\n",ans);
	return 0;
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
POJ 1321 排兵布阵问题可以使用 DFS 算法求解。 题目要求在一个 n x n 的棋盘上,放置 k 个棋子,其中每行、每列都最多只能有一个棋子。我们可以使用 DFS 枚举每个棋子的位置,对于每个棋子,尝试将其放置在每一行中未被占用的位置上,直到放置了 k 个棋子。在 DFS 的过程中,需要记录每行和每列是否已经有棋子,以便在尝试放置下一个棋子时进行判断。 以下是基本的 DFS 模板代码: ```python def dfs(row, cnt): global ans if cnt == k: ans += 1 return for i in range(row, n): for j in range(n): if row_used[i] or col_used[j] or board[i][j] == '.': continue row_used[i] = col_used[j] = True dfs(i + 1, cnt + 1) row_used[i] = col_used[j] = False n, k = map(int, input().split()) board = [input() for _ in range(n)] row_used = [False] * n col_used = [False] * n ans = 0 dfs(0, 0) print(ans) ``` 其中,row 代表当前尝试放置棋子的行数,cnt 代表已经放置的棋子数量。row_used 和 col_used 分别表示每行和每列是否已经有棋子,board 则表示棋盘的状态。在尝试放置棋子时,需要排除掉无法放置的位置,即已经有棋子的行和列,以及棋盘上标记为 '.' 的位置。当放置了 k 个棋子时,即可计数一次方案数。注意,在回溯时需要将之前标记为已使用的行和列重新标记为未使用。 需要注意的是,在 Python 中,递归深度的默认限制为 1000,可能无法通过本题。可以通过以下代码来解除限制: ```python import sys sys.setrecursionlimit(100000) ``` 完整代码如下:

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

古谷彻

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值