zoukankan      html  css  js  c++  java
  • HDU3001 Travelling —— 状压DP(三进制)

    题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=3001

    Travelling

    Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
    Total Submission(s): 8286    Accepted Submission(s): 2703


    Problem Description
    After coding so many days,Mr Acmer wants to have a good rest.So travelling is the best choice!He has decided to visit n cities(he insists on seeing all the cities!And he does not mind which city being his start station because superman can bring him to any city at first but only once.), and of course there are m roads here,following a fee as usual.But Mr Acmer gets bored so easily that he doesn't want to visit a city more than twice!And he is so mean that he wants to minimize the total fee!He is lazy you see.So he turns to you for help.
     
    Input
    There are several test cases,the first line is two intergers n(1<=n<=10) and m,which means he needs to visit n cities and there are m roads he can choose,then m lines follow,each line will include three intergers a,b and c(1<=a,b<=n),means there is a road between a and b and the cost is of course c.Input to the End Of File.
     
    Output
    Output the minimum fee that he should pay,or -1 if he can't find such a route.
     
    Sample Input
    2 1 1 2 100 3 2 1 2 40 2 3 50 3 3 1 2 3 1 3 4 2 3 10
     
    Sample Output
    100 90 7
     
    Source
     
     
     
     
    题解:
    1.对于每一个点, 有三种状态:没有被访问、被访问一次、被访问两次。所以需要用三进制进行压缩。
    2.dp[status][last]表示:在状态为status下(即走了多少个点,以及每个点走了多少次),最后走的是last点的最小花费。
    3.对于一个已有状态:dp[s][j],枚举每一个点k,如果这个点k不是j,而k、j有边,且k被访问的次数不超过2次,那么下一步就可以访问k点。
    4.类似的题目:HDU4856
     
     
    代码如下:
     1 #include <iostream>
     2 #include <cstdio>
     3 #include <cstring>
     4 #include <cmath>
     5 #include <algorithm>
     6 #include <vector>
     7 #include <queue>
     8 #include <stack>
     9 #include <map>
    10 #include <string>
    11 #include <set>
    12 #define ms(a,b) memset((a),(b),sizeof((a)))
    13 using namespace std;
    14 typedef long long LL;
    15 const double EPS = 1e-8;
    16 const int INF = 2e9;
    17 const LL LNF = 2e18;
    18 const int MAXN = 10+10;
    19 
    20 int n, m;
    21 int g[MAXN][MAXN], c[MAXN], dp[200000][10];
    22 
    23 int main()
    24 {
    25     c[0] = 1;
    26     for(int i = 1; i<=10; i++)  //计算三进制
    27         c[i] = c[i-1]*3;
    28 
    29     while(scanf("%d%d", &n, &m)!=EOF)
    30     {
    31         memset(g, -1, sizeof(g));
    32         for(int i = 1; i<=m; i++)
    33         {
    34             int u, v, w;
    35             scanf("%d%d%d", &u, &v, &w);
    36             u--; v--;
    37             if(g[u][v]!=-1) w = min(w, g[u][v]);
    38             g[u][v] = g[v][u] = w;
    39         }
    40 
    41         memset(dp, -1, sizeof(dp));
    42         for(int i = 0; i<n; i++)
    43             dp[c[i]][i] = 0;
    44 
    45         int ans = INF;
    46         for(int s = 1; s<c[n]; s++)
    47         for(int j = 0; j<n; j++)
    48         {
    49             if(dp[s][j]==-1) continue;  //此状态不存在, 跳过
    50 
    51             int cnt = 0;
    52             for(int k = 0; k<n; k++)
    53             {
    54                 if(j!=k && g[j][k]!=-1 && (s/c[k])%3<2 )
    55                 {
    56                     int tmp = s + c[k];     //递推出下一个状态
    57                     if(dp[tmp][k]==-1 || dp[tmp][k]>dp[s][j]+g[j][k])   //更新
    58                         dp[tmp][k] = dp[s][j] + g[j][k];
    59                 }
    60 
    61                 if((s/c[k])%3) cnt++;   //统计状态s下走了多少个点
    62             }
    63 
    64             if(cnt==n) ans = min(ans, dp[s][j]);    //如果状态s下走完所有点, 则更新答案
    65         }
    66         printf("%d
    ", ans==INF?-1:ans);
    67     }
    68 }
    View Code
  • 相关阅读:
    java的泛型
    JAVA集合类--(一闪而过)
    java面向对象- -一闪而过
    进制转换之二进制与十进制转换
    跳跃表-原理及Java实现
    Excel二次开发相关代码
    程序员学炒股(7) 股市心得
    程序员学炒股(7) 股指期货收盘价对第二天开盘价有影响吗?
    程序员学炒股(6) 让我们来看一下7月份A股的表现
    程序员学炒股(5) 股指交割日效应是否存在?
  • 原文地址:https://www.cnblogs.com/DOLFAMINGO/p/7635000.html
Copyright © 2011-2022 走看看