题目链接 \(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. Eclipse报错:An internal error has occurred. Widget is disposed

    win10家庭版报错. 右键Eclipse的快捷方式,在兼容性窗口的兼容模式中,将“以兼容模式运行这个程序”选项打对勾.选择win8就可以解决问题.

  2. DBC格式解析(数据部分)

    dbc格式说明:DBC Format   实战: 我们先来看一段数据 BO_ VOLTAGE01: BMS2 SG_ V01 : |@+ () [|] "" Vector__XXX ...

  3. Create an Azure SQL database in the Azure portal

    Create a SQL database An Azure SQL database is created with a defined set of compute and storage res ...

  4. Wiener Filter

    假设分别有两个WSS process:$x[n]$,$y[n]$,这两个process之间存在某种关系,并且我们也了解这种关系.现在我们手头上有process $x[n]$,目的是要设计一个LTI系统 ...

  5. docker之harbor仓库注意事项

    首先修改harbor的配置文件harbor.cfg hostname可以是ip也可以是主机名 修改docker/etc/docker/daemon.json 添加insecure-registries ...

  6. BZOJ2877 NOI2012魔幻棋盘(二维线段树)

    显然一个序列的gcd=gcd(其差分序列的gcd,序列中第一个数).于是一维情况直接线段树维护差分序列即可. 容易想到将该做法拓展到二维.于是考虑维护二维差分,查询时对差分矩阵求矩形的gcd,再对矩形 ...

  7. Aizu2130-Billion Million Thousand-dp

    用dp求出最大的表达,再用dp求出.//然而并没有想出来 #include <cstdio> #include <string> #include <algorithm& ...

  8. python中切片的理解

    Python中什么可以切片 l  Python中符合序列的有序序列都支持切片(slice) l  如:列表,字符,元祖 Python中切片的格式 l  格式:[start : end : step] ...

  9. 【BZOJ4944】【NOI2017】泳池 概率DP 常系数线性递推 特征多项式 多项式取模

    题目大意 有一个\(1001\times n\)的的网格,每个格子有\(q\)的概率是安全的,\(1-q\)的概率是危险的. 定义一个矩形是合法的当且仅当: 这个矩形中每个格子都是安全的 必须紧贴网格 ...

  10. 【CF472G】Design Tutorial 压位

    题目大意 给出两个\(01\)序列\(A\)和\(B\) 汉明距离定义为两个长度相同的序列中,有多少个对应位置上的数字不一样 \(00111\) 和 \(10101\)的距离为\(2\) \(Q\)次 ...