分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > 代码编程

POJ 2351 Network Saboteur

发布时间:2023-09-06 01:55责任编辑:彭小芳关键词:暂无标签
Network Saboteur
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 14087 Accepted: 6893

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

30 50 3050 0 4030 40 0

Sample Output

90
题意:
将n个数分为两个集合,同一集合内没有距离,不同集合之间有距离,求分成两个集合的最大的距离。
思路:
这道题我们采用DFS的方法进行求解,我们先考虑最简单情况(也就是DFS递归最底层的东西或者说是出口),我们可以将多对多的问题转化成多个一
对多的问题,用0,1区分两个集合,默认刚开始所有的数都在0集合中,然后不断向1集合中转移数据。最后求一个最大值,就是我们想要的结果
AC代码:
#include<cstdio>#include<cstring>#include<iostream>#define N 30#define INF 0x3f3f3f3fusing namespace std;int mp[N][N];bool vis[N];int max1;int n;void DFS(int s,int sum){vis[s]=true;//vis[]=true--在1集合中for(int i=1;i<=n;i++){ ???if(!vis[i])//如果在不同的集合 ?????sum+=mp[s][i]; ???else //在相同的集合 ????sum-=mp[s][i];}max1=max(max1,sum);for(int i=s+1;i<=n;i++){//将0中的数据向1转移 ???vis[i]=true; ???DFS(i,sum); ???vis[i]=false;//注意回复}}int main(){while(~scanf("%d",&n)){ ???for(int i=1;i<=n;i++){ ???????for(int j=1;j<=n;j++){ ???????????scanf("%d",&mp[i][j]); ???????} ???} ???max1=-INF; ???DFS(0,0); ???printf("%d\n",max1); ???}return 0;}
#include<cstdio>#include<cstring>#include<iostream>#define N 30#define INF 0x3f3f3f3fusing namespace std;int mp[N][N];bool vis[N];int max1;int n;void DFS(int s,int sum){vis[s]=true;//vis[]=true--在1集合中for(int i=1;i<=n;i++){ ???if(!vis[i])//如果在不同的集合 ?????sum+=mp[s][i]; ???else //在相同的集合 ????sum-=mp[s][i];}max1=max(max1,sum);for(int i=s+1;i<=n;i++){//将0中的数据向1转移 ???vis[i]=true; ???DFS(i,sum); ???vis[i]=false;//注意回复}}int main(){while(~scanf("%d",&n)){ ???for(int i=1;i<=n;i++){ ???????for(int j=1;j<=n;j++){ ???????????scanf("%d",&mp[i][j]); ???????} ???} ???max1=-INF; ???DFS(0,0); ???printf("%d\n",max1); ???}return 0;}
 

POJ 2351 Network Saboteur

原文地址:https://www.cnblogs.com/by-DSL/p/9073865.html

知识推荐

我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved