Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

dfs暴力求解,参考别人的

Posted by yipilanglbc at 2016-03-16 09:47:40 on Problem 2531
#include<iostream>
#include<cstring>
using namespace std;
int subset[25];
int n, G[25][25];
int ans;
void dfs(int id, int sum)
{
    subset[id] = 1;
    for(int i = 0; i < n; i++)
        if(subset[i])
            sum -= G[id][i];
        else
            sum += G[id][i];
    if(sum > ans)
        ans = sum;
    for(int i = id+1; i < n; i++)
    {
        dfs(i, sum);
        subset[i] = 0;
    }
}
int main()
{
    cin >> n;
    for(int i = 0; i < n; i++)
        for(int j = 0; j < n; j++)
            cin >> G[i][j];
    memset(subset, 0, sizeof(subset));
    ans = 0;
    dfs(0, 0);
    cout << ans;
    return 0;
}

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator