传送门

吐槽:神tm网络流。。。

用持有的钥匙分层,状态压缩,用 2 进制表示持有的钥匙集合。

dis[i][j][k] 表示持有的钥匙集合为 k,到达点 (i, j) 的最短路径。

分层图的最短路听上去很玄乎,其实通过代码来看还是很好理解的。

——代码

 #include <queue>
#include <cstdio>
#include <cstring>
#include <iostream>
#define N 20
#define min(x, y) ((x) < (y) ? (x) : (y)) int n, m, p, ans = ~( << );
int map[N][N][N][N], key[N][N], dis[N][N][ << ];
int dx[] = {, , , -}, dy[] = {, , -, };
bool vis[N][N][ << ]; struct node
{
int x, y, s;
node(int x = , int y = , int s = ) : x(x), y(y), s(s) {}
}; std::queue <node> q; inline int read()
{
int x = , f = ;
char ch = getchar();
for(; !isdigit(ch); ch = getchar()) if(ch == '-') f = -;
for(; isdigit(ch); ch = getchar()) x = (x << ) + (x << ) + ch - '';
return x * f;
} inline bool Acc(int x1, int y1, int x2, int y2, int s)
{
int need_key = map[x1][y1][x2][y2];
if(!need_key) return ;
if(need_key == -) return ;
return (s >> need_key - ) & ;
} inline void spfa()
{
node now;
int i, s, x, y;
memset(dis, / , sizeof(dis));
dis[][][] = ;
q.push(node(, , ));
while(!q.empty())
{
now = q.front();
q.pop();
vis[now.x][now.y][now.s] = ;
for(i = ; i < ; i++)
{
x = now.x + dx[i];
y = now.y + dy[i];
s = now.s | key[x][y];
if(!x || x > n || !y || y > m) continue;
if(Acc(now.x, now.y, x, y, now.s))
if(dis[x][y][s] > dis[now.x][now.y][now.s] + )
{
dis[x][y][s] = dis[now.x][now.y][now.s] + ;
if(!vis[x][y][s])
{
vis[x][y][s] = ;
q.push(node(x, y, s));
}
}
}
}
} int main()
{
int i, j, a, b, c, d, x, y, z, doors, keys;
n = read();
m = read();
p = read();
doors = read();
memset(map, -, sizeof(map));
for(i = ; i <= doors; i++)
{
a = read();
b = read();
c = read();
d = read();
map[a][b][c][d] = map[c][d][a][b] = read();
}
keys = read();
for(i = ; i <= keys; i++)
{
x = read();
y = read();
z = read();
key[x][y] |= << z - ;
}
spfa();
for(i = ; i < ( << ); i++) ans = min(ans, dis[n][m][i]);
if(ans == ) ans = -;
printf("%d\n", ans);
return ;
}

[CODEVS1911] 孤岛营救问题(分层图最短路)的更多相关文章

  1. luogu4011 孤岛营救问题 分层图

    关键词:分层图 状态压缩 最短路径 分层图:现在要求从起点到终点的最优路线,但受到手里拿着哪些钥匙的影响,最优路线不单纯了.因此,决定一个节点.一条边的存在的数中应当增加一个手中拿有钥匙的状态.这样就 ...

  2. 【网络流24题】 No.14 孤岛营救问题 (分层图最短路)

    [题意] 1944 年,特种兵麦克接到国防部的命令,要求立即赶赴太平洋上的一个孤岛, 营救被敌军俘虏的大兵瑞恩. 瑞恩被关押在一个迷宫里, 迷宫地形复杂, 但幸好麦克得到了迷宫的地形图. 迷宫的外形是 ...

  3. poj3635Full Tank?[分层图最短路]

    Full Tank? Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7248   Accepted: 2338 Descri ...

  4. HDU 5669 线段树优化建图+分层图最短路

    用线段树维护建图,即把用线段树把每个区间都标号了,Tree1中子节点有到达父节点的单向边,Tree2中父节点有到达子节点的单向边. 每次将源插入Tree1,汇插入Tree2,中间用临时节点相连.那么T ...

  5. BZOJ 2763 分层图最短路

    突然发现我不会分层图最短路,写一发. 就是同层中用双向边相连,用单向边连下一层 #include <cstdio> #include <algorithm> #include ...

  6. 【网络流24题】 No.15 汽车加油行驶问题 (分层图最短路i)

    [题意] 问题描述:给定一个 N*N 的方形网格,设其左上角为起点◎, 坐标为( 1, 1), X 轴向右为正, Y轴向下为正, 每个方格边长为 1, 如图所示. 一辆汽车从起点◎出发驶向右下角终点▲ ...

  7. BZOJ_2662_[BeiJing wc2012]冻结_分层图最短路

    BZOJ_2662_[BeiJing wc2012]冻结_分层图最短路 Description “我要成为魔法少女!”     “那么,以灵魂为代价,你希望得到什么?” “我要将有关魔法和奇迹的一切, ...

  8. BZOJ_1579_[Usaco2009 Feb]Revamping Trails 道路升级_分层图最短路

    BZOJ_1579_[Usaco2009 Feb]Revamping Trails 道路升级_分层图最短路 Description 每天,农夫John需要经过一些道路去检查牛棚N里面的牛. 农场上有M ...

  9. Nowcoder contest 370H Rinne Loves Dynamic Graph【分层图最短路】

    <题目链接> 题目大意:Rinne 学到了一个新的奇妙的东西叫做动态图,这里的动态图的定义是边权可以随着操作而变动的图.当我们在这个图上经过一条边的时候,这个图上所有边的边权都会发生变动. ...

  10. ACM-ICPC 2018 南京赛区网络预赛 L 【分层图最短路】

    <题目链接> 题目大意: 有N个城市,这些城市之间有M条有向边,每条边有权值,能够选择K条边 边权置为0,求1到N的最短距离. 解题分析: 分层图最短路模板题,将该图看成 K+1 层图,然 ...

随机推荐

  1. CF Gym 100463B Music Mess (思路)

    好题,当时想了半个小时,我往图论方面去想了,把出现过的字符串当场点,然后相互连边,那么就构成了一个三角形,一个大于三个点的连通分量里有以下结论:度为二的点可能是track,度为大于二的点一定不是tra ...

  2. ios常见错误之 Failed to instantiate the default view controller for UIMainStoryboardFile 'Main' - perhaps the designated entry point is not set?

    Failed to instantiate the default view controller for UIMainStoryboardFile 'Main' - perhaps the desi ...

  3. Python-OpenCV中图像合并显示

    在图像处理中,我们通常需要将原图像与处理后的图像放在同一个窗口显示,这样便于比较. 首先,需要介绍Numpy中的两个函数:hstack().vstack(). 函数原型:hstack(tup) ,参数 ...

  4. Memcached笔记之分布式算法

    1.根据余数进行分散:离散度高,但是增加或者移除服务器的时候,缓存充足的代价非常大.添加服务器后,余数就会产生巨变,这样就无法获取与保存时相同的服务器,从而音像缓存的命中率. 2.Consistent ...

  5. 使用Timer组件制作计时器

    实现效果: 知识运用: Timer组件的interval属性 //获取或设置Timer组件Tick事件发生的时间间隔 public int Interval {get;set} NumericUpDo ...

  6. CRF条件随机场简介<转>

    转自http://hi.baidu.com/hehehehello/item/3b0d1f8ba1c2e5c698255f89 CRF(Conditional Random Field) 条件随机场是 ...

  7. Cross-Entropy Loss 与Accuracy的数值关系(很重要,很好的博客)

    http://www.cnblogs.com/dengdan890730/p/6132937.html

  8. laydate时间控件绑定回调事件

    onclick="laydate({istime: true, format: 'YYYY-MM-DD',choose:checkDate});" //回调函数内容 functio ...

  9. APP上线碰到的问题:Non-public API usage

    ①.Non-public API usage:The app references non-public symbols in XXXX: _UICreateCGImageFromIOSurface ...

  10. 119. Pascal's Triangle II@python

    Given a non-negative index k where k ≤ 33, return the kth index row of the Pascal's triangle. Note t ...