POJ2531 Network Saboteur【DFS】

Network Saboteur
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 15990 Accepted: 7952

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

问题链接POJ2531 Network Saboteur
问题简述:(略)
问题分析
    结点分成A和B两组,给定结点间距离,使得两组的∑Cij (i∈A,j∈B)最大,输出最大值。计算最大割问题,数据规模小(n<=2),可以用DFS来解决。如果数据规模大,则需要使用计算最小割算法。
    采用穷尽搜索的办法,开始时结点全放在A集合中,考虑将结点移到B的情况。如果结点已经在集合A中,将结点移到B则需要减去该结点的距离,如果该结点还没有在A集合中,则将结点加入到A中。根据对称原理,计算时仅考虑集合A。
程序说明:(略)
参考链接:(略)
题记:(略)

AC的C++语言程序如下:

/* POJ2531 Network Saboteur */

#include <iostream>

using namespace std;

const int N = 25;
int a[N][N], vis[N], n, ans;

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

int main()
{
    std::ios::sync_with_stdio(false);
    std::cin.tie(NULL),
    std::cout.tie(NULL);

    cin >> n;
    for(int i = 0; i < n; i++)
        for(int j = 0; j < n; j++)
            cin >> a[i][j];

    dfs(0, 0);

    cout << ans << endl;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章