状态压缩DP AcWing 91. 最短Hamilton路径

状态压缩DP AcWing 91. 最短Hamilton路径

原题链接

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;
}

原创不易
转载请标明出处
如果对你有所帮助 别忘啦点赞支持哈
在这里插入图片描述

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
哈密顿回路是一种经过图中每个节点一次且仅一次的回路。哈密顿回路问题是一个NP完全问题,因此没有已知的多项式时间算法可以解决这个问题。不过,可以使用启发式算法来解决近似的问题。 下面是一个使用Java实现的近似算法: ```java import java.util.*; public class HamiltonianPath { private static int[][] graph; // 图 private static int[] path; // 存储路径 private static boolean[] visited; // 标记是否访问过 private static int n; // 节点数 public static void main(String[] args) { Scanner sc = new Scanner(System.in); n = sc.nextInt(); graph = new int[n][n]; path = new int[n]; visited = new boolean[n]; // 构建图 for(int i = 0; i < n; i++) { for(int j = 0; j < n; j++) { graph[i][j] = sc.nextInt(); } } // 从第一个节点出发 path[0] = 0; visited[0] = true; if(findHamiltonianPath(1)) { // 打印路径 for(int i = 0; i < n; i++) { System.out.print(path[i] + " "); } } else { System.out.println("No Hamiltonian Path exists"); } } // 查找哈密顿路径 private static boolean findHamiltonianPath(int pos) { // 如果已经遍历完所有节点 if(pos == n) { // 判断最后一个节点是否与第一个节点相邻 if(graph[path[pos - 1]][path[0]] == 1) { return true; } else { return false; } } // 遍历其它节点 for(int i = 1; i < n; i++) { if(isValid(i, pos)) { path[pos] = i; visited[i] = true; if(findHamiltonianPath(pos + 1)) { return true; } // 回溯 visited[i] = false; } } return false; } // 判断节点是否可达 private static boolean isValid(int node, int pos) { // 如果节点已经被访问过,返回false if(visited[node]) { return false; } // 如果前一个节点与当前节点不相邻,返回false if(graph[path[pos - 1]][node] == 0) { return false; } return true; } } ``` 在这个算法中,我们使用了回溯的方法来查找哈密顿路径。我们从第一个节点开始,依次尝试访问其它节点,直到找到一条哈密顿路径或者遍历完所有节点。在查找过程中,我们使用visited数组来标记节点是否已经被访问过,使用path数组来存储路径。isValid方法用来判断节点是否可达。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

飞滕人生TYF

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值