POJ 2531 dfs回溯(小剪枝)

Network Saboteur
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 10381 Accepted: 4988
Description

A university network is composed of N computers. System administrators gathered information on the traffic between nodes, and carefully divided the network into two subnetworks in order to minimize traffic between parts.
A disgruntled computer science student Vasya, after being expelled from the university, decided to have his revenge. He hacked into the university network and decided to reassign computers to maximize the traffic between two subnetworks.
Unfortunately, he found that calculating such worst subdivision is one of those problems he, being a student, failed to solve. So he asks you, a more successful CS student, to help him.
The traffic data are given in the form of matrix C, where Cij is the amount of data sent between ith and jth nodes (Cij = Cji, Cii = 0). The goal is to divide the network nodes into the two disjointed subsets A and B so as to maximize the sum ∑Cij (i∈A,j∈B).
Input

The first line of input contains a number of nodes N (2 <= N <= 20). The following N lines, containing N space-separated integers each, represent the traffic matrix C (0 <= Cij <= 10000).
Output file must contain a single integer – the maximum traffic between the subnetworks.
Output

Output must contain a single integer – the maximum traffic between the subnetworks.
Sample Input

3
0 50 30
50 0 40
30 40 0
Sample Output

90

题意:
有一个图,路径以数组矩阵表示,然后将所有点分到两个集合里,求分完后两个集合所有边的权重和。
解法:
开始非常直接的dfs+回溯,不到10min就写好水过了,但是看到有的人优化到了16ms,而粗鄙我的暴搜用了563ms(poj有些数据还是比较水的),就想到要剪枝,减枝有两点:1,到当前步时,把其后所有剩余的变权重加起来还不能比当前已经求得最大的MAX值大,则减去。2,可知,把第一个点放在A集合或者B集合会得到一样的两个结果(对称)。那么就只需要对放在A集合里讨论即可。如此优化到了94ms。
PS:实在不明白怎么优化到16ms的,还有神优化到了0ms。如果大家有想法,请赐教~~

1,直接dfs+回溯,比较直观。

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <queue>
#include <cstring>

using namespace std;

int G[30][30];
int MAX,n;
int A[30],B[30];

void dfs(int cur){
    int i = 0,j = 0;
    if(cur == n){
        int sum = 0;
        for(i = 0;A[i]!=-1;i++)
            for(j = 0;B[j]!=-1;j++)
                sum += G[A[i]][B[j]];
        if(sum>MAX) MAX = sum;
        return ;
    }
    while(A[i++]!=-1);i--;
    while(B[j++]!=-1);j--;
    A[i] = cur;
    dfs(cur+1);
    A[i] = -1;

    B[j] = cur;
    dfs(cur+1);
    B[j] = -1;
}

int main(){
    while(scanf("%d",&n)!=EOF){
        int i,j;
        for(i = 0;i<n;i++)
            for(j = 0;j<n;j++)
                scanf("%d",&G[i][j]);
        MAX = -0x3f3f3f3f;
        memset(A,-1,sizeof(A));
        memset(B,-1,sizeof(B));
        dfs(0);
        printf("%d\n",MAX);
    }
    return 0;
}

2,剪枝优化后代码:

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <queue>
#include <cstring>

using namespace std;

int G[30][30];
int MAX,n;
int A[30],B[30];
int P[30];

void init(void){
    int i,j;
    memset(P,0,sizeof(P));
    for(j = 0;j<n;j++)
        P[n-1]+=G[n-1][j];
    for(i = n-2;i>=0;i--){
        P[i] = P[i+1];
        for(j = 0;j<n;j++)
            P[i] += G[i][j];
    }
}

void dfs(int cur,int SUM){
    int i = 0,j = 0,k,tem;
    if(cur == n){
        if(SUM>MAX) MAX = SUM;
        return ;
    }
    tem = P[cur];
    if(SUM + tem < MAX) return;
    i = j = 0;
    while(A[i++]!=-1);i--;
    while(B[j++]!=-1);j--;
    A[i] = cur;
    for(k = 0,tem = 0;B[k]!=-1;k++) tem += G[A[i]][B[k]];
    dfs(cur+1,SUM+tem);
    A[i] = -1;

    B[j] = cur;
    for(k = 0,tem = 0;A[k]!=-1;k++) tem += G[A[k]][B[j]];
    dfs(cur+1,SUM+tem);
    B[j] = -1;
}

int main(){
//    freopen("data.in","r",stdin);
    while(scanf("%d",&n)!=EOF){
        int i,j;
        for(i = 0;i<n;i++)
            for(j = 0;j<n;j++)
                scanf("%d",&G[i][j]);
        MAX = -0x3f3f3f3f;
        memset(A,-1,sizeof(A));
        memset(B,-1,sizeof(B));
        init();
A[0] = 0;
        dfs(1,0);
        printf("%d\n",MAX);
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值