Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 10731 | Accepted: 5166 |
Description
Input
Output
Sample Input
3 0 50 30 50 0 40 30 40 0
Sample Output
90
Source
题意:有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[N][N]; 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; }