HDU 1964 Pipes(插头DP)
Pipes
Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 301 Accepted Submission(s): 154
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.
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
插头DP。
插头DP写起来确实很烦,很容易写错,要修改好久。
但是做多了也发现插头DP挺有趣的。
/* HDU 1964 Pipes 插头DP 每个格子之间的墙壁有一个花费,求用一个环经过每个格子一次的最小花费 G++ 46ms */ #include<stdio.h> #include<iostream> #include<string.h> #include<algorithm> using namespace std; const int MAXD=15; const int HASH=10007; const int STATE=1000010; struct Node { int down,right;//每个格子下边和右边墙的花费 }node[MAXD][MAXD]; int N,M; int maze[MAXD][MAXD]; int code[MAXD]; int ch[MAXD];//最小表示法使用 int ex,ey;//最后一个非障碍格子的坐标 struct HASHMAP { int head[HASH],next[STATE],size; int dp[STATE]; long long state[STATE];//最小表示法,最大是8^11,就是2^33,所以用long long void init() { size=0; memset(head,-1,sizeof(head)); } void push(long long st,int ans) { int i,h=st%HASH; for(i=head[h];i!=-1;i=next[i]) if(state[i]==st) { if(dp[i]>ans)dp[i]=ans; return; } dp[size]=ans; state[size]=st; next[size]=head[h]; head[h]=size++; } }hm[2]; void decode(int *code,int m,long long st) { for(int i=m;i>=0;i--) { code[i]=st&7; st>>=3; } } long long encode(int *code,int m) { int cnt=1; memset(ch,-1,sizeof(ch)); ch[0]=0; long long st=0; for(int i=0;i<=m;i++) { if(ch[code[i]]==-1)ch[code[i]]=cnt++; code[i]=ch[code[i]]; st<<=3; st|=code[i]; } return st; } void shift(int *code,int m) { for(int i=m;i>0;i--)code[i]=code[i-1]; code[0]=0; } void dpblank(int i,int j,int cur) { int k,left,up; for(k=0;k<hm[cur].size;k++) { decode(code,M,hm[cur].state[k]); left=code[j-1]; up=code[j]; if(left&&up) { if(left==up)//只出现在最后一个格子 { if(i==ex&&j==ey) { code[j-1]=code[j]=0; if(j==M)shift(code,M); hm[cur^1].push(encode(code,M),hm[cur].dp[k]); } } else { code[j-1]=code[j]=0; for(int t=0;t<=M;t++) if(code[t]==left) code[t]=up; if(j==M)shift(code,M); hm[cur^1].push(encode(code,M),hm[cur].dp[k]); } } else if((left&&(!up))||((!left)&&up)) { int t; if(left)t=left; else t=up; if(maze[i][j+1]) { code[j-1]=0; code[j]=t; hm[cur^1].push(encode(code,M),hm[cur].dp[k]+node[i][j].right); } if(maze[i+1][j]) { code[j-1]=t; code[j]=0; if(j==M)shift(code,M); hm[cur^1].push(encode(code,M),hm[cur].dp[k]+node[i][j].down); } } else//无插头 { if(maze[i][j+1]&&maze[i+1][j]) { code[j-1]=code[j]=13; hm[cur^1].push(encode(code,M),hm[cur].dp[k]+node[i][j].down+node[i][j].right); } } } } char str[30]; void init() { scanf("%d%d%*c",&N,&M);//跳过一个字符 memset(maze,0,sizeof(maze)); for(int i=1;i<=N;i++) for(int j=1;j<=M;j++) maze[i][j]=1; gets(str); for(int i=1;i<N;i++) { gets(str); for(int j=1;j<M;j++) node[i][j].right=str[2*j]-'0'; gets(str); for(int j=1;j<=M;j++) node[i][j].down=str[2*j-1]-'0'; } gets(str); for(int j=1;j<M;j++) node[N][j].right=str[2*j]-'0'; gets(str); ex=N; ey=M; } void solve() { int i,j,cur=0; int ans=0; hm[cur].init(); hm[cur].push(0,0); for(i=1;i<=N;i++) for(j=1;j<=M;j++) { hm[cur^1].init(); dpblank(i,j,cur); cur^=1; } for(i=0;i<hm[cur].size;i++) ans+=hm[cur].dp[i]; printf("%d\n",ans); } int main() { // freopen("in.txt","r",stdin); // freopen("out.txt","w",stdout); int T; scanf("%d",&T); while(T--) { init(); solve(); } return 0; }
人一我百!人十我万!永不放弃~~~怀着自信的心,去追逐梦想