当前位置:网站首页>State compression DP acwing 91. shortest Hamilton path
State compression DP acwing 91. shortest Hamilton path
2022-07-27 11:18: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 
边栏推荐
- Use of beautifulsoup
- 洛谷P1896 互不侵犯
- Miscellaneous records of Finance
- Non progressive phenomena of entropy and morphology
- Asustek unparalleled, this may be the best affordable high brush thin notebook on the screen
- 最长上升子序列模型 AcWing 1010. 拦截导弹
- The second method of calculating overlapping integral
- ACM warm-up Exercise 1 in 2022 summer vacation (summary)
- Error: image clipToBoundsAndScale, argument 'input'
- 如何创建一个带诊断工具的.NET镜像
猜你喜欢

c语言指针函数和函数指针的辨析

The difference of iteration number and information entropy

解决 ImportError: cannot import name 'abs' 导入tensorflow报错

ACM warm-up Exercise 2 in 2022 summer vacation (summary)

Redis+caffeine two-level cache enables smooth access speed

最长上升子序列模型 AcWing 1010. 拦截导弹

How to build a real-time development platform to deeply release the value of enterprise real-time data?

中国剩余定理 AcWing 204. 表达整数的奇怪方式

Solve importerror: cannot import name'abs'import tensorflow error

Knapsack model acwing 423. Picking herbs
随机推荐
IO stream_ Overview and explanation of data input and output flow
Taishan Office Technology Lecture: scaling and opening files
最长上升子序列模型 AcWing 1014. 登山
Knapsack model acwing 1024. Packing problem
8 find subsequences with a maximum length of K
Wechat push - template message parameter configuration
Learning notes uni app
15th largest value of data flow
Remember not to copy your group work, students. Fortunately, you only passed two questions. Don't have an accident
十年架构五年生活-07 年轻气盛的蜕变
TensorFlow张量运算函数集
XXX packages are looking for funding run 'NPM fund' for details solutions
计算重叠积分的第二种方法
Local and overall differences between emergence and morphology
Wilderness search --- search iterations
Object array de duplication
Neural network learning notes
Students, don't copy all my code, remember to change it, or we both want G
Description and feelings
How to create a.Net image with diagnostic tools