1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
/*
 *  Copyright (C) 2020  Paweł Widera
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details:
 *  http://www.gnu.org/licenses/gpl.html
 */
#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
#include <deque>
#include <chrono>
#include <unordered_map>
using namespace std;

#ifdef DEBUG
	template<typename... ArgTypes>
	inline void debug_print(ArgTypes... args) {
		using expand = int[];
		(void)expand{0, ((cerr << args << " "), void(), 0)... };
		cerr << endl;
	}

	#define DBG(...) debug_print(__VA_ARGS__);
	#define DBGx(x) cerr << #x << " " << x << endl;
	#define DBGa(array) cerr << #array << " "; for (auto a: array) { cerr << a << " "; } cerr << endl;
#else
	#define DBG(...)
	#define DBGx(x)
	#define DBGa(array)
#endif

#define MAX_COST 1000000000
#define MAX_DISTANCE 4000000000000000

struct Cost {
	int up;
	int down;

	int operator<(const Cost& other) const {
		if (up == other.up) {
			return down < other.down;
		} else {
			return up < other.up;
		}
	}

	bool operator==(const Cost& other) const {
		return up ==  other.up && down == other.down;
	}
};

struct Neighbour {
	int dx;
	int dy;
};
const Neighbour NEIGHBOURHOOD[] = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};


long long int find_shortest_path(int target, const Cost& cost, const vector<deque<int>>& graph) {
	vector<long long int> distances(target + 1, -1);
	distances[0] = 0;

	deque<pair<int, int>> queue;
	queue.push_back(make_pair(0, 0));

	while (!queue.empty()) {
		int current = queue.front().first;
		queue.pop_front();
		if (current == target) {
			continue;
		}

		for (auto node: graph[current]) {
			int weight = node > current ? cost.up : cost.down;
			long long int d = distances[current] + weight;

//DBG("next=", node, " d=", d, "current=", current)

			if (distances[node] < 0) {
				distances[node] = d;
				queue.push_back(make_pair(node, d));
			}
			else if (d < distances[node] + weight) {
				distances[node] = d;
			}
		}

		sort(queue.begin(), queue.end(),
			[](pair<int,int> a, pair<int,int> b){ return a.second < b.second; });
	}

	return distances[target];
}


int main() {
	ios::sync_with_stdio(false);
	cin.tie(nullptr);

	int n, m, k;
	string row;
	int cost_up, cost_down;

	vector<string> map;
	vector<Cost> costs;

	cin >> n >> m >> k;
	map.reserve(n);
	costs.reserve(k);

	for (int i = 0; i < n; ++i) {
		cin >> row;
		map.emplace_back(row);
	}
	for (int i = 0; i < k; ++i) {
		cin >> cost_up >> cost_down;
		costs.push_back({cost_up, cost_down});
	}

	auto start_time = chrono::high_resolution_clock::now();

	// construct a graph (list of neighbours)
	vector<deque<int>> graph(n * m);
	for (int r = 0; r < n; ++r) {
		for (int c = 0; c < m; ++c) {
			if (map[r][c] == 'X') {
				continue;
			}

			int node = r * m + c;
			for (auto next: NEIGHBOURHOOD) {
				int x = c + next.dx;
				int y = r + next.dy;

				if (0 <= x && x < m && 0 <= y && y < n && map[y][x] != 'X') {
					graph[node].push_back(y * m + x);
				}
			}
		}
	}

	vector<Cost> unique_costs;
	vector<int> repeats;

	stable_sort(costs.begin(), costs.end());
	for (auto cost: costs) {
		int index = unique_costs.size() - 1;
		if (!unique_costs.empty() && unique_costs[index] == cost) {
			repeats[index] += 1;
		} else {
			unique_costs.push_back(cost);
			repeats.push_back(1);
		}
	}

	// version A: for each climber run full dijkstra
	// version B: find 3 paths: for 2 divergent low,high and high,low
	//            and 1 equal case, then score all climbers


	int target = n * m - 1;
	int count = 0;
	long long int best = MAX_DISTANCE;

	for (unsigned int i = 0; i < unique_costs.size(); ++i) {
		long long int score = find_shortest_path(target, unique_costs[i], graph);

//DBG(score, "vs", best, "count=", count, "cost=", unique_costs[i].up, unique_costs[i].down, "repeats=", repeats[i])

		if (score == best) {
			count += repeats[i];
		}
		if (score < best) {
			best = score;
			count = repeats[i];
		}

		auto stop_time = chrono::high_resolution_clock::now();
		auto elapsed = chrono::duration_cast<std::chrono::milliseconds>(stop_time - start_time).count();
		if (elapsed > 6500) {
			break;
		}
	}

	cout << best << " " << count << endl;
	return 0;
}