結果

問題 No.2328 Build Walls
ユーザー Kinoko_SokoraKinoko_Sokora
提出日時 2023-05-28 15:45:26
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 161 ms / 3,000 ms
コード長 4,680 bytes
コンパイル時間 1,110 ms
コンパイル使用メモリ 110,352 KB
実行使用メモリ 13,736 KB
最終ジャッジ日時 2023-08-27 12:52:32
合計ジャッジ時間 4,569 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 1 ms
4,380 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 1 ms
4,380 KB
testcase_10 AC 2 ms
4,380 KB
testcase_11 AC 2 ms
4,380 KB
testcase_12 AC 2 ms
4,376 KB
testcase_13 AC 34 ms
8,764 KB
testcase_14 AC 42 ms
6,340 KB
testcase_15 AC 34 ms
6,164 KB
testcase_16 AC 7 ms
4,380 KB
testcase_17 AC 36 ms
6,728 KB
testcase_18 AC 3 ms
4,380 KB
testcase_19 AC 6 ms
4,376 KB
testcase_20 AC 4 ms
4,380 KB
testcase_21 AC 29 ms
8,312 KB
testcase_22 AC 65 ms
7,456 KB
testcase_23 AC 134 ms
13,376 KB
testcase_24 AC 128 ms
13,376 KB
testcase_25 AC 131 ms
13,396 KB
testcase_26 AC 110 ms
13,172 KB
testcase_27 AC 122 ms
13,444 KB
testcase_28 AC 56 ms
13,236 KB
testcase_29 AC 132 ms
13,348 KB
testcase_30 AC 62 ms
13,644 KB
testcase_31 AC 61 ms
13,480 KB
testcase_32 AC 121 ms
13,396 KB
testcase_33 AC 161 ms
13,104 KB
testcase_34 AC 63 ms
13,736 KB
testcase_35 AC 144 ms
13,236 KB
testcase_36 AC 2 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<cstdio>
#include<string>
#include<cmath>
#include<algorithm>
#include<map>
#include<vector>
#include<stack>
#include<iomanip>
#include<queue>
#include<set>
#include<functional>
#include<tuple>
#include<bitset>
#include<cassert>
#include<cstdint>
#include<complex>
using namespace std;
bool printb(bool f) {
	if (f)printf("Yes\n");
	else printf("No\n");
	return f;
}
template<class T>
void prt(T t, string sep = "\n") { cout << t << sep; return; }
template<class T>
void printl(vector<T> a, string sep = " ") {
	for (int i = 0; i < a.size(); i++) {
		cout << a[i] << sep;
	}
	cout << "\n";
	return;
}
#define all(a) a.begin(),a.end()
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
using llong = long long;
using pii = pair<int, int>;
using pll = pair<llong, llong>;
using pli = pair<llong, int>;
using pil = pair<int, llong>;
template<typename T> using vec2 = vector<vector<T>>;
template<typename T> inline bool chmin(T& a, T b) { return (a > b) ? (a = b, true) : false; }
template<typename T> inline bool chmax(T& a, T b) { return (a < b) ? (a = b, true) : false; }
bool bitIn(llong a, int b) { return ((a >> b) & 1); }
int bitCnt(llong a) {
	int re = 0;
	while (a > 0) {
		if (a & 1)re++;
		a >>= 1;
	}
	return re;
}
llong powL(llong n, llong i) {
	llong re = 1;
	while (i >= 1) {
		if (i & 1) re *= n;
		n *= n;
		i >>= 1;
	}
	return re;
}
llong powL_M(llong n, llong i, llong m) {
	llong re = 1;
	while (i >= 1) {
		if (i & 1) {
			re *= n;
			re %= m;
		}
		n *= n;
		n %= m;
		i >>= 1;
	}
	return re;
}
struct point {
	llong x = 0, y = 0;
};
//lからrまでの和を返す
template<typename T>
T sum_num(T l, T r) {
	if (((l + r) & 1) == 0) {
		return (l + r) / 2 * (l - r + 1);
	}
	else {
		return (l - r + 1) / 2 * (l + r);
	}
}


//int  dx[4] = { 0,1,0,-1 }, dy[4] = { 1,0,-1,0 };

struct edge {
	int to, co;
};
static const double pi = 3.14159265358979;

/*
modintクラス。四則演算と累乗が定義されている。
割り算はmodが素数でない時にも使える。(逆元の存在条件注意)
extGCD(),GCD()を含む
*/
/*
template <class T>
T extGCD(T a, T b, T& x, T& y) {
	if (b == 0) {
		x = 1;
		y = 0;
		return a;
	}
	T gcd = extGCD(b, a % b, y, x);
	y -= a / b * x;
	return gcd;
}

template <class T>
T GCD(T a, T b) {
	T x, y;
	return extGCD(a, b, x, y);
}


static const int mod = 1e9 + 7; //問題文に合わせて変更すること
class modint {
public:
	long long x;
	modint(long long x = 0) :x((x% mod + mod) % mod) {}
	modint operator-() const {
		return (-x);
	}
	modint& operator+=(const modint& a) {
		if ((x += a.x) >= mod)x -= mod;
		return *this;
	}
	modint& operator-=(const modint& a) {
		if ((x += mod - a.x) >= mod) x -= mod;
		return *this;
	}
	modint& operator*=(const modint& a) {
		(x *= a.x) %= mod;
		return *this;
	}
	modint operator+(const modint& a) const {
		modint res(*this);
		return res += a;
	}
	modint operator-(const modint& a) const {
		modint res(*this);
		return res -= a;
	}
	modint operator*(const modint& a) const {
		modint res(*this);
		return res *= a;
	}
	modint inv() const {
		long long y, c;
		extGCD(x, (long long)mod, y, c);
		return y;
	}
	modint& operator/=(const modint& a) {
		return (*this) *= a.inv();
	}
	modint operator/ (const modint& a) const {
		modint res(*this);
		return res /= a;
	}
	friend ostream& operator<<(ostream& os, const modint& a) {
		os << a.x;
		return os;
	}
};
//pow(a , n) modint型aのn乗のmodを求める
template<class T>
modint powM(modint a, T n) {
	modint re(1);
	while (n > 0) {
		if (n & 1)re *= a;
		a *= a;
		n >>= 1;
	}
	return re;
}
//*/



int main() {
	int h, w;
	cin >> h >> w;
	vec2<llong> co(h - 2, vector<llong>(w + 2));
	rep(i, h - 2) {
		co[i][0] = 0;
		rep(j, w) {
			scanf("%lld", &co[i][j + 1]);
			if (co[i][j + 1] == -1)co[i][j + 1] = 1e18;
		}
		co[i][w + 1] = 0;
	}
	vec2<llong> dis(h - 2, vector<llong>(w + 2, 1e18));
	dis[0][0] = 0;
	using T = tuple<llong, int, int>;
	priority_queue<T, vector<T>, greater<T>> q;
	q.emplace(0, 0, 0);
	int dx[8] = { -1,-1,0,1,1,1,0,-1 }, dy[8] = { 0,-1,-1,-1,0,1,1,1 };
	while (!q.empty()) {
		auto i = q.top();
		q.pop();
		auto d = get<0>(i);
		auto x = get<1>(i), y = get<2>(i);
		if (dis[x][y] < d)continue;
		//cout << x << " " << y << " " << d << "\n";
		rep(k, 8) {
			int nx = x + dx[k], ny = y + dy[k];
			if (nx < 0 || ny < 0)continue;
			if (nx >= h - 2 || ny >= w + 2)continue;
			//cout << " " << nx << " " << ny << "\n";
			if (chmin(dis[nx][ny], d + co[nx][ny])) {
				q.emplace(d + co[nx][ny], nx, ny);
			}
		}

	}
	//for (auto i : dis)printl(i);
	if (dis[h - 3][w + 1] == 1e18)prt(-1);
	else prt(dis[h - 3][w + 1]);
}
0