题目链接 \(Click\) \(Here\)

分层图。。好长时间没写差点要忘了\(hhhhh\),其实思路还是很明了的。

注意需要强制消费。

#include <bits/stdc++.h>
using namespace std; const int N = 110010;
const int M = 550010;
#define int long long int n, k, A, B, C, have, val[110][110]; int node (int x, int y, int f) {
return (f - 1) * n * n + (x - 1) * n + y;
} bool in_map (int x, int y) {
return 1 <= x && x <= n && 1 <= y && y <= n;
} int cnt, head[N]; struct edge {
int nxt, to, w; edge (int _nxt = 0, int _to = 0, int _w = 0) {
nxt = _nxt, to = _to, w = _w;
}
}e[M]; void add_edge (int u, int v, int w) {
e[++cnt] = edge (head[u], v, w); head[u] = cnt;
} int dis[N]; struct Node {
int pos, dis;
bool operator < (Node rhs) const {return dis > rhs.dis;}
Node (int _pos = 0, int _dis = 0) {pos = _pos, dis = _dis;}
}; priority_queue <Node> q; int dijkstra (int s, int t) {
memset (dis, 0x3f, sizeof (dis));
dis[s] = 0; q.push (Node (s, 0));
while (!q.empty ()) {
Node u = q.top (); q.pop ();
if (dis[u.pos] < u.dis) continue;
for (int i = head[u.pos]; i; i = e[i].nxt) {
int v = e[i].to;
if (dis[v] > dis[u.pos] + e[i].w) {
dis[v] = dis[u.pos] + e[i].w;
q.push (Node (v, dis[v]));
}
}
}
return dis[t];
} signed main () {
cin >> n >> k >> A >> B >> C;
for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= n; ++j) {
cin >> have;
val[i][j] = have ? A : A + C;
}
}
int s = node (n, n, k + 1) + 1;
int t = node (n, n, k + 1) + 2;
add_edge (s, node (1, 1, 1), 0);
for (int i = 1; i <= k + 1; ++i) {
for (int x = 1; x <= n; ++x) {
for (int y = 1; y <= n; ++y) {
if (i <= k) {
if (i == 1 || val[x][y] != A) {
if (in_map (x + 1, y + 0)) add_edge (node (x, y, i), node (x + 1, y + 0, i + 1), 0);
if (in_map (x + 0, y + 1)) add_edge (node (x, y, i), node (x + 0, y + 1, i + 1), 0);
if (in_map (x - 1, y - 0)) add_edge (node (x, y, i), node (x - 1, y - 0, i + 1), B);
if (in_map (x - 0, y - 1)) add_edge (node (x, y, i), node (x - 0, y - 1, i + 1), B);
}
}
add_edge (node (x, y, i), node (x, y, 1), val[x][y]);
}
}
add_edge (node (n, n, i), t, 0);
}
cout << dijkstra (s, t) << endl;
}

Luogu P4009 汽车加油行驶问题的更多相关文章

  1. P4009 汽车加油行驶问题

    P4009 汽车加油行驶问题 最短路 清一色的spfa....送上一个堆优化Dijkstra吧(貌似代码还挺短) 顺便说一句,堆优化Dj跑分层图灰常好写 #include<iostream> ...

  2. 洛谷 P4009 汽车加油行驶问题 解题报告

    P4009 汽车加油行驶问题 题目描述 给定一个\(N×N\)的方形网格,设其左上角为起点◎,坐标(1,1) ,\(X\)轴向右为正,\(Y\)轴向下为正,每个方格边长为1 ,如图所示. 一辆汽车从起 ...

  3. 洛谷P4009 汽车加油行驶问题

    题目描述 给定一个 N \times NN×N 的方形网格,设其左上角为起点◎,坐标(1,1)(1,1),XX 轴向右为正, YY 轴向下为正,每个方格边长为 11 ,如图所示. 一辆汽车从起点◎出发 ...

  4. 洛谷P4009汽车加油行驶问题——网络流24题(最短路)

    题目:https://www.luogu.org/problemnew/show/P4009 网络流24题中不是网络流的最短路题: 把每个点拆成各个油量上的点,根据要求连边即可: 注意:点数最大为10 ...

  5. 洛谷P4009 汽车加油行驶问题(分层最短路)

    传送门 说好的网络流24题呢……上次是状压dp,这次怎么又最短路了…… 不过倒是用这题好好学了一下分层图最短路 把每一个位置$(x,y)$,油量剩余$k$表示为一个状态,然后转化成一个$n$进制数,这 ...

  6. 洛谷 P4009 汽车加油行驶问题 【最小费用最大流】

    分层图,建k层,设(i,j,0)为点(i,j)的满油状态,全图的流量都是1,因为重复走到一个点没有意义.如果当前点是加油站,那么它向它上左的点连费用为a的边,向下右连费用为a+b的边: 否则,这个点的 ...

  7. 【题解】【网络流24题】汽车加油行驶问题 [P4009] [Loj6223]

    [题解][网络流24题]汽车加油行驶问题 [P4009] [Loj6223] 传送门:汽车加油行驶问题 \([P4009]\) \([Loj6223]\) [题目描述] 给出一个 \(N \times ...

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

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

  9. 【刷题】LOJ 6223 「网络流 24 题」汽车加油行驶问题

    题目描述 给定一个 \(\text{N}\times \text{N}\) 的方形网格,设其左上角为起点◎,坐标为 \(\text{(1,1)}\) ,\(\text{X}\) 轴向右为正, \(\t ...

随机推荐

  1. 转载 -- jquery easyui datagrid 动态表头 + 嵌套对象属性展示

    代码功能: 1.datagrid 的表头由后台生成,可以配置在数据库 2.datagrid 的列绑定数据 支撑嵌套对象 $(function() { var columns = new Array() ...

  2. How to install Lion on PC

    open 'InstallESD.dmg' open '/Volumes/Mac OS X Install ESD/BaseSystem.dmg' rm '/Volumes/Mac OS X Base ...

  3. matlab颜色映射colormap() pcolor()

    http://blog.csdn.net/qq_20823641/article/details/51711618

  4. hdu-5687(字典树)

    题意:中文题: 解题思路:增加和查询就不说了,标准操作,就是删除操作:删除操作的时候,我们把给定字符串先在字典树中遍历一遍,然后算出这个字符串最后一个字符的出现次数,然后在遍历一遍,每个节点都减去这个 ...

  5. Spring MVC 使用介绍(三)—— Controller接口控制器

    一.概述 Controller接口类图如下,其中,BaseCommandController已从Spring 4移除 基于继承Controller接口的方式已经不推荐使用,仅供学习参考 二.基于Con ...

  6. P1164 小A点菜

    原题链接 https://www.luogu.org/problemnew/show/P1164 此题是一道简单的动规问题 才学两天不是很熟练,我苦思冥想看着题解终于想出来了. 主要的思路如下: 我们 ...

  7. alter table,复制, 单表查询

    修改表 语法:1. 修改表名      ALTER TABLE 表名                           RENAME 新表名; 2. 增加字段      ALTER TABLE 表名 ...

  8. 【XSY2111】Chef and Churus 分块 树状数组

    题目描述 有一个长度为\(n\)的数组\(A\)和\(n\)个区间\([l_i,r_i]\),有\(q\)次操作: \(1~x~y\):把\(a_x\)改成\(y\) \(2~x~y\):求第\(l\ ...

  9. SVG图片如何调整大小和颜色

    设计妹子给了SVG图片,在开发的时候尺寸不对,颜色也要修改,应当如何解决? 1.修改大小:在<svg> 标签中修改width.height 属性(默认单位是px)2.修改颜色:在<p ...

  10. [NOIp2012] 国王游戏(排序 + 贪心 + 高精度)

    题意 给你两个长为 \(n+1\) 的数组 \(a,b\) ,你需要定义一个顺序 \(p\) (\(p_0\) 永远为 \(0\)) 能够最小化 \[ \max_{i=1}^{n} \frac{\pr ...