hdu1964之插头DP求最优值
Pipes
Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 477 Accepted Submission(s): 238
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.
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).
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 #
#####
28
45
10
题意有点难理解,直接看输入好了,空格表示可通过的点,数字表示相邻点之间通过所须要的花费
问通过全部点并回到原点所须要的最少花费
插头DP模板题,仅仅须要将到达某个状态的数量改成最少花费就可以
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <string>
#include <queue>
#include <algorithm>
#include <map>
#include <cmath>
#include <iomanip>
#define INF 99999999
typedef long long LL;
using namespace std; const int MAX=100000+10;
const int N=10+10;
int n,m,size,index;
int mp[N][N],w[N][N][N][N],total[2],bit[N];//w[i][j][k][t]表示i,j->k,t的花费
int head[MAX],next[MAX],Hash[MAX];
LL dp[2][MAX],state[2][MAX],sum;//dp记录到达对应状态的最少花费 void Init(){
memset(mp,0,sizeof mp);
for(int i=1;i<=n;++i)for(int j=1;j<=m;++j)mp[i][j]=1;
index=0;
sum=INF;
total[index]=1;
dp[index][1]=0;
state[index][1]=0;
} void HashCalState(LL s,LL v){
int pos=s%MAX;
for(int i=head[pos];i != -1;i=next[i]){
if(state[index][Hash[i]] == s){
dp[index][Hash[i]]=min(dp[index][Hash[i]],v);
return;
}
}
++total[index];
state[index][total[index]]=s;
dp[index][total[index]]=v;
//头插法
Hash[size]=total[index];
next[size]=head[pos];
head[pos]=size++;
} void DP(){//採用4进制括号表示法
for(int i=1;i<=n;++i){
for(int k=1;k<=total[index];++k)state[index][k]<<=2;//由上一行到本行最后一个插头(0)去掉,在头添加一个插头(0)
for(int j=1;j<=m;++j){//决策i,j格
memset(head,-1,sizeof head);
size=0;
index=index^1;
total[index]=0;
for(int k=1;k<=total[index^1];++k){//上一格的有效状态数
LL s=state[index^1][k];
LL v=dp[index^1][k];
int p=(s>>bit[j-1])%4;
int q=(s>>bit[j])%4;
//这里就不用推断mp[i][j]能否够通过,由于这里一定能通过
if(!p && !q){//创建新连通块
if(!mp[i][j+1] || !mp[i+1][j])continue;
s=s+(1<<bit[j-1])+2*(1<<bit[j]);
v=v+w[i][j][i][j+1]+w[i][j][i+1][j];
HashCalState(s,v);
}else if(!p && q){
if(mp[i][j+1])HashCalState(s,v+w[i][j][i][j+1]);
if(mp[i+1][j]){
s=s+q*(1<<bit[j-1])-q*(1<<bit[j]);
v=v+w[i][j][i+1][j];
HashCalState(s,v);
}
}else if(p && !q){
if(mp[i+1][j])HashCalState(s,v+w[i][j][i+1][j]);
if(mp[i][j+1]){
s=s-p*(1<<bit[j-1])+p*(1<<bit[j]);
v=v+w[i][j][i][j+1];
HashCalState(s,v);
}
}else if(p == 1 && q == 1){
int b=1;
for(int t=j+1;t<=m;++t){
int a=(s>>bit[t])%4;
if(a == 1)++b;
if(a == 2)--b;
if(!b){
s=s+(1<<bit[t])-2*(1<<bit[t]);
break;
}
}
s=s-(1<<bit[j-1])-(1<<bit[j]);
HashCalState(s,v);
}else if(p == 2 && q == 2){
int b=1;
for(int t=j-2;t>=0;--t){
int a=(s>>bit[t])%4;
if(a == 2)++b;
if(a == 1)--b;
if(!b){
s=s-(1<<bit[t])+2*(1<<bit[t]);
break;
}
}
s=s-2*(1<<bit[j-1])-2*(1<<bit[j]);
HashCalState(s,v);
}else if(p == 1 && q == 2){
if(i == n && j == m)sum=min(sum,v);
}else if(p == 2 && q == 1){
s=s-2*(1<<bit[j-1])-(1<<bit[j]);
HashCalState(s,v);
}
}
}
}
} int main(){
for(int i=0;i<N;++i)bit[i]=i<<1;
int t;
char s[N+N];
scanf("%d",&t);
while(t--){
scanf("%d%d%*c",&n,&m);
Init();
gets(s);
for(int i=1;i<=n;++i){
gets(s);
for(int j=2;j<2*m+1;j+=2){
w[i][j/2][i][j/2+1]=s[j]-'0';
}
gets(s);
for(int j=1;j<2*m+1;j+=2){
w[i][j/2+1][i+1][j/2+1]=s[j]-'0';
}
}
DP();
printf("%lld\n",sum);
}
return 0;
}
hdu1964之插头DP求最优值的更多相关文章
- HDU1964 Pipes —— 插头DP
题目链接:https://vjudge.net/problem/HDU-1964 Pipes Time Limit: 5000/1000 MS (Java/Others) Memory Limi ...
- USACO 6.5 Betsy's Tour (插头dp)
Betsy's TourDon Piele A square township has been divided up into N2 square plots (1 <= N <= 7) ...
- 插头DP专题
建议入门的人先看cd琦的<基于连通性状态压缩的动态规划问题>.事半功倍. 插头DP其实是比较久以前听说的一个东西,当初是水了几道水题,最近打算温习一下,顺便看下能否入门之类. 插头DP建议 ...
- URAL 1519 基础插头DP
题目大意: 给定一个图,一部分点'*'作为障碍物,求经过所有非障碍点的汉密尔顿回路有多少条 基础的插头DP题目,对于陈丹琦的论文来说我觉得http://blog.sina.com.cn/s/blog_ ...
- hdu1693:eat trees(插头dp)
题目大意: 题目背景竟然是dota!屠夫打到大后期就没用了,,只能去吃树! 给一个n*m的地图,有些格子是不可到达的,要把所有可到达的格子的树都吃完,并且要走回路,求方案数 题解: 这题大概是最简单的 ...
- 插头DP题目泛做(为了对应WYD的课件)
题目1:BZOJ 1814 URAL 1519 Formula 1 题目大意:给定一个N*M的棋盘,上面有障碍格子.求一个经过所有非障碍格子形成的回路的数量. 插头DP入门题.记录连通分量. #inc ...
- 动态规划之插头DP入门
基于联通性的状态压缩动态规划是一类非常典型的状态压缩动态规划问题,由于其压缩的本质并不像是普通的状态压缩动态规划那样用0或者1来表示未使用.使用两种状态,而是使用数字来表示类似插头的状态,因此.它又被 ...
- ural1519插头DP
1519. Formula 1 Time limit: 1.0 second Memory limit: 64 MB Background Regardless of the fact, that V ...
- [入门向选讲] 插头DP:从零概念到入门 (例题:HDU1693 COGS1283 BZOJ2310 BZOJ2331)
转载请注明原文地址:http://www.cnblogs.com/LadyLex/p/7326874.html 最近搞了一下插头DP的基础知识……这真的是一种很锻炼人的题型…… 每一道题的状态都不一样 ...
随机推荐
- What is the difference between JRE,JVM and JDK?
If you are a Java developer, it is very often that you think about understanding the JRE,JVM and JDK ...
- TCP关闭过程
状态迁移 . SO_LINGER/ SO_REUSEADDR TCP正常的关闭过程如下(四次握手过程): (FIN_WAIT_1) A ---FIN---> B(CLOSE_WAIT) (FIN ...
- net 面向接口框架
Asp.net 面向接口框架之应用程序上下文作用域组件 在团队中推广面向接口开发两年左右,成果总体来说我还是挺满意的,使用面向接口开发的模块使用Unity容器配置的功能非常稳定,便于共享迁移(另一个项 ...
- [ACM] hdu 4418 Time travel (高斯消元求期望)
Time travel Problem Description Agent K is one of the greatest agents in a secret organization calle ...
- Codeforces 452A Eevee
#include<bits/stdc++.h> using namespace std; string m[]={"vaporeon","jolteon&qu ...
- WPF自定义圆形按钮样式资源文件
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" x ...
- java解析String类型t复杂xml,多级节点,最好的例子
需要用jar包 dom4j-1.6.1.jar 字符串xml如下: <root> <flw> <name>aa</name> <age>22 ...
- hdu2089(数位dp)
题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=2089 题意:求区间[a,b]内不含有62或4的数的个数. 分析:数位dp,dp[pos][0]表示到第 ...
- hdu 5282 Senior's String 两次dp
题链:http://acm.hdu.edu.cn/showproblem.php?pid=5282 Senior's String Time Limit: 2000/1000 MS (Java/Oth ...
- 如何用C#使用java
如何使用C#调用Java 今天需要使用C#调用Java的包,研究了一下,大体是以下几种解决方案: 把Java包转换为DLL或者EXE后注册为com组件,之后调用. 使用web service 比如:H ...