当前位置:网站首页>状态压缩DP例题(旅行商问题和填矩形问题)
状态压缩DP例题(旅行商问题和填矩形问题)
2022-06-13 10:57:00 【重生之我会拧瓶盖】
方便以后复习看一下
给定一张 n 个点的带权无向图,点从 0∼n−1 标号,求起点 0 到终点 n−1 的最短 Hamilton 路径。
Hamilton 路径的定义是从 0 到 n−1 不重不漏地经过每个点恰好一次。
输入格式
第一行输入整数 n。
接下来 n 行每行 n 个整数,其中第 i 行第 j 个整数表示点 i 到 j 的距离(记为 a[i,j])。
对于任意的 x,y,z,数据保证 a[x,x]=0,a[x,y]=a[y,x] 并且 a[x,y]+a[y,z]≥a[x,z]。
输出格式
输出一个整数,表示最短 Hamilton 路径的长度。
数据范围
1≤n≤20
0≤a[i,j]≤107
输入样例:
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 0
输出样例:
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;
}

//这种问题总感觉无从下手,所有横放的方案为所有方案数,感觉像是搜索,状压DP感觉需要凭经验了。状态感觉像是背包问题的体积,是决策条件,
#include <iostream>
#include <cstring>
#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;
}
边栏推荐
- Codeforces Round #798 (Div. 2)ABCD
- DNS协议分析
- Go zero microservice Practice Series (III. API definition and table structure design)
- 宝塔添加一个网站:PHP项目
- 硬件工程师薪资虚高,你认可吗?
- Modification of string class object
- 电赛校赛经验-程控风力摆
- Develop a basic module with low code
- MySQL transaction isolation level and mvcc
- Do you agree that the salary of hardware engineers is falsely high?
猜你喜欢

View the default MySQL password in the pagoda

Introduction to binary tree

Database learning notes (Chapter 15)

Idea remote debugging jar submitted by spark submit

5.5 clock screensaver

EasyClick 运行代码片段出Null

Pagoda access changed from IP to domain name

文件夹数据同步工具Sync Folders Pro

Go needs to add an arrow syntax, which is more like PHP!

Vivo large scale kubernetes cluster automation operation and maintenance practice
随机推荐
vivo大规模 Kubernetes 集群自动化运维实践
Brief request process
of_ find_ compatible_ Node find all nodes
[dynamic planning] beginner level
DNS protocol analysis
Database learning notes (Chapter 15)
2022年劳务员-通用基础(劳务员)上岗证题目及答案
ST表学习
宝塔访问从IP改为域名
很妙的贪心(F2. Nearest Beautiful Number (hard version))
2022甘肃省安全员C证上岗证题目及在线模拟考试
Necessary for Architects: system capacity status checklist
技术管理进阶——管理者可以使用哪些管理工具
Web3 系统构建:去中心化的原则、模型和方法(上)
硬件工程师薪资虚高,你认可吗?
JGR-A | 南京大学黄安宁团队揭示高原湖泊山地影响极端降水的动力-热力机制
Folder data synchronization tool sync folders Pro
Actual combat simulation │ real time error alarm of enterprise wechat robot
【20220526】UE5.0.2 release d11782b
2022 coal mine water exploration and drainage special operation certificate examination question bank simulated examination platform operation