HLJU 1036: Teamwork Brings Profits! (dfs)

13 篇文章 0 订阅

1036: Teamwork Brings Profits!

Time Limit: 1 Sec  Memory Limit: 64 MB
Submit: 52  Solved: 8
[ Submit][ Status][ Web Board]

Description

Williammed's father is running a small company now, which has N employees.(2<=N<=10 and N is an even number). The employees are numbered from 1 to N. All the employees are divided into N/2 groups. One employee can only be in one group, and each group works on one project. As we all know, teamwork is very important to a company, so different team can make different profits. And now, given any two people i and j, Williammed's father can tell how much profit(Pij) can they make if they work together. Here comes the problem, given all the Pij(1 <= i <= N,1 <= j <= N,0 < Pij<= 100), you should tell the most profits this company can make. This is an easy problem, isn't it?

Input

The first line of the input is N(2<=N<=10 and N is an even number),the number of employees in the company.

Then there're N lines,each line has N numbers.The jth number in the ith line is Pij,as we discribe above.And we guarantee Pij = Pji,Pii = 0.

The end-of-file is denoted by a single line containing the integer 0.

Output

For each case,output the most profits this company can make.

Sample Input

40 6 62 136 0 35 9462 35 0 513 94 5 00

Sample Output

156

HINT

Source




解析:dfs搜出所有组合,选出最大值即可。




AC代码:

#include <bits/stdc++.h>
using namespace std;

int a[12][12];
bool vis[12];
int n, ans;

void dfs(int cnt, int sum){
    if(cnt == n){
        ans = max(ans, sum);
        return ;
    }
    for(int i=1; i<=n; i++){
        if(!vis[i])
            for(int j=i+1; j<=n; j++){
                if(!vis[j]){
                    vis[i] = vis[j] = true;
                    dfs(cnt + 2, sum + a[i][j]);
                    vis[i] = vis[j] = false;
                }
            }
    }
}

int main(){
    #ifdef sxk
        freopen("in.txt", "r", stdin);
    #endif // sxk

    while(~scanf("%d", &n) && n){
        for(int i=1; i<=n; i++)
        for(int j=1; j<=n; j++) scanf("%d", &a[i][j]);
        memset(vis, false, sizeof(vis));
        ans = 0;
        dfs(0, 0);
        printf("%d\n", ans);
    }
    return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值