您的位置:首页 > 其它

POJ 2531(状态压缩||DFS)

2016-04-04 18:18 351 查看
Network Saboteur

Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 10731 Accepted: 5166
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个点,给你大小为n的矩阵,G[I][J]表示i点到j点的路径长度,现在让你使这些点分为2个集合,使得他们之间的路径长度之和最大

题解:直接DFS枚举所有的可能性,但是想了一下直接状态压缩不是更好吗?暴力枚举一下就OK啦

#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<iostream>
#include<algorithm>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<string>
#include<bitset>
#include<utility>
#include<functional>
#include<iomanip>
#include<sstream>
#include<ctime>
using namespace std;

#define N int(1e2)
#define inf int(0x3f3f3f3f)
#define mod int(1e9+7)
typedef long long LL;

#ifdef CDZSC
#define debug(...) fprintf(stderr, __VA_ARGS__)
#else
#define debug(...)
#endif
int g

;
vector<int>st1, st2;
int main()
{
#ifdef CDZSC
freopen("i.txt", "r", stdin);
//freopen("o.txt","w",stdout);
int _time_jc = clock();
#endif

int n;
while (~scanf("%d", &n))
{
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
scanf("%d", &g[i][j]);
int ans = -1;
for (int i = 0; i < (1 << n); i++)
{
st1.clear();
st2.clear();
for (int j = 0; j < n; j++)
{
if (i&(1 << j))
{
st1.push_back(j);
}
else
{
st2.push_back(j);
}
}
int sum = 0;
for (int j = 0; j < st1.size(); j++)
{
for (int k = 0; k < st2.size(); k++)
{
sum += g[st1[j]][st2[k]];
}
}
ans = max(ans, sum);
}
printf("%d\n", ans);
}
#ifdef CDZSC
debug("time: %d\n", int(clock() - _time_jc));
#endif
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: