博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
HDU 4049 状态压缩DP
阅读量:2440 次
发布时间:2019-05-10

本文共 3575 字,大约阅读时间需要 11 分钟。

/*******************************************************************************    去年北京赛区网络赛水题,状态压缩DP,一开始TLE,然后发现是没初始化,应该用空间换时间来着,接着WA,搞了半天没搞明白。。。最后发现发现一个小小的纰漏~~~    解法就是建立状态dp[M][1 << N],用第一个状态表示第i个城市,第二个状态表示决定在这个城市里游玩的人的状态,用二进制表示~然后就是很裸很裸的状态压缩DP了~*******************************************************************************/#include 
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;#define LOWBIT(x) ( (x) & ( (x) ^ ( (x) - 1 ) ) )#define CLR(x, k) memset((x), (k), sizeof(x))#define CPY(t, s) memcpy((t), (s), sizeof(s))#define SC(t, s) static_cast
(s)#define LEN(s) static_cast
( strlen((s)) )#define SZ(s) static_cast
( (s).size() )typedef double LF;typedef __int64 LL; //VCtypedef unsigned __int64 ULL;typedef pair
PII;typedef pair
PLL;typedef pair
PDD;typedef vector
VI;typedef vector
VC;typedef vector
VF;typedef vector
VS;template
T sqa(const T & x){ return x * x;}template
T gcd(T a, T b){ if (!a || !b) { return max(a, b); } T t; while (t = a % b) { a = b; b = t; } return b;};const int INF_INT = 0x3f3f3f3f;const LL INF_LL = 0x7fffffffffffffffLL; //15fconst double oo = 10e9;const double eps = 10e-7;const double PI = acos(-1.0);#define ONLINE_JUDGEconst int MAXN = 12;const int MAXM = 12;const int MAXS = (1 << MAXN);int n, m, cost[MAXM], inter[MAXN][MAXM], bonus[MAXN][MAXN];int dp[2][MAXS], maze[MAXM][MAXS];bool hs[MAXN];VI G[MAXS];int valueCount(int city, int status){ int res = 0, bit = 0; CLR(hs, false); for (int ind = 0; ind < n; ++ind) { if (status & (1 << ind)) { hs[ind] = true; ++bit; } } res -= bit * cost[city]; for (int i = 0; i < n; ++i) { if (hs[i]) { res += inter[i][city]; } } for (int i = 0; i < n; ++i) { if (!hs[i]) { continue ; } for (int j = i + 1; j < n; ++j) { if (hs[j]) { res += bonus[i][j]; } } } return res;}bool inline containsJudge(const int & lhs, const int & rhs){ return (lhs & rhs) == lhs;}void init(){ int buf = (1 << n); for (int city = 0; city < m; ++city) { for (int ind = 0; ind < buf; ++ind) { maze[city][ind] = valueCount(city, ind); } } for (int i = 0; i < buf; ++i) { G[i].clear(); for (int j = i + 1; j < buf; ++j) { if (containsJudge(i, j)) { G[i].push_back(j); } } } return ;}int solve(){ CLR(dp[0], -INF_INT); CLR(dp[1], 0); int res = 0, buf = (1 << n); for (int ind = 0, k = 0; ind < m; ++ind, k ^= 1) { for (int crt = 0; crt < buf; ++crt) { dp[k][crt] = dp[k ^ 1][crt] + maze[ind][crt]; res = max(res, dp[k][crt]); //TMD相当无语了。。。 for (VI::iterator it = G[crt].begin(); it != G[crt].end(); ++it) { int nxt = *it; dp[k][crt] = max(dp[k][crt], dp[k ^ 1][nxt] + maze[ind][crt]); res = max(res, dp[k][crt]); } } } return res;}void ace(){ int ans; while (cin >> n >> m, n || m) { for (int i = 0; i < m; ++i) { cin >> cost[i]; } for (int i = 0; i < n; ++i) { for (int j = 0; j < m; ++j) { cin >> inter[i][j]; } } for (int i = 0; i < n; ++i) { for (int j = 0; j < n; ++j) { cin >> bonus[i][j]; } } init(); ans = solve(); if (0 >= ans) { puts("STAY HOME"); continue ; } cout << ans << endl; } return ;}int main(){#ifndef ONLINE_JUDGE freopen("in.txt", "r", stdin); freopen("out.txt", "w", stdout);#endif ace(); return 0;}/*******************************************************************************Test Data...2 1101550 55 03 230 5024 4840 7035 200 4 14 0 51 5 02 2100 10050 5050 500 2020 00 0*******************************************************************************/

转载地址:http://sibqb.baihongyu.com/

你可能感兴趣的文章
XML加ASP实现网页“本地化” (转)
查看>>
Java中的异步网络编程 (转)
查看>>
用于核心模式驱动程序的网络体系结构(1) (转)
查看>>
More Effective C++ 条款20 (转)
查看>>
一个程序员的爱恋 (转)
查看>>
足球战术->边锋之Decorator篇 (转)
查看>>
编写优质无错代码(1) (转)
查看>>
MySQL 4.1.0 中文参考手册 --- 6.3 用于 SELECT 和 WHERE 子句的函数 (1) (转)
查看>>
vs.net beta 2中利用DataGrid分页详解 (转)
查看>>
Process-Display-Process (PDP) pattern (转)
查看>>
基于构件复用的软件方法与COM支持 (转)
查看>>
DELPHI中使用API函数详解 (转)
查看>>
Single Entry Point to EJB Layer (转)
查看>>
InsideJVM(3)--Method area(方法区) (转)
查看>>
中文版Windows XP 的新增功能(转)
查看>>
Web Application 開 發 利 器 - WebSnap(三) (转)
查看>>
跟我学 安装Windows Vista Bata2实录(转)
查看>>
Windows Vista IIS 7.0开启方法(转)
查看>>
Windows Vista六大版本详细介绍(转)
查看>>
Windows XP 中注册表内容的导入和导出(转)
查看>>