【滚动数组】【状压dp】Gym - 100956F - Colored Path

f(i,j,S)表示到(i,j),且经由的路径上的颜色集合为S的价值的最小值,从上方和左方转移过来即可。
要注意,内存不足,需要滚动数组优化,即使用了map,还是需要。
路径输出的时候,可以再跑一遍dp,这样就不用再开一个大数组了。
我的写法比较菜。卡了常数
#include<cstdio>
#include<algorithm>
#include<map>
using namespace std;
int w[401][401],c[401][401];
typedef pair<int,int> Point;
map<int,int>f[401][401];
typedef pair<int,Point> data;
typedef map<int,int>::iterator ITER;
map<int,data>g[401][401];
int n,K,W;
int calc(int x)
{
int res=0;
while(x)
{
res+=(x&1);
x>>=1;
}
return res;
}
Point path[1001];
int e;
int main()
{
scanf("%d%d%d",&n,&K,&W);
for(int i=1;i<=n;++i)
for(int j=1;j<=n;++j)
scanf("%d",&w[i][j]);
for(int i=1;i<=n;++i)
for(int j=1;j<=n;++j)
scanf("%d",&c[i][j]);
if(w[1][1]>W)
{
puts("-1");
return 0;
}
f[1][1][1<<(c[1][1]-1)]=w[1][1];
for(int i=1;i<=n;++i){
for(int j=1;j<=n;++j)
{
if(i>1 && j>1)
{
for(ITER it=f[i-1][j].begin();it!=f[i-1][j].end();++it)
{
int nS=(*it).first|(1<<(c[i][j]-1)),tmp=(*it).second+w[i][j];
if(f[i][j].find(nS)==f[i][j].end())
{
if(tmp<=W)
{
f[i][j][nS]=tmp;
g[i][j][nS]=data((*it).first,Point(i-1,j));
}
}
else if(f[i][j][nS]>tmp)
{
f[i][j][nS]=tmp;
g[i][j][nS]=data((*it).first,Point(i-1,j));
}
}
for(ITER it=f[i][j-1].begin();it!=f[i][j-1].end();++it)
{
int nS=(*it).first|(1<<(c[i][j]-1)),tmp=(*it).second+w[i][j];
if(f[i][j].find(nS)==f[i][j].end())
{
if(tmp<=W)
{
f[i][j][nS]=tmp;
g[i][j][nS]=data((*it).first,Point(i,j-1));
}
}
else if(f[i][j][nS]>tmp)
{
f[i][j][nS]=tmp;
g[i][j][nS]=data((*it).first,Point(i,j-1));
}
}
}
else if(i==1 && j>1)
{
for(ITER it=f[i][j-1].begin();it!=f[i][j-1].end();++it)
{
int nS=(*it).first|(1<<(c[i][j]-1)),tmp=(*it).second+w[i][j];
if(f[i][j].find(nS)==f[i][j].end())
{
if(tmp<=W)
{
f[i][j][nS]=tmp;
g[i][j][nS]=data((*it).first,Point(i,j-1));
}
}
else if(f[i][j][nS]>tmp)
{
f[i][j][nS]=tmp;
g[i][j][nS]=data((*it).first,Point(i,j-1));
}
}
}
else if(i>1 && j==1)
{
for(ITER it=f[i-1][j].begin();it!=f[i-1][j].end();++it)
{
int nS=(*it).first|(1<<(c[i][j]-1)),tmp=(*it).second+w[i][j];
if(f[i][j].find(nS)==f[i][j].end())
{
if(tmp<=W)
{
f[i][j][nS]=tmp;
g[i][j][nS]=data((*it).first,Point(i-1,j));
}
}
else if(f[i][j][nS]>tmp)
{
f[i][j][nS]=tmp;
g[i][j][nS]=data((*it).first,Point(i-1,j));
}
}
}
}
if(i>=3)
{
for(int j=1;j<=n;++j)
{
f[i-2][j].clear();
// g[i-2][j].
}
}
}
int ans=2147483647;
int anS;
for(ITER it=f[n][n].begin();it!=f[n][n].end();++it)
{
int tmp=calc((*it).first);
if(tmp<ans)
{
ans=tmp;
anS=(*it).first;
}
}
if(ans==2147483647)
puts("-1");
else
{
printf("%d\n",ans);
data U=data(anS,Point(n,n));
path[++e]=Point(n,n);
while(U.second!=Point(1,1))
{
U=g[U.second.first][U.second.second][U.first];
path[++e]=U.second;
}
for(int i=e;i>=1;--i)
printf("%d %d%c",path[i].first,path[i].second,i==1 ? '\n' : ' ');
}
return 0;
}
【滚动数组】【状压dp】Gym - 100956F - Colored Path的更多相关文章
- 状压dp Gym - 100676G
http://codeforces.com/gym/100676 题目大意: 给你n个科目,m个关系,例如A->B,表示要学习B科目,一定要把A科目学习掉.同理,如果还有C->B,那么,B ...
- zoj3545Rescue the Rabbit (AC自动机+状压dp+滚动数组)
Time Limit: 10 Seconds Memory Limit: 65536 KB Dr. X is a biologist, who likes rabbits very much ...
- Codeforces Gym 100015F Fighting for Triangles 状压DP
Fighting for Triangles 题目连接: http://codeforces.com/gym/100015/attachments Description Andy and Ralph ...
- Codeforces Gym 100610 Problem K. Kitchen Robot 状压DP
Problem K. Kitchen Robot Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/10061 ...
- POJ 3311 Hie with the Pie (状压DP)
dp[i][j][k] i代表此层用的状态序号 j上一层用的状态序号 k是层数&1(滚动数组) 标准流程 先预处理出所有合法数据存在status里 然后独立处理第一层 然后根据前一层的max推 ...
- ZOJ3802 Easy 2048 Again (状压DP)
ZOJ Monthly, August 2014 E题 ZOJ月赛 2014年8月 E题 http://acm.zju.edu.cn/onlinejudge/showProblem.do?proble ...
- 状压DP uvalive 6560
// 状压DP uvalive 6560 // 题意:相邻格子之间可以合并,合并后的格子的值是之前两个格子的乘积,没有合并的为0,求最大价值 // 思路: // dp[i][j]:第i行j状态下的值 ...
- ZOJ 3723 (浙大月赛)状压DP
A了一整天~~~终于搞掉了. 真是血都A出来了. 题目意思很清楚,肯定是状压DP. 我们可以联系一下POJ 1185 炮兵阵地,经典的状压DP. 两道题的区别就在于,这道题的攻击是可以被X挡住的,而 ...
- hdu_4529_郑厂长系列故事——N骑士问题(状压DP)
题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=4529 题意:中文,不解释 题解:状压DP,dp[i][j][k][s]表示第i行当前用了j个骑士,i- ...
随机推荐
- bash报错./mq.sh: line 15: warning: here-document at line 10 delimited by end-of-file (wanted `eof')
[root@localhost tmp]# ./mq.sh./mq.sh: line 15: warning: here-document at line 10 delimited by end-of ...
- 剑指offer 整数中1 出现的次数
给定一个十进制正整数N,写下从1开始,到N的所有整数,然后数一下其中出现的所有"1"的个数. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 1 ...
- mac编译openssl扩展报错 openssl.c:44:10: fatal error: 'openssl/evp.h' file not found
解决办法 brew link openssl --force 然后 ./configure --with-openssl --with-php-config=/usr/local/php/bin/ph ...
- Gerald and Giant Chess
Gerald and Giant Chess time limit per test 2 seconds memory limit per test 256 megabytes input stand ...
- 1381: Munching(BFS)
Description Bessie loves her grass and loves to hurry to the barn for her evening milking session. S ...
- HDU 3201 Build a Fence
水题 #include<cstdio> #include<cstring> #include<cmath> #include<algorithm> us ...
- HashSet 与HashMap底层实现
1. HashSet底层通过包装HashMap来实现,HashSet在添加一个值的时候,实际上是将此值作为HashMap中的key来进行保存. 2. HashMap的底层实现是通过初始化化一个Entr ...
- USACO 1.3.3 Prime Cryptarithm
题目链接:1.3.3 我用的枚举法,即每产生一组数据就判断是否是所给数字里的. AC还沾沾自喜,但一看题解,发现自己的代码真low... 在平时练习时,应该追求高效,精炼的代码,这样比赛时才能省出大量 ...
- 二部图(二分图判定--dfs)
题目链接:二部图 二部图 时间限制:1000 ms | 内存限制:65535 KB 难度:1 描述 二 部图又叫二分图,我们不是求它的二分图最大匹配,也不是完美匹配,也不是多重匹配,而是证明一个图 ...
- pcap filter
今天用tshark抓包,本以为wireshark能用的filter,如“mysql”它也应该能用,其实不然:tshark -f只认识pcap filter,用-R的话,说要用-2,加上-2的话又说什么 ...