#include #define endl '\n' using namespace std; const long long maxN = 205, maxP = 1e5 + 5, maxK = 205, moves_x[] = {-1, 0, 1, 0}, moves_y[] = {0, 1, 0, -1}; struct party { long long x, y, start, duration; party(){} party(long long x, long long y, long long start, long long duration) { this->x = x; this->y = y; this->start = start; this->duration = duration; } }; struct edge { long long node_i, node_j, weight; string path; edge(){} edge(long long node_i, long long node_j, long long weight, string path) { this->node_i = node_i; this->node_j = node_j; this->weight = weight; this->path = path; } }; struct shop { long long i, j; shop(){} shop(long long i, long long j) { this->i = i; this->j = j; } }; bool is_shop[maxN][maxN], visited[maxN][maxN]; long long n, p, k, matrix[maxN][maxN], a, b, x, y, start, duration, dist[maxN][maxN], current_min, ind_shop; long long my_ans; string paths[maxN][maxN], answer; shop shops[maxK]; party parties[maxP]; priority_queue pq; clock_t startTime, currTime; void input() { cin>>n>>p>>k; for(long long i = 0; i < n; i++) { for(long long j = 0; j < n; j++) { cin>>matrix[i][j]; } } cin>>a>>b; for(long long i = 0; i < p; i++) { cin>>x>>y>>start>>duration; parties[i] = party(x - 1, y - 1, start, duration); } for(long long i = 0; i < k; i++) { cin>>x>>y; shops[i] = shop(x - 1, y - 1); } } bool cmp_parties(party q, party w) { if(q.start != w.start) return q.start < w.start; else return q.duration > w.duration; } bool operator<(edge q, edge w) { return q.weight > w.weight; } char xhash(long long i) { if(i == 0) return 'U'; else if(i == 1) return 'R'; else if(i == 2) return 'D'; else return 'L'; } long long absolute(long long q, long long w) { if(q > w) return q - w; else return w - q; } void dijkstra(long long start_i, long long start_j, long long cnt_cakes) { for(int i = 0; i < n; i++) { for(int j = 0; j < n; j++) { visited[i][j] = false; dist[i][j] = -1; paths[i][j] = ""; } } pq.push(edge(start_i, start_j, 0, "")); dist[start_i][start_j] = 0; paths[start_i][start_j] = ""; long long current_i, current_j, current_weight; string current_path; while(!pq.empty()) { current_i = pq.top().node_i; current_j = pq.top().node_j; current_weight = pq.top().weight; current_path = pq.top().path; pq.pop(); if(visited[current_i][current_j]) continue; else visited[current_i][current_j] = true; for(long long i = 0; i < 4; i++) { if(current_i + moves_x[i] >= 0 && current_i + moves_x[i] < n && current_j + moves_y[i] >= 0 && current_j + moves_y[i] < n) { if(!visited[current_i + moves_x[i]][current_j + moves_y[i]]) { if(dist[current_i + moves_x[i]][current_j + moves_y[i]] == -1 || dist[current_i + moves_x[i]][current_j + moves_y[i]] > current_weight + (absolute(matrix[current_i][current_j], matrix[current_i + moves_x[i]][current_j + moves_y[i]]) + cnt_cakes) * (absolute(matrix[current_i][current_j], matrix[current_i + moves_x[i]][current_j + moves_y[i]]) + cnt_cakes) + 1) { dist[current_i + moves_x[i]][current_j + moves_y[i]] = current_weight + (absolute(matrix[current_i][current_j], matrix[current_i + moves_x[i]][current_j + moves_y[i]]) + cnt_cakes) * (absolute(matrix[current_i][current_j], matrix[current_i + moves_x[i]][current_j + moves_y[i]]) + cnt_cakes) + 1; paths[current_i + moves_x[i]][current_j + moves_y[i]] = current_path + xhash(i); pq.push(edge(current_i + moves_x[i], current_j + moves_y[i], dist[current_i + moves_x[i]][current_j + moves_y[i]], paths[current_i + moves_x[i]][current_j + moves_y[i]])); } } } } } } void solve(long long current_i, long long current_j, long long current_time, long long cnt_cakes) { // cout<= 3500) return; if(current_time > 10000000000LL) return; if(current_time > parties[p - 1].start + parties[p - 1].duration) return; for(long long i = 0; i < p; i++) { if(current_i == parties[i].x && current_j == parties[i].y && current_time >= parties[i].start && current_time <= parties[i].start + parties[i].duration) { my_ans += (parties[i].start + parties[i].duration - current_time) * cnt_cakes; answer += "+"; if(cnt_cakes != 0) answer += "50"; // cout<<"party !!!!!!! "< current_time) { dijkstra(current_i, current_j, cnt_cakes); // if(paths[parties[i].x][parties[i].y] == "") continue; // cout<<"path "< current_time) { solve(current_i, current_j, parties[i].start, cnt_cakes); return; } } for(long long i = 0; i < p; i++) { if(parties[i].start > current_time) { dijkstra(current_i, current_j, cnt_cakes); answer += paths[parties[i].x][parties[i].y]; solve(parties[i].x, parties[i].y, max(parties[i].start, current_time + dist[parties[i].x][parties[i].y]), cnt_cakes); return; } } if(current_i > 0) { answer += 'U'; solve(current_i - 1, current_j, current_time + 1, cnt_cakes); } else if(current_j < n - 1) { answer += 'R'; solve(current_i, current_j + 1, current_time + 1, cnt_cakes); } else if(current_i < n - 1) { answer += 'D'; solve(current_i + 1, current_j, current_time + 1, cnt_cakes); } else { answer += 'L'; solve(current_i, current_j - 1, current_time + 1, cnt_cakes); } } int main() { // ios_base::sync_with_stdio(false); // cin.tie(NULL); freopen("party.in", "r", stdin); freopen("party.out", "w", stdout); input(); startTime = clock(); a--; b--; sort(parties, parties + p, cmp_parties); ///Test dijkstra // dijkstra(a, b, 0); // for(long long i = 0; i < n; i++) // { // for(long long j = 0; j < n; j++) // { // cout<