当前位置:网站首页>[dynamic planning] - state compression DP
[dynamic planning] - state compression DP
2022-07-18 05:28:00 【Xuanche_】

AcWing 291. Mondrian's dream
sample input :
1 2 1 3 1 4 2 2 2 3 2 4 2 11 4 11 0 0sample output :
1 0 1 2 3 5 144 51205
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
const int N = 12, M = 1 << N;
int n, m;
long long f[N][M];
bool st[M];
int main()
{
while (cin >> n >> m, n || m)
{
for (int i = 0; i < 1 << n; i ++ )
{
int cnt = 0;
st[i] = true;
for (int j = 0; j < n; j ++ )
if (i >> j & 1)
{
if (cnt & 1) st[i] = false;
cnt = 0;
}
else cnt ++ ;
if (cnt & 1) st[i] = false;
}
memset(f, 0, sizeof f);
f[0][0] = 1;
for (int i = 1; i <= m; i ++ )
for (int j = 0; j < 1 << n; j ++ )
for (int k = 0; k < 1 << n; k ++ )
if ((j & k) == 0 && st[j | k])
f[i][j] += f[i - 1][k];
cout << f[m][0] << endl;
}
return 0;
}
AcWing 91. The shortest Hamilton route
sample input :
5 0 2 4 5 1 2 0 6 5 3 4 6 0 8 3 5 5 8 0 5 1 3 3 5 0sample output :
18
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
const int N = 20, M = 1 << N;
int n;
int w[N][N];
int f[M][N];
int main()
{
cin >> n;
for (int i = 0; i < n; i ++ )
for (int j = 0; j < n; j ++ )
cin >> w[i][j];
memset(f, 0x3f, sizeof f);
f[1][0] = 0;
for (int i = 0; i < 1 << n; i ++ )
for (int j = 0; j < n; j ++ )
if (i >> j & 1)
for (int k = 0; k < n; k ++ )
if (i >> k & 1)
f[i][j] = min(f[i][j], f[i - (1 << j)][k] + w[k][j]);
cout << f[(1 << n) - 1][n - 1];
return 0;
}
边栏推荐
- Alibaba cloud architect Ma song: high performance computing on the cloud helps gene sequencing
- Do you know the debugging skills of the most commonly used Chrome browser console.
- The bill module of freeswitch
- SQL实现将数据表中的字段中的值按分隔符分成多列
- 数据库每日一题---第23天:游戏玩法分析 l
- If you don't want to step on those holes in SaaS, you must first understand the "SaaS architecture"
- database
- 全球云市场增势迅猛,数据安全进入法治化的强监管时代
- Implementation of CRC16 verification C language
- Considérations relatives à la mise en œuvre du modèle d'influence de la campagne dans Salesforce
猜你喜欢

Salesforce中實施Campaign Influence模型注意事項

在Salesforce中基于SAML 2.0搭建SSO并启用JIT User Provisioning(SF Orgs间 / SF Org与Experience Cloud间 / 其他IdP)

Alibaba cloud architect Ma song: high performance computing on the cloud helps gene sequencing

知识干货:基础存储服务新手体验营

阿里云E-MapReduce 极客大赛开放报名 数十万奖金等你挑战

nifi ListSFTP等代理设置

助力开发者,全方位解读 APISIX 测试案例

Salesforce中解析合并字段Merge Fields

NiFi集群搭建及必要的相关配置

【服务器数据恢复】IBM某型号存储热备盘同步数据过程中硬盘离线导致RAID5崩溃的数据恢复案例
随机推荐
VSS技巧:搜索所有签出的文件(根据用户搜索签出文件)
关于安全细节 Timing Attack
FreeModbus 在 STM32F1 平台的移植和解析
【面试:并发篇14:多线程: Monitor 概念】
Interpolation method to calculate the value between two points
【7.8-7.15】寫作社區精彩技術博文回顧
C#利用浏览按钮获得文件路径和文件夹路径
Fail-Fast & Fail-Safe
由四种颜色组成的环,填到五个段组成的一个环上,使得各个环与相邻的颜色并不相同的组合能有多少种(全量)。
Trigger creation, deletion and other operations
工程监测振弦无线采集仪外接数字传感器接入逻辑与数据发送
If you don't want to step on those holes in SaaS, you must first understand the "SaaS architecture"
拜占庭将军问题
阿里云E-MapReduce 极客大赛开放报名 数十万奖金等你挑战
What fault simulation does the chaosblade now support for the database? Do the teachers have any information?
datatable删除行
Light up
Huawei image xmage: seek all the images in the world, and finally see the Bodhi Heart
Transplantation and analysis of freemodbus on stm32f1 platform
@What is tap

