• HDU1964 Pipes —— 插头DP


    题目链接:https://vjudge.net/problem/HDU-1964

    Pipes

    Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
    Total Submission(s): 987    Accepted Submission(s): 494


    Problem Description
    The construction of office buildings has become a very standardized task. Pre-fabricated modules are combined according to the customer’s needs, shipped from a faraway factory, and assembled on the construction site. However, there are still some tasks that require careful planning, one example being the routing of pipes for the heating system. 

    Amodern office building ismade up of squaremodules, one on each floor being a service module from which (among other things) hot water is pumped out to the other modules through the heating pipes. Each module (including the service module) will have heating pipes connecting it to exactly two of its two to four neighboring modules. Thus, the pipes have to run in a circuit, from the service module, visiting each module exactly once, before finally returning to the service module. Due to different properties of the modules, having pipes connecting a pair of adjacent modules comes at different costs. For example, some modules are separated by thick walls, increasing the cost of laying pipes. Your task is to, given a description of a floor of an office building, decide the cheapest way to route the heating pipes.
     
    Input
    The first line of input contains a single integer, stating the number of floors to handle. Then follow n floor descriptions, each beginning on a new line with two integers, 2 <= r <= 10 and 2 <= c <= 10, defining the size of the floor – r-by-c modules. Beginning on the next line follows a floor description in ASCII format, in total 2r + 1 rows, each with 2c + 2 characters, including the final newline. All floors are perfectly rectangular, and will always have an even number of modules. All interior walls are represented by numeric characters, ’0’ to ’9’, indicating the cost of routing pipes through the wall (see sample input).
     
    Output
    For each test case, output a single line with the cost of the cheapest route.
     
    Sample Input
    3 4 3 ####### # 2 3 # #1#9#1# # 2 3 # #1#7#1# # 5 3 # #1#9#1# # 2 3 # ####### 4 4 ######### # 2 3 3 # #1#9#1#4# # 2 3 6 # #1#7#1#5# # 5 3 1 # #1#9#1#7# # 2 3 0 # ######### 2 2 ##### # 1 # #2#3# # 4 # #####
     
    Sample Output
    28 45 10
     
    Source
     
    Recommend
    wangye

    题意:

    给出一个n*m(n、m<=10)的棋盘,对于一个格子,分别给出它与四个方向相连所需要的花费(即打穿这堵墙所需的花费),求一个回路使得这个回路经过所有的格子刚好一次,且花费是最小的,输出最小花费。

    题解:

    与此题(URAL1519 Formula 1 )无异,只不过把统计个数改成求最小值。

    代码如下:

      1 #include <iostream>
      2 #include <cstdio>
      3 #include <cstring>
      4 #include <algorithm>
      5 #include <vector>
      6 #include <cmath>
      7 #include <queue>
      8 #include <stack>
      9 #include <map>
     10 #include <string>
     11 #include <set>
     12 using namespace std;
     13 typedef long long LL;
     14 const int INF = 2e9;
     15 const LL LNF = 9e18;
     16 const int MOD = 1e9+7;
     17 const int MAXN = 1e5;
     18 const int HASH = 1e4;
     19 
     20 int n, m, last_x, last_y;
     21 bool maze[15][15];
     22 int down_cost[15][15], ri_cost[15][15];
     23 
     24 struct
     25 {
     26     int size, head[HASH], next[MAXN];
     27     LL state[MAXN];
     28     int cost[MAXN];
     29 
     30     void init()
     31     {
     32         size = 0;
     33         memset(head, -1, sizeof(head));
     34     }
     35 
     36     void insert(LL status, int Cost)
     37     {
     38         int u = status%HASH;
     39         for(int i = head[u]; i!=-1; i = next[i])
     40         {
     41             if(state[i]==status)
     42             {
     43                 cost[i] = min(cost[i], Cost);
     44                 return;
     45             }
     46         }
     47         state[size] = status;
     48         cost[size] = Cost;
     49         next[size] = head[u];
     50         head[u] = size++;
     51     }
     52 
     53 }Hash_map[2];
     54 
     55 struct
     56 {
     57     int code[13];
     58     LL encode(int m)
     59     {
     60         LL status = 0;
     61         int id[13], cnt = 0;
     62         memset(id, -1, sizeof(id));
     63         id[0] = 0;
     64         for(int i = m; i>=0; i--)
     65         {
     66             if(id[code[i]]==-1) id[code[i]] = ++cnt;
     67             code[i] = id[code[i]];
     68             status <<= 3;
     69             status += code[i];
     70         }
     71         return status;
     72     }
     73 
     74     void decode(int m, LL status)
     75     {
     76         memset(code, 0, sizeof(code));
     77         for(int i = 0; i<=m; i++)
     78         {
     79             code[i] = status&7;
     80             status >>= 3;
     81         }
     82     }
     83 
     84     void shift(int m)
     85     {
     86         for(int i = m-1; i>=0; i--)
     87             code[i+1] = code[i];
     88         code[0] = 0;
     89     }
     90 
     91 }Line;
     92 
     93 //插头的花费在新建的时候加进去
     94 void transfer(int i, int j, int cur)
     95 {
     96     for(int k = 0; k<Hash_map[cur].size; k++)
     97     {
     98         LL status = Hash_map[cur].state[k];
     99         LL Cost = Hash_map[cur].cost[k];
    100         Line.decode(m, status);
    101         int up = Line.code[j];
    102         int left = Line.code[j-1];
    103 
    104         if(!up && !left)
    105         {
    106             if(maze[i+1][j] && maze[i][j+1])
    107             {
    108                 Line.code[j] = Line.code[j-1] = 5;  //最多只有5个连通分量
    109                 Hash_map[cur^1].insert(Line.encode(m), Cost+down_cost[i][j]+ri_cost[i][j]);
    110             }
    111         }
    112         else if( (left&&!up) || (!left&&up) )
    113         {
    114             int line = left?left:up;
    115             if(maze[i][j+1])
    116             {
    117                 Line.code[j-1] = 0;
    118                 Line.code[j] = line;
    119                 Hash_map[cur^1].insert(Line.encode(m), Cost+ri_cost[i][j]);
    120             }
    121             if(maze[i+1][j])
    122             {
    123                 Line.code[j-1] = line;
    124                 Line.code[j] = 0;
    125                 if(j==m) Line.shift(m);
    126                 Hash_map[cur^1].insert(Line.encode(m), Cost+down_cost[i][j]);
    127             }
    128         }
    129         else
    130         {
    131             if(up!=left)
    132             {
    133                 Line.code[j] = Line.code[j-1] = 0;
    134                 for(int t = 0; t<=m; t++)
    135                     if(Line.code[t]==up)
    136                         Line.code[t] = left;
    137                 if(j==m) Line.shift(m);
    138                 Hash_map[cur^1].insert(Line.encode(m), Cost);
    139             }
    140             else if(i==last_x && j==last_y)
    141             {
    142                 Line.code[j] = Line.code[j-1] = 0;
    143                 if(j==m) Line.shift(m);
    144                 Hash_map[cur^1].insert(Line.encode(m), Cost);
    145             }
    146         }
    147     }
    148 }
    149 
    150 int main()
    151 {
    152     int T;
    153     char str[50];
    154     scanf("%d", &T);
    155     while(T--)
    156     {
    157         scanf("%d%d", &n, &m); getchar();
    158         memset(maze, false, sizeof(maze));
    159         for(int i = 1; i<=n; i++)
    160         for(int j = 1; j<=m; j++)
    161             maze[i][j] = true;
    162 
    163         gets(str);
    164         for(int i = 1; i<n; i++)
    165         {
    166             gets(str);
    167             for(int j = 1; j<m; j++)
    168                 ri_cost[i][j] = str[j*2] - '0';
    169             gets(str);
    170             for(int j = 1; j<=m; j++)
    171                 down_cost[i][j] = str[j*2-1] - '0';
    172         }
    173         gets(str);
    174         for(int j = 1; j<m; j++)
    175             ri_cost[n][j] = str[j*2] - '0';
    176         gets(str);
    177         last_x = n;
    178         last_y = m;
    179 
    180         int cur = 0;
    181         Hash_map[cur].init();
    182         Hash_map[cur].insert(0, 0);
    183         for(int i = 1; i<=n; i++)
    184         for(int j = 1; j<=m; j++)
    185         {
    186             Hash_map[cur^1].init();
    187             transfer(i, j, cur);
    188             cur ^= 1;
    189         }
    190 
    191         LL last_status = 0;
    192         LL ans = Hash_map[cur].size?Hash_map[cur].cost[last_status]:0;
    193         printf("%d
    ", ans);
    194     }
    195 }
    View Code
  • 相关阅读:
    Google笔试题
    OpenStack Ceilometer简介
    OpenStack Object Storage(Swift)架构、原理及特性
    【大话存储2读书笔记】磁盘IO的重要概念
    递归,汉诺塔游戏
    函数定义与使用
    字符串操作
    for循环:用turtle画一颗五角星
    列表、元组、字典、集合的定义、操作与综合练习
    npm 相关
  • 原文地址:https://www.cnblogs.com/DOLFAMINGO/p/8012898.html
Copyright © 2020-2023  润新知