zoukankan      html  css  js  c++  java
  • Hie with the Pie(POJ3311+floyd+状压dp+TSP问题dp解法)

    题目链接:http://poj.org/problem?id=3311

    题目:

    题意:n个城市,每两个城市间都存在距离,问你恰好经过所有城市一遍,最后回到起点(0)的最短距离。

    思路:我们首先用floyd预处理出每两个城市间的最短路,然后采用状压dp来解题。dp[i][j]表示在i这种状压下以j为目标城市的最短距离,i的二进制中x位为1表示到了城市x,为0表示没到城市x,则转移方程为dp[i][j] = min(dp[i][j], dp[i^(1<<(j-1))][k] + dis[k][j]),其中i^(1<<(j-1))表示没到城市j。

    代码实现如下:

     1 #include <set>
     2 #include <map>
     3 #include <queue>
     4 #include <stack>
     5 #include <cmath>
     6 #include <bitset>
     7 #include <cstdio>
     8 #include <string>
     9 #include <vector>
    10 #include <cstdlib>
    11 #include <cstring>
    12 #include <iostream>
    13 #include <algorithm>
    14 using namespace std;
    15 
    16 typedef long long ll;
    17 typedef pair<ll, ll> pll;
    18 typedef pair<ll, int> pli;
    19 typedef pair<int, ll> pil;;
    20 typedef pair<int, int> pii;
    21 typedef unsigned long long ull;
    22 
    23 #define lson i<<1
    24 #define rson i<<1|1
    25 #define bug printf("*********
    ");
    26 #define FIN freopen("D://code//in.txt", "r", stdin);
    27 #define debug(x) cout<<"["<<x<<"]" <<endl;
    28 #define IO ios::sync_with_stdio(false),cin.tie(0);
    29 
    30 const double eps = 1e-8;
    31 const int mod = 10007;
    32 const int maxn = 1e6 + 7;
    33 const double pi = acos(-1);
    34 const int inf = 0x3f3f3f3f;
    35 const ll INF = 0x3f3f3f3f3f3f3f;
    36 
    37 int n;
    38 int dp[2050][15], dis[15][15];
    39 
    40 void floyd() {
    41     for(int k = 0; k <= n; k++) {
    42         for(int i = 0; i <= n; i++) {
    43             for(int j = 0; j <= n; j++) {
    44                 dis[i][j] = min(dis[i][j], dis[i][k] + dis[k][j]);
    45             }
    46         }
    47     }
    48 }
    49 
    50 int main() {
    51     //FIN;
    52     while(~scanf("%d", &n) && n) {
    53         for(int i = 0; i <= n; i++) {
    54             for(int j = 0; j <= n; j++) {
    55                 scanf("%d", &dis[i][j]);
    56             }
    57         }
    58         floyd();
    59         memset(dp, -1, sizeof(dp));
    60         dp[1][0] = 0;
    61         int tot = 1 << (n+1);
    62         for(int i = 0; i < tot; i++) {
    63             for(int j = 1; j <= n; j++) {
    64                 if(i & (1 << (j-1))) {
    65                     if(i == (1<<(j-1))) dp[i][j] = dis[0][j];
    66                     else {
    67                         dp[i][j] = inf;
    68                         for(int k = 1; k <= n; k++) {
    69                             if(i & (1 << (k-1)) && j != k) {
    70                                 dp[i][j] = min(dp[i][j], dp[i^(1<<(j-1))][k] + dis[k][j]);
    71                             }
    72                         }
    73                     }
    74                 }
    75             }
    76         }
    77         int ans = inf;
    78         for(int i = 1; i <= n; i++) {
    79             ans = min(ans, dp[(1<<n)-1][i] + dis[i][0]); //最后要从某一座城市回到起点
    80         }
    81         printf("%d
    ", ans);
    82     }
    83     return 0;
    84 }
  • 相关阅读:
    [PHP] PHP1 与 CGI
    [PHP] Phalcon操作示范
    [Shell] swoole_timer_tick 与 crontab 实现定时任务和监控
    [PHP] Phalcon应用升级PHP7记录
    [GNU] 喝一杯咖啡, 写一写 Makefile
    [PHP] Xhprof 非侵入式使用指南
    [PHP]OOP两类写法的性能对比
    [OSI] 网络间通信流程
    [OSI] 网络7层模型的理解
    [Tools] Vim 插件管理
  • 原文地址:https://www.cnblogs.com/Dillonh/p/9447614.html
Copyright © 2011-2022 走看看