BZOJ2143: 飞飞侠
2143: 飞飞侠
题意:
给出两个 n ∗ m 的矩阵 A,B,以及 3 个人的坐标
在 (i, j) 支付 Ai,j 的费用可以弹射到曼哈顿距离不超过 Bi,j 的位置
问三个人汇合所需要的最小总费用
其中 0 < n,m < 150,0 < A < 1000,0 < B < 10^9。
分层图最短路也好,用dijkstra/spfa做dp也罢,反正就是辣么一回事!
把距离等效成油量之类的东西,单独拿出一维
\(f_{i,j,k}\)表示从起点到(i,j),剩下k个油的最小费用
枚举移动和换油(不是加油)来转移
因为不是dag所以用dijkstra来做
spfa应该也可以,不过这是个满足最短路性质的dp没必要用spfa
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
using namespace std;
typedef long long ll;
#define pii pair<int, int>
#define fir first
#define sec second
const int N = 152;
int n, m, a[N][N], b[N][N];
struct meow {
int x, y, c, d;
bool operator < (const meow &r) const {
return d > r.d;
}
meow(int a1=0, int a2=0, int a3=0, int a4=0):x(a1), y(a2), c(a3), d(a4){}
} ;
meow a1, a2, a3;
inline bool valid(int x, int y) {
return 1<=x && x<=n && 1<=y && y<=m;
}
int d[N][N][305];
bool vis[N][N][305];
priority_queue<meow> q;
int dx[10] = {1, -1, 0, 0}, dy[10] = {0, 0, 1, -1};
int ans[5][5];
int flag[N][N];
void dij(meow s) {
memset(flag, 0, sizeof(flag));
memset(d, 127, sizeof(d));
memset(vis, 0, sizeof(vis));
int x = s.x, y = s.y;
d[x][y][0] = 0;
q.push(meow(x, y, 0, 0));
while(!q.empty()) {
//if(vis[a2.x][a2.y][0] && vis[a3.x][a3.y][0] && vis[a1.x][a1.y][0]) break;
if(flag[a1.x][a1.y] && flag[a2.x][a2.y] && flag[a3.x][a3.y]) break;
meow u = q.top(); q.pop();
int x = u.x, y = u.y, c = u.c;
if(vis[x][y][c]) continue;
vis[x][y][c] = 1;
flag[x][y] = 1;
int &t = d[x][y][b[x][y]];
if(t > d[x][y][c] + a[x][y] && b[x][y] > c) {
t = d[x][y][c] + a[x][y];
q.push(meow(x, y, b[x][y], t));
}
if(c) for(int i=0; i<5; i++) {
int tx = x + dx[i], ty = y + dy[i];
if(!valid(tx, ty)) continue;
int &t = d[tx][ty][c-1];
if(t > d[x][y][c]) {
t = d[x][y][c];
if(!vis[tx][ty][c-1]) q.push(meow(tx, ty, c-1, t));
}
} /*else {
int &t = d[x][y][b[x][y]];
if(t > d[x][y][c] + a[x][y]) {
t = d[x][y][c] + a[x][y];
q.push(meow(x, y, b[x][y], t));
}
}
*/
}
while(!q.empty()) q.pop();
}
int main() {
freopen("in", "r", stdin);
ios::sync_with_stdio(false); cin.tie(); cout.tie();
cin >> n >> m;
for(int i=1; i<=n; i++) for(int j=1; j<=m; j++) cin >> b[i][j], b[i][j] = min(b[i][j], 300);
for(int i=1; i<=n; i++) for(int j=1; j<=m; j++) cin >> a[i][j];
cin >> a1.x >> a1.y >> a2.x >> a2.y >> a3.x >> a3.y;
memset(ans, 127, sizeof(ans));
dij(a1);
for(int k=0; k<=300; k++) {
ans[1][2] = min(ans[1][2], d[a2.x][a2.y][k]);
ans[1][3] = min(ans[1][3], d[a3.x][a3.y][k]);
}
//cout << ans[1][2];
//return 0;
dij(a2);
for(int k=0; k<=300; k++) {
ans[2][1] = min(ans[2][1], d[a1.x][a1.y][k]);
ans[2][3] = min(ans[2][3], d[a3.x][a3.y][k]);
}
dij(a3);
for(int k=0; k<=300; k++) {
ans[3][2] = min(ans[3][2], d[a2.x][a2.y][k]);
ans[3][1] = min(ans[3][1], d[a1.x][a1.y][k]);
}
ll ans1 = (ll)ans[2][1] + ans[3][1], ans2 = (ll)ans[1][2] + ans[3][2], ans3 = (ll)ans[1][3] + ans[2][3];
//printf("look %d %d %d\n", ans1, ans2, ans3);
if(ans1 > 1e9 && ans2 > 1e9 && ans3 > 1e9) cout << "NO";
else if(ans1 <= ans2 && ans1 <= ans3) cout << 'X' << endl << ans1;
else if(ans2 <= ans1 && ans2 <= ans3) cout << 'Y' << endl << ans2;
else if(ans3 <= ans1 && ans3 <= ans2) cout << 'Z' << endl << ans3;
}
BZOJ2143: 飞飞侠的更多相关文章
- BZOJ2143 飞飞侠 & [校内NOIP2018模拟20181026] 最强大脑
Time Limit: 50 Sec Memory Limit: 259 MB Description 飞飞国是一个传说中的国度,国家的居民叫做飞飞侠.飞飞国是一个N×M的矩形方阵,每个格子代表一个街 ...
- 2018.11.05 bzoj2143: 飞飞侠(最短路)
传送门 最短路好题. 考虑对每个二维坐标建立一个高度属性. 这样每次如果在点(i,j,0)(i,j,0)(i,j,0)只能选择花费bi,jb_{i,j}bi,j跳向(i,j,ai,j)(i,j,a_ ...
- bzoj千题计划225:bzoj2143: 飞飞侠
http://www.lydsy.com/JudgeOnline/problem.php?id=2143 分层图最短路 把能够弹跳的曼哈顿距离看做能量 dp[i][j][k]表示在(i,j)位置,还有 ...
- [BZOJ2143]飞飞侠 并查集优化最短路
链接 题解 首先很容易想到对每个点暴力跑Dijkstra,但是这样边数是 \(N^4\) 的,考虑优化 发现每次松弛的时候,都要把整个地图扫一遍,每个节点都要重复扫很多次,如果我们在一个点不会再被更新 ...
- 刷题总结——飞飞侠(bzoj2143 最短路)
题目: Description 飞飞国是一个传说中的国度,国家的居民叫做飞飞侠.飞飞国是一个N×M的矩形方阵,每个格子代表一个街区.然而飞飞国是没有交通工具的.飞飞侠完全靠地面的弹射装置来移动.每个街 ...
- 【BZOJ 2143】 飞飞侠
Description 飞飞国是一个传说中的国度,国家的居民叫做飞飞侠.飞飞国是一个N×M的矩形方阵,每个格子代表一个街区.然而飞飞国是没有交通工具的.飞飞侠完全靠地面的弹射装置来移动.每个街区都装有 ...
- BZOJ 2143 飞飞侠(分层最短路)
飞飞国是一个N×M的矩形方阵,每个格子代表一个街区.然而飞飞国是没有交通工具的.飞飞侠完全靠地面的弹射装置来移动.每个街区都装有弹射装置.使用弹射装置是需要支付一定费用的.而且每个弹射装置都有自己的弹 ...
- luogu4473 BZOJ2143 2011[国家集训队]飞飞侠
题目戳这里 有问题可以在博客@ 应该还会有人来看吧,嘻嘻 正题: 题目大意: 题目很清楚,就是一个点有一定的范围,会有一定的花费 求三个点中的任意两个点到另一个点的最小花费 (麻麻教育我千万读好题目( ...
- bzoj 2143: 飞飞侠
#include<cstdio> #include<iostream> #include<queue> #define inf 1000000000 #define ...
随机推荐
- fastjson存在乱序的问题
现象及原因 通常来讲,在使用json数据格式时一般不需要要求数据有序.但凡事都有例外,针对查询时序数据这样一个场景,就必须要求服务器端返回的数据是按时间有序的,否则前端在进行数据展示时就会有问题. 项 ...
- ElasticSearch query_string vs multi_match cross_fields query
ElasticSearch query_string vs multi_match cross_fields query 本文记录以字段为中心的查询和以词为中心的查询这两种查询方式的区别以及在Elas ...
- centos6 升级pip后导致pip不可用
问题:公司内部一台服务器在用pip安装python某个模块的时候提示pip需要升级,然后我就手贱升级了一下,结果悲催了,再次执行pip命令时报错如下: Google了下错误,说是: CENTOS/RH ...
- iptables 防火墙日常
. 检查机目标机器 httpd 服务/etc/init.d/httpd status ========================================================= ...
- Mail.Ru Cup 2018 Round 3 B. Divide Candies
题目链接 分析一下题意可以得到题目要求的是满足下面这个 公式的不同的i,ji,ji,j的方案数; 即(i2+j2)mod   m=0 (n ≤ ...
- mysql5.x安装脚本
直接贴出来: #!/bin/bash #linux安装mysql服务分两种安装方法: #①源码安装,优点是安装包比较小,只有十多M,缺点是安装依赖的库多,安装编译时间长,安装步骤复杂容易出错: #②使 ...
- vo类,model类,dto类的作用及划分
1.entity里的每一个字段,与数据库相对应, 2.dto里的每一个字段,是和你前台页面相对应, 3.VO,这是用来转换从entity到dto,或者从dto到entity的中间的东西. 举个例子 ...
- 一个简单的go语言爬虫
package main import ( "bufio" "fmt" "golang.org/x/net/html/charset" &q ...
- java 生产者消费者简单实现demo
第一种方式 使用BlockingQueue 阻塞队列 public class Threads { public static void main(String[] args) { final Arr ...
- SQL语句case when then的用法
case具有两种格式.简单case函数和case搜索函数. //简单case函数 case sex when '1' then '男' when '2' then '女’ else '其他' end ...