제출 #163416

#제출 시각아이디문제언어결과실행 시간메모리
163416iefnah06Soccer (JOI17_soccer)C++11
100 / 100
1189 ms154600 KiB
#include <bits/stdc++.h>
using namespace std;

using ll = long long;

const int MAXH = 510;
const int MAXS = MAXH * MAXH;
const int MAXV = 5 * MAXS;
const int MAXN = 100010;

const int INF = 1e6;
const ll DINF = 4e18;

int X, Y, N;
ll A, B, C;
int S, V;
int P[MAXN], Q[MAXN];

vector<int> vx[MAXH], vy[MAXH];

int inds[5][MAXH][MAXH];
int mincost[MAXH][MAXH];

const int mx[] = {1, 0, -1, 0};
const int my[] = {0, 1, 0, -1};

vector<pair<int, ll>> adj[MAXV];
ll dist[MAXV];

int main() {
	ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
	cin >> X >> Y; X++, Y++;
	cin >> A >> B >> C;
	S = X * Y;
	V = 5 * S;
	cin >> N;
	vector<pair<int, int>> s;
	for (int x = 0; x < X; x++) {
		for (int y = 0; y < Y; y++) {
			mincost[x][y] = INF;
		}
	}
	for (int i = 0; i < N; i++) {
		int x, y; cin >> x >> y;
		P[i] = x, Q[i] = y;
		if (mincost[x][y] == INF) {
			s.emplace_back(x, y);
			mincost[x][y] = 0;
		}
	}
	for (size_t i = 0; i < s.size(); i++) {
		int x, y; tie(x, y) = s[i];
		for (int dir = 0; dir < 4; dir++) {
			int nx = x + mx[dir], ny = y + my[dir];
			if (nx < 0 || nx >= X || ny < 0 || ny >= Y) continue;
			if (mincost[nx][ny] != INF) continue;
			mincost[nx][ny] = mincost[x][y] + 1;
			s.emplace_back(nx, ny);
		}
	}

	for (int cur = 0, i = 0; i < 5; i++) {
		for (int x = 0; x < X; x++) {
			for (int y = 0; y < Y; y++) {
				inds[i][x][y] = cur;
				cur++;
			}
		}
	}

	auto add_edge = [&](int a, int b, ll c) -> void {
		adj[a].emplace_back(b, c);
	};

	for (int x = 0; x < X; x++) {
		for (int y = 0; y < Y; y++) {
			for (int dir = 0; dir < 4; dir++) {
				int nx = x + mx[dir], ny = y + my[dir];
				if (nx < 0 || nx >= X || ny < 0 || ny >= Y) continue;
				add_edge(inds[4][x][y], inds[4][nx][ny], C);
			}
		}
	}

	for (int x = 0; x < X; x++) {
		for (int y = 0; y < Y; y++) {
			for (int dir = 0; dir < 4; dir++) {
				if (mincost[x][y] != INF) {
					add_edge(inds[dir][x][y], inds[4][x][y], C * mincost[x][y] + B);
				}
				int nx = x + mx[dir], ny = y + my[dir];
				if (nx < 0 || nx >= X || ny < 0 || ny >= Y) continue;
				add_edge(inds[4][x][y], inds[dir][nx][ny], A);
			}
		}
	}

	for (int x = 0; x < X; x++) {
		for (int y = 0; y < Y; y++) {
			for (int dir = 0; dir < 4; dir++) {
				int nx = x + mx[dir], ny = y + my[dir];
				if (nx < 0 || nx >= X || ny < 0 || ny >= Y) continue;
				add_edge(inds[dir][x][y], inds[dir][nx][ny], A);
			}
		}
	}

	set<pair<ll, int>> q;
	q.emplace(0, inds[4][P[0]][Q[0]]);
	for (int v = 0; v < V; v++) {
		dist[v] = DINF;
	}
	dist[inds[4][P[0]][Q[0]]] = 0;
	int dest = inds[4][P[N-1]][Q[N-1]];
	while (!q.empty()) {
		ll d; int cur; tie(d, cur) = *q.begin();
		q.erase(q.begin());
		if (cur == dest) {
			cout << d << '\n';
			return 0;
		}
		for (auto& e : adj[cur]) {
			int nxt; ll w; tie(nxt, w) = e;
			if (d + w < dist[nxt]) {
				q.erase(make_pair(dist[nxt], nxt));
				dist[nxt] = d + w;
				q.emplace(dist[nxt], nxt);
			}
		}
	}
	assert(false);

	return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...