poj 2531 Network Saboteurb

来源:互联网 发布:网络诽谤罪如何取证 编辑:程序博客网 时间:2024/06/06 03:35

Network Saboteur
Time Limit: 2000MS Memory Limit: 65536KTotal Submissions: 11199 Accepted: 5411

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个点,然后告诉你每个点之间的距离。这个黑客学生只会入侵,然而并不会写深搜 qwq(嘲讽脸) ,他想要让∑Cij (i∈A,j∈B).      变大,来找你帮忙,你可以把这些点分别分到两个集合里,在一个集合里的不计算距离,求一个可能的最大的和。

这样我们把1号点作为基准,然后后面的的点都只有两种可能,那就是和1一个集合或者是不和1一个集合。直接dfs所有情况就可以了,每次情况计算出和并更新最大值。

#include <cstdio>#include <cstring>#include <cmath>#include <string>#include <algorithm>#include <queue>#include <vector>#include <map>using namespace std;const int MAXN=20+5;const int inf=0x3f3f3f;int n;int tu[MAXN][MAXN];int vis[MAXN];int ans=0;void dfs(int x,int y){    if(x==n)return ;    int i,j,sum=0;    vis[x]=y;    for(i=0;i<n;++i)    {        if(vis[i])        {            for(j=0;j<n;++j)            {                if(!vis[j])sum+=tu[i][j];            }        }    }    ans=max(ans,sum);    dfs(x+1,0);    dfs(x+1,1);}int main(){    scanf("%d",&n);    int i,j;    for(i=0;i<n;++i)        for(j=0;j<n;++j)scanf("%d",&tu[i][j]);    dfs(0,1);    printf("%d\n",ans);    return 0;}
简单的深搜,没什么好说的,关键是明白有哪些情况,怎么来搜索。




0 0
原创粉丝点击