Description
Solution
用dis[u][t] d i s [ u ] [ t ] 表示位于节点u u ,剩余的油还可以走tt步,跑最短路即可。
Code
/**************************************
* Au: Hany01
* Prob: [LOJ6223][网络流24题]汽车加油行驶问题
* Date: Jul 17th, 2018
**************************************/
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef pair<int, int> PII;
typedef vector<int> VI;
#define File(a) freopen(a".in", "r", stdin), freopen(a".out", "w", stdout)
#define rep(i, j) for (register int i = 0, i##_end_ = j; i < i##_end_; ++ i)
#define For(i, j ,k) for (register int i = (j), i##_end_ = (k); i <= i##_end_; ++ i)
#define Fordown(i, j, k) for (register int i = (j), i##_end_ = (k); i >= i##_end_; -- i)
#define Set(a, b) memset(a, b, sizeof(a))
#define Cpy(a, b) memcpy(a, b, sizeof(a))
#define SZ(a) ((int)(a.size()))
#define ALL(a) a.begin(), a.end()
#define pb(a) push_back(a)
#define mp(a, b) make_pair(a, b)
#define x first
#define y second
#define INF (0x3f3f3f3f)
#define INF1 (2139062143)
#define y1 wozenmezhemecaia
#ifdef hany01
#define debug(...) fprintf(stderr, __VA_ARGS__)
#else
#define debug(...)
#endif
template<typename T> inline bool chkmax(T &a, T b) { return a < b ? a = b, 1 : 0; }
template<typename T> inline bool chkmin(T &a, T b) { return b < a ? a = b, 1 : 0; }
inline int read() {
register char c_; register int _, __;
for (_ = 0, __ = 1, c_ = getchar(); !isdigit(c_); c_ = getchar()) if (c_ == '-') __ = -1;
for ( ; isdigit(c_); c_ = getchar()) _ = (_ << 1) + (_ << 3) + (c_ ^ 48);
return _ * __;
}
const int maxn = 105 * 105, maxm = maxn << 2, maxk = 11;
int n, full, A, B, C, dis[maxn][maxk], isst[maxn], beg[maxn], v[maxm], nex[maxm], e, vis[maxn][maxk], w[maxm];
inline void add(int uu, int vv, int ww) { v[++ e] = vv, w[e] = ww, nex[e] = beg[uu], beg[uu] = e; }
#define idx(i, j) (((i) - 1) * n + j)
inline int SPFA()
{
static queue<PII> q;
Set(dis, 63), dis[1][full] = 0, q.push(mp(1, full));
while (!q.empty()) {
register int u = q.front().x, res = q.front().y; q.pop(), vis[u][res] = 0;
if (res) for (register int i = beg[u]; i; i = nex[i]) {
int d = dis[u][res] + w[i], gas = res - 1;
if (isst[v[i]]) gas = full, d += A;
if (chkmin(dis[v[i]][gas], d) && !vis[v[i]][gas])
vis[v[i]][gas] = 1, q.push(mp(v[i], gas));
}
if (chkmin(dis[u][full], dis[u][res] + A + C) && !vis[u][full])
vis[u][full] = 1, q.push(mp(u, full));
}
int Ans = INF;
For(i, 0, full) chkmin(Ans, dis[n * n][i]);
return Ans;
}
int main()
{
#ifdef hany01
File("loj6223");
#endif
n = read(), full = read(), A = read(), B = read(), C = read();
For(i, 1, n) For(j, 1, n) {
isst[idx(i, j)] = read();
if (i > 1) add(idx(i, j), idx(i - 1, j), B);
if (i < n) add(idx(i, j), idx(i + 1, j), 0);
if (j > 1) add(idx(i, j), idx(i, j - 1), B);
if (j < n) add(idx(i, j), idx(i, j + 1), 0);
}
printf("%d\n", SPFA());
return 0;
}