当前位置:网站首页>状态压缩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;
}
原创不易
转载请标明出处
如果对你有所帮助 别忘啦点赞支持哈
边栏推荐
- Unity learning notes
- OpenGL learning notes
- Markdown learning
- Markdown learning
- [set theory] order relation (total order relation | total order set | total order relation example | quasi order relation | quasi order relation theorem | bifurcation | quasi linear order relation | q
- Pit & ADB wireless debugging of vivo real machine debugging
- Common DOS commands
- [RPC] RPC remote procedure call
- producer consumer problem
- [rust notes] 12 closure
猜你喜欢

Sending and receiving of request parameters

Redux - learning notes

Query XML documents with XPath

VIM learning notes from introduction to silk skating

Animation_ IK overview

First Servlet

单调栈-42. 接雨水

Life cycle of Servlet

Dom4j traverses and updates XML

UE4 source code reading_ Bone model and animation system_ Animation process
随机推荐
22-05-26 西安 面试题(01)准备
Cesium for unreal quick start - simple scenario configuration
Annotations simplify configuration and loading at startup
Monotonic stack -42 Connect rainwater
Servlet的生命周期
Osganimation library parsing
Notes and bugs generated during the use of h:i:s and y-m-d
VIM learning notes from introduction to silk skating
Deep parsing (picture and text) JVM garbage collector (II)
Unity learning notes
Unity multi open script
DOM 渲染系统(render mount patch)响应式系统
UE4 source code reading_ Mobile synchronization
I made mistakes that junior programmers all over the world would make, and I also made mistakes that I shouldn't have made
注解简化配置与启动时加载
Pit & ADB wireless debugging of vivo real machine debugging
Concurrent programming (III) detailed explanation of synchronized keyword
too many open files解决方案
Baidu editor ueeditor changes style
100 GIS practical application cases (78) - Multi compliance database design and data warehousing