HDU 3001 Travelling 状态压缩DP简单题

这篇博客介绍了一个利用状态压缩动态规划(DP)解决HDU 3001题目的方法。首先,初始化一些关键数组如cst、f、dp,然后读取地图信息并构建最小距离矩阵。接着,通过状态转移方程更新dp数组以找到最短路径。最后,输出最短路径长度,如果不存在路径则输出-1。
摘要由CSDN通过智能技术生成
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int inf = 1000000009;
int map[13][13];
int cst[13];
int f[60004][13];
int dp[60004][13];
int n, m;
void init() {
	int i, j;
	cst[0] = 1;
	for(i = 1; i <= 10; i++) cst[i] = cst[i-1]*3;
	for(i = 0; i <= cst[10]; i++) {
		int t = i;
		for(j = 0; j < 10; j++) {
			f[i][j] = t % 3;
			t /= 3;
		}
	}
}
int main() {
	int i, j, k;
	init();
	while( ~scanf("%d%d", &n, &m)) {
		for(i = 0; i < n; i++)
			for(j = 0; j < n; j++)
				map[i][j] = inf;
		while(m--) {
			int x, y, z;
			scanf("%d%d%d", &x, &y, &z);
			 x--; y--;
			map[y][x] = map[x][y] = min(map[x][y], z);
		}
		for(i = 0; i < cst[n]; i++)
            for(j  = 0; j < n; j++)
                dp[i][j] = inf;

		for(i &#
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值