답안 #684341

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
684341 2023-01-20T23:32:01 Z ethening Soccer (JOI17_soccer) C++17
100 / 100
314 ms 89588 KB
#include "bits/stdc++.h"
using namespace std;
using ll = long long;
using pii = pair<int, int>;

// pii pos[100005];
int close[505][505];
// int dex[505][505][5];
vector<pair<int, ll>> E[753005];
ll ans[753005];

bool vis[753005];

queue<pii> Q;
priority_queue<pair<ll, int>, vector<pair<ll, int>>, greater<pair<ll, int>>> PQ;

int dex(int x, int y, int z) {
	return (x * 501 + y) * 3 + z;
}

int main() {
	cin.tie(0)->sync_with_stdio(0);
	int h, w;
	cin >> h >> w;
	ll A, B, C;
	cin >> A >> B >> C;
	int n;
	cin >> n;
	memset(close, -1, sizeof(close));
	
	int st, ed;

	for (int i = 0; i < n; i++) {
		int tmp, tmp2;
		cin >> tmp >> tmp2;
		if (i == 0) {
			st = dex(tmp, tmp2,  0);
		}
		if (i == n - 1) {
			ed = dex(tmp, tmp2, 0);
		}
		close[tmp][tmp2] = 0;
		Q.push(make_pair(tmp, tmp2));
	}

	while (!Q.empty()) {
		pii a = Q.front();
		Q.pop();
		if (a.first > 0 && close[a.first - 1][a.second] == -1) {
			close[a.first - 1][a.second] = close[a.first][a.second] + 1;
			Q.push(make_pair(a.first - 1, a.second));
 		}
		if (a.first < h && close[a.first + 1][a.second] == -1) {
			close[a.first + 1][a.second] = close[a.first][a.second] + 1;
			Q.push(make_pair(a.first + 1, a.second));
 		}
		if (a.second > 0 && close[a.first][a.second - 1] == -1) {
			close[a.first][a.second - 1] = close[a.first][a.second] + 1;
			Q.push(make_pair(a.first, a.second - 1));
 		}
		if (a.second < w && close[a.first][a.second + 1] == -1) {
			close[a.first][a.second + 1] = close[a.first][a.second] + 1;
			Q.push(make_pair(a.first, a.second + 1));
 		}
	}

	for (int i = 0; i <= h; i++) {
		for (int j = 0; j <= w; j++) {
			for (int k = 0; k < 3; k++) {
				ans[dex(i, j, k)] = (ll)1e18;
			}
		}
	}

	for (int i = 0; i <= h; i++) {
		for (int j = 0; j <= w; j++) {
			E[dex(i, j, 0)].reserve(6);
			E[dex(i, j, 1)].reserve(3);
			E[dex(i, j, 2)].reserve(3);
			E[dex(i, j, 0)].push_back(make_pair(dex(i, j, 1), B));
			E[dex(i, j, 0)].push_back(make_pair(dex(i, j, 2), B));
			
			E[dex(i, j, 1)].push_back(make_pair(dex(i, j, 0), close[i][j] * C));
			E[dex(i, j, 2)].push_back(make_pair(dex(i, j, 0), close[i][j] * C));
			if (i > 0) {
				E[dex(i, j, 0)].push_back(make_pair(dex(i - 1, j, 0), C));
				E[dex(i, j, 1)].push_back(make_pair(dex(i - 1, j, 1), A));
			}
			if (i < h) {
				E[dex(i, j, 0)].push_back(make_pair(dex(i + 1, j, 0), C));
				E[dex(i, j, 1)].push_back(make_pair(dex(i + 1, j, 1), A));
			}
			if (j > 0) {
				E[dex(i, j, 0)].push_back(make_pair(dex(i, j - 1, 0), C));
				E[dex(i, j, 2)].push_back(make_pair(dex(i, j - 1, 2), A));
			}
			if (j < w) {
				E[dex(i, j, 0)].push_back(make_pair(dex(i, j + 1, 0), C));
				E[dex(i, j,2)].push_back(make_pair(dex(i, j + 1, 2), A));
			}
		}
	}

	// for (int i = 1; i <= cnt; i++) {
	// 	cout << "!" << (i - 1) / 5 + 1 << " " << (i - 1) % 5 << endl;
	// 	for (auto [to, weight] : E[i]) {
	// 		cout << (to - 1) / 5 + 1 << " " << (to - 1) % 5 << " " << weight << endl;
	// 	}
	// }


	ans[st] = 0;
	// cout << st << " " << ed << "!!!" << endl;
	PQ.push(make_pair(0, st));
	while (!PQ.empty()) {
		const pair<ll, int> x = PQ.top();
		PQ.pop();
		if (ans[x.second] < x.first) continue;
		if (x.second == ed) break;
		if (vis[x.second]) continue;
		vis[x.second] = 1;
		for (const pair<int, ll>& y: E[x.second]) {
			if (ans[x.second] + y.second < ans[y.first] && !vis[y.first]) {
				ans[y.first] = ans[x.second] + y.second;
				PQ.push(make_pair(ans[y.first], y.first));
				// cout << x.second << " " << y.first << " " << ans[y.first] << endl;
			}
		}
	}

	cout << ans[ed] << endl;
}

Compilation message

soccer.cpp: In function 'int main()':
soccer.cpp:131:16: warning: 'ed' may be used uninitialized in this function [-Wmaybe-uninitialized]
  131 |  cout << ans[ed] << endl;
      |                ^
# 결과 실행 시간 메모리 Grader output
1 Correct 47 ms 37400 KB Output is correct
2 Correct 10 ms 19028 KB Output is correct
3 Correct 156 ms 88796 KB Output is correct
4 Correct 112 ms 86672 KB Output is correct
5 Correct 61 ms 44648 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 106 ms 86684 KB Output is correct
2 Correct 97 ms 85712 KB Output is correct
3 Correct 149 ms 75672 KB Output is correct
4 Correct 221 ms 88760 KB Output is correct
5 Correct 160 ms 76956 KB Output is correct
6 Correct 205 ms 88756 KB Output is correct
7 Correct 144 ms 88740 KB Output is correct
8 Correct 158 ms 88772 KB Output is correct
9 Correct 93 ms 85724 KB Output is correct
10 Correct 35 ms 32868 KB Output is correct
11 Correct 215 ms 88768 KB Output is correct
12 Correct 207 ms 88760 KB Output is correct
13 Correct 154 ms 75728 KB Output is correct
14 Correct 133 ms 88748 KB Output is correct
15 Correct 75 ms 73376 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 47 ms 37400 KB Output is correct
2 Correct 10 ms 19028 KB Output is correct
3 Correct 156 ms 88796 KB Output is correct
4 Correct 112 ms 86672 KB Output is correct
5 Correct 61 ms 44648 KB Output is correct
6 Correct 106 ms 86684 KB Output is correct
7 Correct 97 ms 85712 KB Output is correct
8 Correct 149 ms 75672 KB Output is correct
9 Correct 221 ms 88760 KB Output is correct
10 Correct 160 ms 76956 KB Output is correct
11 Correct 205 ms 88756 KB Output is correct
12 Correct 144 ms 88740 KB Output is correct
13 Correct 158 ms 88772 KB Output is correct
14 Correct 93 ms 85724 KB Output is correct
15 Correct 35 ms 32868 KB Output is correct
16 Correct 215 ms 88768 KB Output is correct
17 Correct 207 ms 88760 KB Output is correct
18 Correct 154 ms 75728 KB Output is correct
19 Correct 133 ms 88748 KB Output is correct
20 Correct 75 ms 73376 KB Output is correct
21 Correct 51 ms 44080 KB Output is correct
22 Correct 278 ms 88772 KB Output is correct
23 Correct 314 ms 80056 KB Output is correct
24 Correct 245 ms 86668 KB Output is correct
25 Correct 259 ms 88848 KB Output is correct
26 Correct 274 ms 88820 KB Output is correct
27 Correct 180 ms 85112 KB Output is correct
28 Correct 249 ms 85444 KB Output is correct
29 Correct 297 ms 87448 KB Output is correct
30 Correct 175 ms 85348 KB Output is correct
31 Correct 135 ms 86680 KB Output is correct
32 Correct 193 ms 89588 KB Output is correct
33 Correct 235 ms 88824 KB Output is correct
34 Correct 106 ms 85736 KB Output is correct
35 Correct 230 ms 85476 KB Output is correct