Test instructions: A set (matrix) m[i][j]=m[j][i] weight, divided into two sets, so that its weight value is maximum. Note: Weights in the same set can only be counted one.
Idea: DFS
- Assume that all are in the collection 0
- Get the collection 1 when you traverse the ID.
- If the overall weight of the set 0 associated with the ID is to be added, the overall weight value should be reduced
- If you get a set of 1 weights, go back to DFS, and then a backtracking operation
Questions to note on the code:
for (int1; i < n; i++) { if (tmp > data) { DFS (i, TMP); 0 ; } }
Here is a backtracking operation, if you do not execute DFS, I put to set 1 is not appropriate, so put in the collection 0
Code to solve the problem:
#include <iostream>#include<cstdio>#include<string.h>using namespacestd;intmap[ -][ -];intdep[ -];intN, ans;voidDfsintIdintdata) {Dep[id]=1; intTMP =data; for(inti =0; I < n; i++) { if(Dep[i] = =0) tmp + =Map[i][id]; ElseTMP-=Map[i][id]; } if(ANS < tmp) ans =tmp; for(inti = ID +1; I < n; i++) { if(tmp >data) {DFS (I, TMP); Dep[i]=0; } }}intMain () {scanf ("%d", &N); for(inti =0; I < n; i++) for(intj =0; J < N; J + +) scanf ("%d", &Map[i][j]); memset (DEP,0,sizeof(DEP)); Ans=0; DFS (0,0); printf ("%d\n", ans); return 0;}
POJ 2531 decentralization Problem DFS algorithm