HDU - 5418 Victor and World (TSP问题求最小哈密顿回路 floyd + 状压dp)

After trying hard for many years, Victor has finally received a pilot license. To have a celebration, he intends to buy himself an airplane and fly around the world. There are nn countries on the earth, which are numbered from 11 to nn . They are connected by mm undirected flights, detailedly the ii -th flight connects the uiui -th and the vivi -th country, and it will cost Victor's airplane wiwi L fuel if Victor flies through it. And it is possible for him to fly to every country from the first country.

Victor now is at the country whose number is 11 , he wants to know the minimal amount of fuel for him to visit every country at least once and finally return to the first country.

Input

The first line of the input contains an integer TT , denoting the number of test cases.
In every test case, there are two integers nn and mm in the first line, denoting the number of the countries and the number of the flights.

Then there are mm lines, each line contains three integers uiui , vivi and wiwi , describing a flight.

1≤T≤201≤T≤20 .

1≤n≤161≤n≤16 .

1≤m≤1000001≤m≤100000 .

1≤wi≤1001≤wi≤100 .

1≤ui,vi≤n1≤ui,vi≤n .

Output

Your program should print TT lines : the ii -th of these should contain a single integer, denoting the minimal amount of fuel for Victor to finish the travel.

Sample Input

1
3 2
1 2 2
1 3 3

Sample Output

10

题意:

无向图中有 n 个点 m 条边,求最小哈密顿回路的长度。

思路:

先用floyd求一遍最短路。

数据最大是16,用1 << n以内的数代表一个状态(图),dp[i][S]表示当前位于 i 点且走过的图为 S 的最小花费,然后再添加一条边,遍历起点 i 和终点 j ,更新 dp[j][S | (1 << j)](走过g[i][j]这条边后位于 j 点,走过的图是之前的S再加上 j 点)

然后 for 循环一遍每个点再回到起点的最小值

#include <bits/stdc++.h>
using namespace std;
const int inf = 0x3f3f3f3f;
const int mod = 1e9 + 7;
const int N = (1 << 16) + 10;

int t, n, m;
int dp[20][N], g[20][20];

void init()
{
    memset(dp, inf, sizeof(dp));
    memset(g, inf, sizeof(g));
}

void floyd()
{
    for(int k = 0; k < n; ++k)
    {
        for(int i = 0;  i < n; ++i)
        {
            for(int j = 0; j < n; ++j)
            {
                g[i][j] = min(g[i][j], g[i][k] + g[k][j]);
            }
        }
    }
}

int main()
{
    int u, v, w;
    scanf("%d", &t);
    while(t--)
    {
        init();
        scanf("%d%d", &n, &m);
        for(int i = 1; i <= m; ++i)
        {
            scanf("%d%d%d", &u, &v, &w);
            u--;
            v--;
            if(g[u][v] > w)
                g[u][v] = g[v][u] = w;
        }
        floyd();
        int bit = (1 << n);
        dp[0][1] = 0;
        for(int S = 1; S < bit; ++S)
        {
            for(int i = 0; i < n; ++i)    ///起点
            {
                if(S & (1 << i))    ///S中包含第 i 个状态,即走过第 i 点
                {
                    for(int j = 0; j < n; ++j)    ///终点
                    {
                        if(!(S & (1 << j)) && g[i][j] != inf)    ///之前没有走过 j 点且i到j之间有边
                        {
                            dp[j][S | (1 << j)] = min(dp[j][S | (1 << j)], dp[i][S] + g[i][j]);    ///更新
                        }
                    }
                }
            }
        }
        int minn = inf;
        g[0][0] = 0;
        for(int i = 0; i < n; ++i)
        {
            minn = min(minn, dp[i][bit - 1] + g[0][i]);    ///回到起点
        }
        cout<<minn<<'\n';
    }
    return 0;
}

 

全部评论

相关推荐

03-08 18:11
门头沟学院 Java
Java抽象小篮子:海投就完事了,简历没什么问题,最大问题是学历
点赞 评论 收藏
分享
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
正在热议
更多
# 春招至今,你的战绩如何? #
6395次浏览 61人参与
# 你的实习产出是真实的还是包装的? #
1304次浏览 32人参与
# MiniMax求职进展汇总 #
23220次浏览 302人参与
# 军工所铁饭碗 vs 互联网高薪资,你会选谁 #
7077次浏览 37人参与
# 简历第一个项目做什么 #
31329次浏览 315人参与
# 当下环境,你会继续卷互联网,还是看其他行业机会 #
186521次浏览 1115人参与
# 巨人网络春招 #
11213次浏览 223人参与
# 研究所笔面经互助 #
118787次浏览 577人参与
# 面试紧张时你会有什么表现? #
30416次浏览 188人参与
# 简历中的项目经历要怎么写? #
309572次浏览 4163人参与
# 职能管理面试记录 #
10722次浏览 59人参与
# AI时代,哪些岗位最容易被淘汰 #
62719次浏览 748人参与
# 网易游戏笔试 #
6374次浏览 83人参与
# 把自己当AI,现在最消耗你token的问题是什么? #
6993次浏览 154人参与
# 腾讯音乐求职进展汇总 #
160437次浏览 1107人参与
# 从哪些方向判断这个offer值不值得去? #
56712次浏览 357人参与
# 正在春招的你,也参与了去年秋招吗? #
362741次浏览 2632人参与
# 你怎么看待AI面试 #
179417次浏览 1182人参与
# 小红书求职进展汇总 #
226905次浏览 1357人参与
# 你的房租占工资的比例是多少? #
92146次浏览 896人参与
# 校招笔试 #
467654次浏览 2954人参与
# 经纬恒润求职进展汇总 #
155713次浏览 1085人参与
牛客网
牛客网在线编程
牛客网题解
牛客企业服务