poj 2531 Network Saboteur

来源:互联网 发布:淘宝店库存软件 编辑:程序博客网 时间:2024/06/05 23:46
Network Saboteur
Time Limit: 2000MS Memory Limit: 65536KTotal Submissions: 11667 Accepted: 5664

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

30 50 3050 0 4030 40 0

Sample Output

90

这一题就是要一步一步的枚举就可以了。
代码如下:
#include <cstdio>#include <cstring>#include <algorithm>#include <iostream>using namespace std;int n;char a[20];int num;int ans;int dis[20], nu[20];int len;void dfs(int l, int now, int tot){    int i;    if ( l == len )        tot = tot+now;    if ( tot > n )        return ;    if ( l == len )    {        if ( num == tot )            ans++;        if ( num < tot )        {            num = tot;            ans = 1;            for ( i = 0;i < 20; i++ )            {                nu[i] = dis[i];            }        }        return ;    }    dis[l-1] = 1;    dfs(l+1, a[l]-'0', tot+now);    dis[l-1] = 0;    dfs(l+1, now*10+a[l]-'0', tot);}int main(){    int i;    while ( ~scanf ( "%d %s", &n, a ) )    {        len = strlen(a);        if ( n == 0 && a[0] == '0' && len == 1)            break;        num = 0;        ans = 1;        int sum = 0;        for ( i = 0;i < len; i++ )        {            sum = sum+a[i]-'0';        }        if ( sum > n )        {            printf ( "error\n" );            continue;        }        dfs(1, a[0]-'0', 0);        if ( ans != 1 )        {            printf ( "rejected\n" );            continue;        }        printf ( "%d ", num );        for ( i = 0;i < len; i++ )        {            if ( nu[i] == 1 && i != len-1 )                printf ( "%c ", a[i] );            else if ( nu[i] == 0 && i != len-1 )                printf ( "%c", a[i] );            else                printf ( "%c\n", a[i] );        }    }}

代码菜鸟,如有错误,请多包涵!!!
如有帮助记得支持我一下,谢谢!!!
0 0