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个点分到两个集合(0和1)中,初始所有元素都在集合0里面,求出其中一个集合所有点到另一个集合所有点的距离和最大值。因为n最大只有20,所以直接DFS。
// 664K 110MS
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
using namespace std;
const int maxn = 30;
int n, ans;
int G[maxn][maxn];
int InSetOne[maxn];//是否在集合里
void dfs(int depth, int sum) {//dfs的功能:能将第depth个元素放到集合1里面,并将其之后的所有元素的0、1分组遍历一次。
InSetOne[depth] = 1;//将第depth个元素放到集合1里面
int tmp_sum = sum;
for(int i = 1; i <= n; i ++){//遍历所有元素,将仍在在集合0的元素到depth的距离加到ans,将已经在集合1的元素到depth的距离从ans中减去
if(InSetOne[i] == 0) tmp_sum += G[i][depth];
else tmp_sum -= G[i][depth];
}
ans = ans>tmp_sum?ans:tmp_sum;
for(int i = depth + 1; i <= n; i ++){//将depth之后的所有元素的0、1分组遍历一次。
dfs(i, tmp_sum);//根据dfs功能,此操作会将第i个元素放到集合1里面,并将其后的所有元素的0、1分组遍历一次。
InSetOne[i] = 0;//回溯,遍历完第i个元素之后所有元素的0、1分组后,再将其放回到集合0里面。
}
}
int main(){
freopen("input.txt","r",stdin);
freopen("output.txt","w",stdout);
while(1==scanf("%d", &n)){
for(int i = 1; i <= n; i ++)
for(int j = 1; j <= n; j ++)
scanf("%d", &G[i][j]);
memset(InSetOne, 0, sizeof(InSetOne));
ans = 0;
dfs(1, 0);
printf("%d\n", ans);
}
}
//下面的代码思路更清晰,但是更耗时
//下面的代码思路更清晰,但是更耗时
//356K 1469MS
#include <stdio.h>
#include <string.h>
int N;
int d[20][20];
int result;
int InSetOne[20];
void dfs(int i){
if(i==N){
int sum=0;
for(int j=0;j<N;j++){
for(int k=0;k<N;k++){
if(InSetOne[j]==0&&InSetOne[k]==1){
sum+=d[j][k];
}
}
}
result=sum>result?sum:result;
return;
}
InSetOne[i]=0; dfs(i+1);
InSetOne[i]=1; dfs(i+1);
}
int main(void){
freopen("input.txt","r",stdin);
freopen("output.txt","w",stdout);
scanf("%d",&N);
int i,j;
for(i=0;i<N;i++)
for(j=0;j<N;j++)
scanf("%d",&d[i][j]);
memset(InSetOne,0,sizeof(InSetOne));
result=0; InSetOne[0]=0;
dfs(1);
printf("%d\n",result);
return 0;
}
都是DFS枚举,为什么会更耗时?自然想到是因为dfs函数调用次数太多,那么当然定义成inline函数后就会解决这个问题。但实际上,更耗时。为什么?原来递归函数不能设置成内联函数。