(POJ2531)Network Saboteur <枚举+状态压缩 || DFS+剪枝>

87 篇文章 0 订阅
27 篇文章 0 订阅

Network Saboteur
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
Source

Northeastern Europe 2002, Far-Eastern Subregion

题意:
有n个点,把这些点分别放到两个集合里,在两个集合的每个点之间都会有权值,求可能形成的最大权值。

分析:
最容易想到的就是枚举出所有的情况,总共有2的20次方,时间接近为10的6次方。然后还要分别求出集合直接的权值,最坏为10*10.所有时间为10的8次方。题目时间给的2s,所有刚好可以。
在这里用状态压缩即整数的二进制形式表示集合的分类:某一位是为1表示在A集合,为0表示在B集合。
如3个顶点时有:000,001,010,011,100,101,110,111(全0或1不满足题意)

枚举代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;

int m[25][25];
int A[25],B[25];

int main()
{
    int n,ans;
    while(scanf("%d",&n)!=EOF)
    {
        ans = -1;
        for(int i=0;i<n;i++)
            for(int j=0;j<n;j++)
            scanf("%d",&m[i][j]);
        for(int i=1;i<(1<<n);i++)
        {
            int numa=0,numb=0;
            for(int j=0;j<n;j++)
            {
                if(i & (1<<j))
                    A[numa++] = j;
                else B[numb++] = j;
            }
            int tmp = 0;
            for(int j=0;j<numa;j++)
            {
                for(int k=0;k<numb;k++)
                {
                    tmp += m[A[j]][B[k]];
                }
            }
            ans = max(ans,tmp);
        }
        printf("%d\n",ans);

    }
    return 0;
}

DFS思路:
1、把这两个集合标记为0和1,先默认所有点都在集合0里。
2、依次枚举每个点id,把每个点都放到集合1里去,这个时候就要调整集合的权值了,原来和id都在集合0里的点,要把权值加上;而在集合1里的点,要把权值减去。
3、权值调整完毕后,和ans比较,如果比ans要大, 调整ans。
4、如果放到集合1中,调整节点后的权值比放在集合0中要大,那么就默认这个点在集合1中,继续枚举下面的点进行DFS。最终是可以把最有状态都枚举出来的。

AC代码:

#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
int dep[25],a[25][25];
int n,ans;
void dfs(int id,int z)
{
    dep[id]=1;              //将此节点放于B 
    int temp=z;
    for(int i=1;i<=n;i++)
    {
        if(dep[i]==0)
        temp+=a[i][id];       
        else
        temp-=a[i][id];     //将此节点从A移到B 
    }
    if(ans<temp)      //记录最大值 
    ans=temp;
    for(int i=id+1;i<=n;i++)
    {
        if(z<temp)   //剪枝,当前值小于已经取得的最大值 
        {
            dfs(i,temp);
            dep[i]=0;     //回溯 
        }
    }
}
main()
{
    while(~scanf("%d",&n))
    {
        for(int i=1;i<=n;i++)
        for(int j=1;j<=n;j++)
        scanf("%d",&a[i][j]);
        ans=0;
        memset(dep,0,sizeof(dep));
        dfs(1,0);
        printf("%d\n",ans);
    }
}
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值