货郎担问题(TSP)

有n个城市,用0,1,…,n-1表示,城i,j之间的距离为dij,有一个货郎从城1出发到其他城市一次且仅一次,最后回到城市1,怎样选择行走路线使总路程最短?
不妨设0为起点和终点, d(i, s)为从i出发, 还需访问s中的各个城市一次回到0,走过的距离.
状态转移方程:d(i, s) = min{d(j, s - {j}) + dist(i, j)}

#include
#include
#include
#include
#include
#include
#include
#include 
using namespace std;

#define maxn 20
#define INF 1e5
int Graph[maxn][maxn];
int d[maxn][1<int route[maxn][1<int dp(int m, int s)
{
    if(d[m][s] != -1)
      return d[m][s];
    if(!s)
        return d[m][s] = Graph[m][0];
    d[m][s] = INF;
    for(int i = 1; i < n; i++)
    {
        if(s & (1<int p = s;
            p ^= 1<int k = dp(i, p) + Graph[m][i];
            if(k < d[m][s]){
                d[m][s] = k;
                route[m][s] = i;
            }
        }
    }
    return d[m][s];
}
int main()
{
    //freopen("in.txt", "r", stdin);
    cin >> n;

    for(int i = 0; i < n*(n-1); i++)
    {
        int m1, m2, m3;
        cin >> m1 >> m2 >> m3;
        Graph[m1][m2] = m3;
    }
    int s = (1<1;
    s ^= 1;
    memset(d, -1, sizeof(d));
    memset(route, 0, sizeof(route));
    dp(0, s);
    cout << d[0][s] << endl;
    int k = 0;

    cout << k;
    while(route[k][s])
    {
        cout << " " << route[k][s];
        k = route[k][s];
        s ^= 1<cout << " " << 0 << endl;
    return 0;
}

你可能感兴趣的:(货郎担问题(TSP))