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;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章