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\n", ans);
194     }
195 }
View Code

 

转载于:https://www.cnblogs.com/DOLFAMINGO/p/8012898.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: hdu 2829 Lawrence 斜率优化dp 这道题是一道经典的斜率优化dp题目,需要用到单调队列的思想。 题目大意是给定一个序列a,求出一个序列b,使得b[i]表示a[1]~a[i]中的最小值,且满足b[i] = min{b[j] + (i-j)*k},其中k为给定的常数。 我们可以将上式拆开,得到b[i] = min{b[j] - j*k} + i*k,即b[i] = i*k + min{b[j] - j*k},这个式子就是斜率优化dp的形式。 我们可以用单调队列来维护min{b[j] - j*k},具体思路如下: 1. 首先将第一个元素加入队列中。 2. 从第二个元素开始,我们需要将当前元素加入队列中,并且需要维护队列的单调性。 3. 维护单调性的方法是,我们从队列的末尾开始,将队列中所有大于当前元素的元素弹出,直到队列为空或者队列中最后一个元素小于当前元素为止。 4. 弹出元素的同时,我们需要计算它们对应的斜率,即(b[j]-j*k)/(j-i),并将这些斜率与当前元素的斜率比较,如果当前元素的斜率更小,则将当前元素加入队列中。 5. 最后队列中的第一个元素就是min{b[j] - j*k},我们将它加上i*k就得到了b[i]的值。 6. 重复以上步骤直到处理完所有元素。 具体实现可以参考下面的代码: ### 回答2: HDU 2829 Lawrence 斜率优化 DP 是一道经典的斜率优化 DP 题目,其思想是通过维护一个下凸包来优化 DP 算法。下面我们来具体分析一下这道题目。 首先,让我们看一下该题目的描述。题目给定一些木棒,要求我们将这些木棒割成一些给定长度,且要求每种长度的木棒的数量都是一样的,求最小的割枝次数。这是一个典型的背包问题,而且在此基础上还要求每种长度的木棒的数量相同,这就需要我们在状态设计上走一些弯路。 我们来看一下状态的定义。定义 $dp[i][j]$ 表示前 $i$ 个木棒中正好能割出 $j$ 根长度为 $c_i$ 的木棒的最小割枝次数。对于每个 $dp[i][j]$,我们可以分类讨论: 1. 不选当前的木棒,即 $dp[i][j]=dp[i-1][j]$; 2. 选当前的木棒,即 $dp[i][j-k]=dp[i-1][j-k]+k$,其中 $k$ 是 $j/c_i$ 的整数部分。 现在问题再次转化为我们需要在满足等量限制的情况下,求最小的割枝次数。可以看出,这是一个依赖于 $c_i$ 的限制。于是,我们可以通过斜率优化 DP 来解决这个问题。 我们来具体分析一下斜率优化 DP 算法的思路。我们首先来看一下动态规划的状态转移方程 $dp[i][j]=\min\{dp[i-1][k]+x_k(i,j)\}$。可以发现,$dp[i][j]$ 的最小值只与 $dp[i-1][k]$ 和 $x_k(i,j)$ 有关。其中,$x_k(i,j)$ 表示斜率,其值为 $dp[i-1][k]-k\times c_i+j\times c_i$。 接下来,我们需要维护一个下凸包,并通过斜率进行优化。我们具体分析一下该过程。假设我们当前要计算 $dp[i][j]$。首先,我们需要找到当前点 $(i,j)$ 在凸包上的位置,即斜率最小值的位置。然后,我们根据该位置的斜率计算 $dp[i][j]$ 的值。接下来,我们需要将当前点 $(i,j)$ 加入到下凸包上。 我们在加入点的时候需要注意几点。首先,我们需要将凸包中所有斜率比当前点小的点移除,直到该点能够加入到凸包中为止。其次,我们需要判断该点是否能够加入到凸包中。如果不能加入到凸包中,则直接舍弃。最后,我们需要保证凸包中斜率是单调递增的,这就需要在加入新的点之后进行上一步操作。 以上就是该题目的解题思路。需要注意的是,斜率优化 DP 算法并不是万能的,其使用情况需要根据具体的问题情况来确定。同时,该算法中需要维护一个下凸包,可能会增加一些算法的复杂度,建议和常规 DP 算法进行对比,选择最优的算法进行解题。 ### 回答3: 斜率优化DP是一种动态规划优化算法,其主要思路是通过对状态转移方程进行变形,提高算法的时间复杂度。HDU2829 Lawrence问题可以用斜率优化DP解决。 首先,我们需要了解原问题的含义。问题描述如下:有$n$个人在数轴上,第$i$个人的位置为$A_i$,每个人可以携带一定大小的行李,第$i$个人的行李重量为$B_i$,但是每个人只能帮助没有他们重量大的人搬行李。若第$i$个人搬运了第$j$个人的行李,那么第$i$个人会累加$C_{i,j}=\left|A_i-A_j\right|\cdot B_j$的体力消耗。求$m$个人帮助每个人搬运行李的最小体力消耗。 我们可以通过斜率优化DP解决这个问题。记$f_i$为到前$i$个人的最小体力消耗,那么状态转移方程为: $$f_i=\min_{j<i}\{f_j+abs(A_i-A_j)\cdot B_i\}$$ 如果直接使用该方程,时间复杂度为$O(n^2)$,如果$n=10^4$,则需要计算$10^8$次,运算时间极长。斜率优化DP通过一些数学推导将方程变形,将时间复杂度降低到$O(n)$,大大缩短了计算时间。 通过斜率优化DP的推导式子,我们可以得到转移方程为: $$f_i=\min_{j<i}\{f_j+slope(j,i)\}$$ 其中,$slope(j,i)$表示直线$j-i$的斜率。我们可以通过如下方式来求解$slope(j,i)$: $$slope(j,i)=\frac{f_i-f_j}{A_i-A_j}-B_i-B_j$$ 如果$slope(j,i)\leq slope(j,k)$,那么$j$一定不是最优,可以直接舍去,降低计算时间。该算法的时间复杂度为$O(n)$。 综上所述,斜率优化DP是一种动态规划优化算法,可以大大缩短计算时间。在处理类似HDU2829 Lawrence问题的时候,斜率优化DP可以很好地解决问题。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值