[状压dp]HDOJ1565 方格取数(1)
2014-11-07 21:25
148 查看
中文题~~ 题意略
$n\le 20$ ! 很明显是状压!
HDOJ 1565
$n\le 20$ ! 很明显是状压!
#include <cstdio> #include <cstdlib> #include <cstring> #include <climits> #include <cctype> #include <cmath> #include <string> #include <sstream> #include <iostream> #include <algorithm> #include <iomanip> using namespace std; #include <queue> #include <stack> #include <vector> #include <deque> #include <set> #include <map> typedef long long LL; typedef long double LD; #define pi acos(-1.0) #define lson l, m, rt<<1 #define rson m+1, r, rt<<1|1 typedef pair<int, int> PI; typedef pair<int, PI> PP; #ifdef _WIN32 #define LLD "%I64d" #else #define LLD "%lld" #endif //#pragma comment(linker, "/STACK:1024000000,1024000000") //LL quick(LL a, LL b){LL ans=1;while(b){if(b & 1)ans*=a;a=a*a;b>>=1;}return ans;} //inline int read(){char ch=' ';int ans=0;while(ch<'0' || ch>'9')ch=getchar();while(ch<='9' && ch>='0'){ans=ans*10+ch-'0';ch=getchar();}return ans;} //inline void print(LL x){printf(LLD, x);puts("");} //inline void read(double &x){char c = getchar();while(c < '0') c = getchar();x = c - '0'; c = getchar();while(c >= '0'){x = x * 10 + (c - '0'); c = getchar();}} int mp[25][25]; int dp[2][1<<20]; // n格为一个状态 int p[1<<20], d; void pre() //先记录所有的合法状态 即不相邻 { d=0; for(int i=0;i<(1<<20);i++) if((i & (i<<1))==0) p[d++]=i; } int main() { #ifndef ONLINE_JUDGE freopen("in.txt", "r", stdin); freopen("out.txt", "w", stdout); #endif pre(); int n; while(~scanf("%d", &n)) { for(int i=0;i<n;i++) for(int j=0;j<n;j++) scanf("%d", &mp[i][j]); memset(dp, 0, sizeof(dp)); int cur=0; for(int i=0;i<n;i++) { for(int k=0;k<d;k++) { int sum=0; if(p[k]>=(1<<n)) // 算是优化吧 没有就tle了 break; for(int j=0;j<n;j++) if(p[k] & (1<<j)) sum+=mp[i][j]; for(int j=0;j<d;j++) { if(p[j]>=(1<<n)) // 算是优化吧 没有就tle了 break; if((p[k] & p[j])==0) dp[cur][p[k]]=max(dp[cur][p[k]], dp[cur^1][p[j]]+sum); } } cur^=1; } cur^=1; int ans=0; for(int i=0;i<(1<<n);i++) ans=max(ans, dp[cur][i]); printf("%d\n", ans); } return 0; }
HDOJ 1565
相关文章推荐
- hdoj 1565 方格取数(1) 【状压DP 基础题目】
- HDOJ/HDU 1565 方格取数(1)
- hdu 1565 方格取数(1)【状压dp】
- HDOJ 1565 方格取数(1)(状态压缩dp)
- HDU - 1565 方格取数(1) 状压DP
- HDU 1565 方格取数(1) 【最大点权独立集】
- hdoj 2046 骨牌铺方格(递推)
- hdu 1565 方格取数(1)(最小割,最大点权独立集)
- hdu 1565 方格取数
- HDU-1565 方格取数(1)
- HDU_1565_方格取数(1)
- hdu 1565 方格取数(1) 状态压缩+dp
- HDU 1565 方格取数(1)(最小割-Dinic)
- 方格取数(hdu 1565)
- hrbust 1843 方格取数【状压dp】
- hdu 1565 方格取数(1) (最小割/状态压缩+DP)
- 方格取数 hdu 1565 最小割
- HDU 1565 方格取数(1)
- HDOJ&nbsp;&nbsp;2046&nbsp;&nbsp;骨牌铺方格
- hdu 1565 方格取数(1) (状态压缩DP)