POJ 2531 decentralization Problem DFS algorithm

Source: Internet
Author: User

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

    1. Assume that all are in the collection 0
    2. Get the collection 1 when you traverse the ID.
    3. If the overall weight of the set 0 associated with the ID is to be added, the overall weight value should be reduced
    4. 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.