POJ 2531(状态压缩||DFS)
2016-04-04 18:18
351 查看
Network Saboteur
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
Sample Output
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;
}
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 10731 | Accepted: 5166 |
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;
}
相关文章推荐
- 计数器的实现
- sqlmap使用笔记
- 亲子游
- 在linux中添加ftp用户,并设置相应的权限
- 判断文件是否有以.docx结尾
- Maven介绍,包括作用、核心概念、用法、常用命令、扩展及配置
- 经典的代码案例
- HDU 1272小希的迷宫
- spring事务管理全面分析
- 【系列】Matei Zaharia(Spark系统作者)博士论文-0 摘要
- HTablePool为何弃用?
- 文件的判断
- Linux 下java 安装
- JS中的事件
- CodeForces 18A Triangle
- 获取指定目录下的文件是否有以.docx结尾
- lua基础——基本语法
- asp 验证控件导致 button按钮 事件无效
- C++实现蛇形矩阵
- 《构建之法》第四章读后感