Poj 2531 Network Saboteur(DFS+剪枝)

2531 -- Network Saboteur (poj.org)

#include<iostream>
#include<cstring>
using namespace std;
const int N=30;
int n,ans,C[N][N];
bool group[N];
void DFS(int row,int sum){
    if(row>n) return ;
    group[row]=true;
    int tmp=sum;
    for(int i=1;i<=n;i++){
        if(group[i]) tmp-=C[row][i];
        else tmp+=C[row][i];
    }
    ans=max(ans,tmp);
    if(tmp>sum){
        for(int i=row+1;i<=n;i++){
            DFS(i,tmp);
        }
    }
    group[row]=false;
}
signed main(){
    ios::sync_with_stdio(false);
    cin.tie(0);cout.tie(0);
    cin>>n;
    for(int i=1;i<=n;i++)
        for(int j=1;j<=n;j++)
            cin>>C[i][j];
    ans=0;
    DFS(1,0);
    cout<<ans<<endl;    
    
    return 0;
}

 

posted @ 2024-02-12 21:22  ACCbulb  阅读(13)  评论(0)    收藏  举报