国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

POJ 2531-Network Saboteur(N個點分成兩部分)

2019-11-08 02:48:37
字體:
來源:轉載
供稿:網友
Network Saboteur
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 12566 Accepted: 6061

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

Source

Northeastern Europe 2002, Far-Eastern Subregion

題目意思:

有N臺電腦,給出一個N*N的鄰接矩陣,表示i節點到j節點的流量是Cij,且Cij=Cji。將N個網絡節點劃分為兩個不相交的子集A和B,計算最大的和ΣCij(i∈A,j∈B)。

解題思路:

暴力枚舉+剪枝,借鑒了大神分組的方法。剪枝:只需要枚舉[2^(n-1)]-1次而不是n次。n個不同物品分成兩組的方法數是[2^(n-1)]-1,因為:ΣC(i,n)=2^n,其中i∈[0,n];因為兩組均不為空,且C(0,n)=C(n,n)=1,所以ΣC(i,n)=(2^n)-2,其中i∈[1,n-1];因為兩組相互區別,所以方法數是ΣC(i,n)/2,,即[2^(n-1)]-1,其中i∈[1,n-1]。
#include<iostream>#include<cstdio>#include<iomanip>#include<cmath>#include<cstdlib>#include<cstring>#include<map>#include<algorithm>#include<vector>#include<queue>using namespace std;#define INF 0x3f3f3f3f#define MAXN 25int c[MAXN][MAXN];int vis[25];int main(){#ifdef ONLINE_JUDGE#else    freopen("F:/cb/read.txt","r",stdin);    //freopen("F:/cb/out.txt","w",stdout);#endif    ios::sync_with_stdio(false);    cin.tie(0);    int n;    while(cin>>n)    {        for(int i=0; i<n; i++)            for(int j=0; j<n; j++)                cin>>c[i][j];;        memset(vis,0,sizeof(vis));        int ans=0;        for(int i=0; i<(1<<(n-1)); i++)//枚舉2^n-1次就可以將所有情況枚舉出來        {            ++vis[0];            for(int j=0; j<n; j++)//通過0,1的值將n個點分到兩個部分            {                if(vis[j]==2)                {                    vis[j]=0;                    ++vis[j+1];                }                else break;            }            int res=0;            for(int j=0; j<n; j++)//求出總和                if(vis[j])//計算從所有的1的所有的0的和                    for(int k=0; k<n; k++)                        if(!vis[k]) res+=c[j][k];            ans=max(ans,res);        }        cout<<ans<<endl;    }    return 0;}/*30 50 3050 0 4030 40 0*/
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 永平县| 克拉玛依市| 和平县| 乌鲁木齐县| 柏乡县| 蓝山县| 霸州市| 布拖县| 县级市| 临澧县| 汶上县| 新野县| 策勒县| 原阳县| 甘谷县| 尖扎县| 岱山县| 渝中区| 子洲县| 阳朔县| 安龙县| 奉节县| 华亭县| 八宿县| 奈曼旗| 同江市| 古浪县| 黄龙县| 体育| 石台县| 获嘉县| 福州市| 当涂县| 江西省| 千阳县| 昔阳县| 韶关市| 卢湾区| 莆田市| 温州市| 江达县|