题目链接:点击打开链接
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).
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 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
题目大意:有一个集合(1,2,3),把这个集合分成两个子集合,然后求这两个子集合之间的值得和。
给处两点之间的距离
基本思路:这个题确实想不出是怎么实现的,看了别人的题解后,还是有点思路模糊
dfs是真的很抽象,就是两个集合,把点分到这两个集合中,从集合A到集合B的所有点距离,
深搜一下就行了,可是在我脑子中出现不了是怎么回事,具体代码的作用已经在代码中注释
<span style="font-size:18px;">#include <iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<queue>
#include<map>
#define INF 0x3f3f3f3f
using namespace std;
int mp[50][50];
int vis[50];
int maxsum;
int n;
///每一个点都有两种状态,在集合0中和在集合1中,遍历枚举这两种情况
void dfs(int x)
{
int sum;
if(x==n)///当把所有的点的情况安置号后计算距离
{
sum=0;
for(int i=1; i<=n; i++)
{
if(vis[i]==0)continue;///在集合1中的
for(int j=1; j<=n; j++)
{
if(vis[j]==0)///在集合0中的
{
sum+=mp[i][j];
}
}
}
maxsum=max(sum,maxsum);///更新最大值
return;
}
vis[x]=1;///枚举每个点的两种情况
dfs(x+1);
vis[x]=0;
dfs(x+1);
}
int main()
{
while(cin>>n)
{
for(int i=1; i<=n; i++)
{
for(int j=1; j<=n; j++)
{
cin>>mp[i][j];
}
}
memset(vis,0,sizeof(vis));
vis[1]=1;//从1开始,将1放到集合1中
maxsum=0;
dfs(1);
cout<<maxsum<<endl;
}
return 0;
}</span>