当前位置:网站首页>状态压缩DP AcWing 91. 最短Hamilton路径
状态压缩DP AcWing 91. 最短Hamilton路径
2022-07-03 08:41:00 【T_Y_F666】
状态压缩DP AcWing 91. 最短Hamilton路径
原题链接
算法标签
二进制状态 压缩DP
思路
代码
#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);
// 在初始点尚未走过任何路程, 因此初始值位0
f[1][0]=0;
// i表示所有的情况
rep(i, 0, 1<<n){
rep(j, 0, n){
// j表示走到哪一个点
if(i>>j&1){// j未走过
// k表示走到j这个点之前,以k为终点的最短距离
rep(k, 0, n){
if(i>>k&1){ //k未走过
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;
}
原创不易
转载请标明出处
如果对你有所帮助 别忘啦点赞支持哈
边栏推荐
- LinkedList set
- 【Rust笔记】02-所有权
- php public private protected
- [concurrent programming] Table hopping and blocking queue
- 【Rust 笔记】10-操作符重载
- Unity multi open script
- Notes and bugs generated during the use of h:i:s and y-m-d
- Concurrent programming (V) detailed explanation of atomic and unsafe magic classes
- 22-06-27 Xian redis (01) commands for installing five common data types: redis and redis
- Eating fruit
猜你喜欢
Deep parsing JVM memory model
too many open files解决方案
TP5 multi condition sorting
Concurrent programming (VI) ABA problems and solutions under CAS
Dom4j traverses and updates XML
Monotonic stack -84 The largest rectangle in the histogram
22-06-27 Xian redis (01) commands for installing five common data types: redis and redis
MySQL 8
Facial expression recognition based on pytorch convolution -- graduation project
20220630学习打卡
随机推荐
Unity notes 1
Try to reprint an article about CSDN reprint
[rust notes] 12 closure
Dom4j遍历和更新XML
[linear table] basic operation of bidirectional linked list specify node exchange
[MySQL] MySQL Performance Optimization Practice: introduction of database lock and index search principle
Markdown directory generation
Constraintlayout's constraintset dynamically modifies constraints
Campus lost and found platform based on SSM, source code, database script, project import and operation video tutorial, Thesis Writing Tutorial
How to delete CSDN after sending a wrong blog? How to operate quickly
Annotations simplify configuration and loading at startup
DOM 渲染系统(render mount patch)响应式系统
Servlet的生命周期
PHP function date (), y-m-d h:i:s in English case
[concurrent programming] consistency hash
Binary tree sorting (C language, int type)
记忆化搜索 AcWing 901. 滑雪
Notes and bugs generated during the use of h:i:s and y-m-d
How to deal with the core task delay caused by insufficient data warehouse resources
第一个Servlet