• HDU5418——TSP变形——Victor and World


    http://acm.hdu.edu.cn/showproblem.php?pid=5418

    /*
    模板题
    先用floeyed处理
    
    
    */
    /************************************************
    * Author        :Powatr
    * Created Time  :2015-8-22 21:20:36
    * File Name     :B.cpp
     ************************************************/
    
    #include <cstdio>
    #include <algorithm>
    #include <iostream>
    #include <sstream>
    #include <cstring>
    #include <cmath>
    #include <string>
    #include <vector>
    #include <queue>
    #include <deque>
    #include <stack>
    #include <list>
    #include <map>
    #include <set>
    #include <bitset>
    #include <cstdlib>
    #include <ctime>
    using namespace std;
    
    #define lson l, mid, rt << 1
    #define rson mid + 1, r, rt << 1 | 1
    typedef long long ll;
    const int MAXN = 1 << 17;
    const int inf = 0x3f3f3f3f;
    const int MOD = 1e9 + 7;
    
    int d[20][20];
    int dp[MAXN][20];
    int main(){
        int T, n, m;
        int x, y, z;
        for(scanf("%d", &T); T--; ){
             scanf("%d%d", &n, &m);
             memset(d, inf, sizeof(d));
             memset(dp, inf, sizeof(dp));
             for(int i = 1; i <= m; i++){
                 scanf("%d%d%d", &x, &y, &z);
                 x--, y--;
                     if(d[x][y] > z){
                         d[x][y] = d[y][x] = z;
                     }
             }
             for(int i = 0; i < n; i++)
                d[i][i] = 0;
             for(int i = 0; i < n; i++){
                 for(int j = 0; j < n; j++){
                     for(int k = 0; k < n; k++){
                         d[i][j] = min(d[i][j], d[i][k] + d[k][j]);
                     }
                 }
             }
             int maxn = (1 << n) - 1;
             dp[1][0] = 0;
             for(int i = 0; i <= maxn; i++){
                 for(int j = 0; j < n; j++){
                     for(int k = 0; k < n; k++){
                         if(!((i>>k)&1))
                             dp[i|(1<<k)][k] = min(dp[i|(1<<k)][k], dp[i][j] + d[j][k]);
                         }
                     }
                 }
             int ans = inf;
             for(int i = 0 ; i < n; i++){
                ans = min(ans, dp[maxn][i] + d[i][0]);
             }
             printf("%d
    ", ans);
        }
        return 0;
    }
    

      

  • 相关阅读:
    C#
    if
    .net 5.0
    C# 未提供必须形参对应的实参
    EasyRTC报错 “[ERR] mod_local_stream.c:880 Unknown source default”排查
    EasyRTC通过公网进入会议室失败问题排查及解决
    【CF1558B】Up the Strip
    【AT Keyence2019E】Connecting Cities
    【洛谷P7470】岛屿探险
    【洛谷P6628】丁香之路
  • 原文地址:https://www.cnblogs.com/zero-begin/p/4751214.html
Copyright © 2020-2023  润新知