旅行商问题dfs解法

一个售货员必须访问n个城市,恰好访问每个城市一次,并最终回到出发城市。

售货员从城市到城市的旅行费用是一个整数,旅行所需的全部费用是他旅行经过的的各边费用之和,而售货员希望使整个旅行费用最低。

(等价于求图的最短哈密尔顿回路问题)

输入格式
第一行两个数n和m,表示顶点数和边数。

接下来行,每行三个数,表示有向边h和路径长度。

图中可能有重边和自环。

输出格式
一个数如果不存在哈密顿环路,则输出-1,否则输出最短哈密尔顿回路长度。

样例
input
4 12
1 2 1
1 3 10
1 4 10
2 1 10
2 3 1
2 4 10
3 1 10
3 2 10
3 4 1
4 1 1
4 2 10
4 3 10
output
4

邻接矩阵dfs:
非常容易超时,n取到12的时候已经扛不住了…

#include <algorithm>
#include <iostream>
using namespace std;
int map[13][13];
int book[13];
const int INF = 11111111;
long long sum = 0, n, m, cnt = 0, ans = INF;

void dfs(int c) {
    if (cnt >= n - 1) {
        ans = min(sum + map[c][1], ans);
        return;
    }
    book[c] = 1;
    for (int i = 1; i <= n; i++) {
        if (!book[i]) {
            sum += map[c][i];
            cnt++;
            dfs(i);
            cnt--;
            sum -= map[c][i];
        }
    }
    book[c] = 0;
}

int main() {
    cin >> n >> m;
    if (!m) {
        if (n == 1) {
            cout << 0;
        } else
            cout << -1;
        return 0;
    }
    fill(*map, *map + 13 * 13 + 1, INF);
    for (int i = 1; i <= n; i++)
        map[i][i] = 0;
    while (m--) {
        int a, b, v;
        cin >> a >> b >> v;
        if (a != b)
            map[a][b] = min(map[a][b], v);
    }
    if (n == 1) {
        cout << 0;
        return 0;
    }
    dfs(1);
    cout << (ans != INF ? ans : -1);
    return 0;
}

邻接表dfs

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
const int INF = 11111111;
long long sum = 0, n, m, cnt = 0, ans = INF;
int book[13];
struct node {
    long long to, w;
    node(int t, int ww) {
        to = t;
        w = ww;
    }
};
int mm[13][13];
vector<node> map[13];

void dfs(int c) {
    if (cnt >= n - 1) {
        ans = min(sum + mm[c][1], ans);
        return;
    }
    book[c] = 1;
    for (auto i : map[c]) {
        if (!book[i.to]) {
            sum += i.w;
            cnt++;
            dfs(i.to);
            cnt--;
            sum -= i.w;
        }
    }
    book[c] = 0;
}

int main() {
    cin >> n >> m;
    if (!m) {
        if (n == 1) {
            cout << 0;
        } else
            cout << -1;
        return 0;
    }
    fill(*mm, *mm + 13 * 13 + 1, INF);
    while (m--) {
        int a, b, v;
        cin >> a >> b >> v;
        if (a != b)
            mm[a][b] = min(mm[a][b], v);
    }
    if (n == 1) {
        cout << 0;
        return 0;
    }
    for (int i = 1; i <= n; i++) {
        map[i].push_back(node(i, 0));
    }
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= n; j++) {
            if (mm[i][j] && mm[i][j] != INF)
                map[i].push_back(node(j, mm[i][j]));
        }
    }
    dfs(1);
    cout << (ans != INF ? ans : -1);
    return 0;
}

注意一定要考虑n=1和m=0的特殊情况!!
以及这个邻接表其实写的不太好。
参考下图:
可以额外用一个vector来存储路径。以及剪枝的问题。
在这里插入图片描述

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值