当前位置:网站首页>State compression DP acwing 91 Shortest Hamilton path
State compression DP acwing 91 Shortest Hamilton path
2022-07-03 09:06:00 【T_ Y_ F666】
State compression DP AcWing 91. The shortest Hamilton route
Original link
AcWing 91. The shortest Hamilton route
Algorithm tags
Binary status Compress DP
Ideas
Code
#include<bits/stdc++.h>
#define int long long
#define rep(i, a, b) for(int i=a;i<b;++i)
#define Rep(i, a, b) for(int i=a;i>b;--i)
using namespace std;
const int N = 20, M = 1 << N;
int a[N][N], f[M][N];
inline int read(){
int s=0,w=1;
char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')w=-1;ch=getchar();}
while(ch>='0'&&ch<='9') s=s*10+ch-'0',ch=getchar();
return s*w;
}
void put(int x) {
if(x<0) putchar('-'),x=-x;
if(x>=10) put(x/10);
putchar(x%10^48);
}
signed main(){
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int n=read();
rep(i, 0, n){
rep(j, 0, n){
a[i][j]=read();
}
}
memset(f, 0x3f, sizeof f);
// No journey has been made at the initial point , Therefore, the initial value bit 0
f[1][0]=0;
// i It means all the situations
rep(i, 0, 1<<n){
rep(j, 0, n){
// j Indicates which point to go
if(i>>j&1){// j Not past
// k Said go j Before this point , With k The shortest distance to the end
rep(k, 0, n){
if(i>>k&1){ //k Not past
f[i][j]=min(f[i][j], f[i-(1<<j)][k]+a[k][j]);
}
}
}
}
}
printf("%lld", f[(1<<n)-1][n-1]);
return 0;
}
Originality is not easy.
Reprint please indicate the source
If it helps you Don't forget to praise and support
边栏推荐
- Facial expression recognition based on pytorch convolution -- graduation project
- LeetCode 30. 串联所有单词的子串
- The method of replacing the newline character '\n' of a file with a space in the shell
- AcWing 786. Number k
- 低代码前景可期,JNPF灵活易用,用智能定义新型办公模式
- PIC16F648A-E/SS PIC16 8位 微控制器,7KB(4Kx14)
- SQL statement error of common bug caused by Excel cell content that is not paid attention to for a long time
- LeetCode 57. Insert interval
- php public private protected
- JS ternary operator - learning notes (with cases)
猜你喜欢
随机推荐
Format - C language project sub file
LeetCode 535. Encryption and decryption of tinyurl
Using DLV to analyze the high CPU consumption of golang process
LeetCode 30. 串联所有单词的子串
The method of replacing the newline character '\n' of a file with a space in the shell
Complex character + number pyramid
【点云处理之论文狂读经典版8】—— O-CNN: Octree-based Convolutional Neural Networks for 3D Shape Analysis
LeetCode 532. K-diff number pairs in array
createjs easeljs
Facial expression recognition based on pytorch convolution -- graduation project
MySQL three logs
Deeply understand the underlying data structure of MySQL index
AcWing 787. Merge sort (template)
AcWing 788. 逆序对的数量
JS non Boolean operation - learning notes
On the setting of global variable position in C language
LeetCode 515. Find the maximum value in each tree row
Try to reprint an article about CSDN reprint
数字化管理中台+低代码,JNPF开启企业数字化转型的新引擎
LeetCode 871. 最低加油次数