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

直接暴搜,未做任何优化 125ms。。。数据有点弱

Posted by hejunfei at 2012-03-01 21:35:28 on Problem 2531
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;

int c[22][22], n, ans, set[22];

void DFS(int id, int sum)
{
    int i;

    set[id] = 1;
    for (i = 0; i < n; i++) {
        if (set[i] == 1)
            sum -= c[id][i];
        else sum += c[id][i];
    }
    if (sum > ans) ans = sum;

    for (i = id+1; i < n; i++)
    {
        DFS(i, sum);
        set[i] = 0;
    }
}

int main()
{
    int i, j;

    scanf ("%d", &n);
    for (i = 0; i < n; i++)
        for (j = 0; j < n; j++)
             scanf ("%d", &c[i][j]);
    memset (set, 0, sizeof (set));
    ans = 0;
    DFS(0, 0);
    printf ("%d\n", 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