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
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.
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 #
#####
45
10
题意:
给出一个n*m(n、m<=10)的棋盘,对于一个格子,分别给出它与四个方向相连所需要的花费(即打穿这堵墙所需的花费),求一个回路使得这个回路经过所有的格子刚好一次,且花费是最小的,输出最小花费。
题解:
与此题(URAL1519 Formula 1)无异,只不过把统计个数改成求最小值。
代码如下:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <string>
#include <set>
using namespace std;
typedef long long LL;
const int INF = 2e9;
const LL LNF = 9e18;
const int MOD = 1e9+;
const int MAXN = 1e5;
const int HASH = 1e4; int n, m, last_x, last_y;
bool maze[][];
int down_cost[][], ri_cost[][]; struct
{
int size, head[HASH], next[MAXN];
LL state[MAXN];
int cost[MAXN]; void init()
{
size = ;
memset(head, -, sizeof(head));
} void insert(LL status, int Cost)
{
int u = status%HASH;
for(int i = head[u]; i!=-; i = next[i])
{
if(state[i]==status)
{
cost[i] = min(cost[i], Cost);
return;
}
}
state[size] = status;
cost[size] = Cost;
next[size] = head[u];
head[u] = size++;
} }Hash_map[]; struct
{
int code[];
LL encode(int m)
{
LL status = ;
int id[], cnt = ;
memset(id, -, sizeof(id));
id[] = ;
for(int i = m; i>=; i--)
{
if(id[code[i]]==-) id[code[i]] = ++cnt;
code[i] = id[code[i]];
status <<= ;
status += code[i];
}
return status;
} void decode(int m, LL status)
{
memset(code, , sizeof(code));
for(int i = ; i<=m; i++)
{
code[i] = status&;
status >>= ;
}
} void shift(int m)
{
for(int i = m-; i>=; i--)
code[i+] = code[i];
code[] = ;
} }Line; //插头的花费在新建的时候加进去
void transfer(int i, int j, int cur)
{
for(int k = ; k<Hash_map[cur].size; k++)
{
LL status = Hash_map[cur].state[k];
LL Cost = Hash_map[cur].cost[k];
Line.decode(m, status);
int up = Line.code[j];
int left = Line.code[j-]; if(!up && !left)
{
if(maze[i+][j] && maze[i][j+])
{
Line.code[j] = Line.code[j-] = ; //最多只有5个连通分量
Hash_map[cur^].insert(Line.encode(m), Cost+down_cost[i][j]+ri_cost[i][j]);
}
}
else if( (left&&!up) || (!left&&up) )
{
int line = left?left:up;
if(maze[i][j+])
{
Line.code[j-] = ;
Line.code[j] = line;
Hash_map[cur^].insert(Line.encode(m), Cost+ri_cost[i][j]);
}
if(maze[i+][j])
{
Line.code[j-] = line;
Line.code[j] = ;
if(j==m) Line.shift(m);
Hash_map[cur^].insert(Line.encode(m), Cost+down_cost[i][j]);
}
}
else
{
if(up!=left)
{
Line.code[j] = Line.code[j-] = ;
for(int t = ; t<=m; t++)
if(Line.code[t]==up)
Line.code[t] = left;
if(j==m) Line.shift(m);
Hash_map[cur^].insert(Line.encode(m), Cost);
}
else if(i==last_x && j==last_y)
{
Line.code[j] = Line.code[j-] = ;
if(j==m) Line.shift(m);
Hash_map[cur^].insert(Line.encode(m), Cost);
}
}
}
} int main()
{
int T;
char str[];
scanf("%d", &T);
while(T--)
{
scanf("%d%d", &n, &m); getchar();
memset(maze, false, sizeof(maze));
for(int i = ; i<=n; i++)
for(int j = ; j<=m; j++)
maze[i][j] = true; gets(str);
for(int i = ; i<n; i++)
{
gets(str);
for(int j = ; j<m; j++)
ri_cost[i][j] = str[j*] - '';
gets(str);
for(int j = ; j<=m; j++)
down_cost[i][j] = str[j*-] - '';
}
gets(str);
for(int j = ; j<m; j++)
ri_cost[n][j] = str[j*] - '';
gets(str);
last_x = n;
last_y = m; int cur = ;
Hash_map[cur].init();
Hash_map[cur].insert(, );
for(int i = ; i<=n; i++)
for(int j = ; j<=m; j++)
{
Hash_map[cur^].init();
transfer(i, j, cur);
cur ^= ;
} LL last_status = ;
LL ans = Hash_map[cur].size?Hash_map[cur].cost[last_status]:;
printf("%d\n", ans);
}
}
HDU1964 Pipes —— 插头DP的更多相关文章
- hdu1964之插头DP求最优值
Pipes Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Subm ...
- 插头DP专题
建议入门的人先看cd琦的<基于连通性状态压缩的动态规划问题>.事半功倍. 插头DP其实是比较久以前听说的一个东西,当初是水了几道水题,最近打算温习一下,顺便看下能否入门之类. 插头DP建议 ...
- 插头dp练习
最近学了插头dp,准备陆续更新插头dp类练习. 学习论文还是cdq那篇<基于连通性状态压缩的动态规划问题>. 基本的想法都讲得很通透了,接下来就靠自己yy了. 还有感谢kuangbin大大 ...
- 插头dp
插头dp 感受: 我觉得重点是理解,算法并不是直接想出怎样由一种方案变成另一种方案.而是方案本来就在那里,我们只是枚举状态统计了答案. 看看cdq的讲义什么的,一开始可能觉得状态很多,但其实灰常简单 ...
- HDU 4113 Construct the Great Wall(插头dp)
好久没做插头dp的样子,一开始以为这题是插头,状压,插头,状压,插头,状压,插头,状压,无限对又错. 昨天看到的这题. 百度之后发现没有人发题解,hust也没,hdu也没discuss...在acm- ...
- HDU 4949 Light(插头dp、位运算)
比赛的时候没看题,赛后看题觉得比赛看到应该可以敲的,敲了之后发现还真就会卡题.. 因为写完之后,无限TLE... 直到后来用位运算代替了我插头dp常用的decode.encode.shift三个函数以 ...
- HDU 1693 Eat the Trees(插头DP、棋盘哈密顿回路数)+ URAL 1519 Formula 1(插头DP、棋盘哈密顿单回路数)
插头DP基础题的样子...输入N,M<=11,以及N*M的01矩阵,0(1)表示有(无)障碍物.输出哈密顿回路(可以多回路)方案数... 看了个ppt,画了下图...感觉还是挺有效的... 参考 ...
- HDU 1693 Eat the Trees(插头DP)
题目链接 USACO 第6章,第一题是一个插头DP,无奈啊.从头看起,看了好久的陈丹琦的论文,表示木看懂... 大体知道思路之后,还是无法实现代码.. 此题是插头DP最最简单的一个,在一个n*m的棋盘 ...
- HDU 4064 Carcassonne(插头DP)(The 36th ACM/ICPC Asia Regional Fuzhou Site —— Online Contest)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4064 Problem Description Carcassonne is a tile-based ...
随机推荐
- cf725F Family Photos
Alice and Bonnie are sisters, but they don't like each other very much. So when some old family phot ...
- 作诗(bzoj 2821)
Description 神犇SJY虐完HEOI之后给傻×LYD出了一题:SHY是T国的公主,平时的一大爱好是作诗.由于时间紧迫,SHY作完诗 之后还要虐OI,于是SHY找来一篇长度为N的文章,阅读M次 ...
- 球形空间产生器 BZOJ 1013
球形空间产生器 [问题描述] 有一个球形空间产生器能够在n维空间中产生一个坚硬的球体.现在,你被困在了这个n维球体中,你只知道球面上n+1个点的坐标,你需要以最快的速度确定这个n维球体的球心坐标,以便 ...
- 解析XML字符串为json对象
var overtime='<?xml version="1.0" encoding="UTF-8"?><response><co ...
- HDU 4022 stl multiset
orz kss太腻害了. 一.set和multiset基础 set和multiset会根据特定的排序准则,自动将元素进行排序.不同的是后者允许元素重复而前者不允许. 需要包含头文件: #include ...
- (8)C#连sqlserver
str="Data Source=ip; Network Library=DBMSSOCN; Initial Catalog=数据库; User ID=sa; Password=xx&quo ...
- P2085 最小函数值 洛谷
https://www.luogu.org/problem/show?pid=2085 题目描述 有n个函数,分别为F1,F2,...,Fn.定义Fi(x)=Ai*x^2+Bi*x+Ci (x∈N*) ...
- codevs——1294 全排列
1294 全排列 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 黄金 Gold 题解 查看运行结果 题目描述 Description 给出一个n, 请输出n的所有全 ...
- CocoaPods为project的全部target添加依赖支持
在使用CocoaPods时.pod install默认仅仅能为xcodeproject的第一个target加入依赖库支持.假设要为全部的target添加可依照例如以下步骤进行 两种情 1. 编辑Pod ...
- RESTful Web Service