79992024-01-12 09:56:50UnluckYCiklikus rácsháló gráfcpp17Accepted 40/4028ms4668 KiB
// Source: https://usaco.guide/general/io

#include <bits/stdc++.h>
using namespace std;

int INF = 1e9;

int main() {
	int n, m, kk; cin >> n >> m >> kk;
	vector<vector<int>> d(n*m+1, vector<int>(n*m+1, INF));

	for (int i = 1; i <= n*m; i++){
		cerr << setw(3) << i << " ";
		if (i % m == 0) cerr << "\n";
		d[i][i] = 0;
		if (i % m == 1){ // bal szél
			if (i < m+1){ // teteje
				d[i][i+m] = 1;
				d[i][i + (n-1)*m] = 1;
			}
			else if (i > (n-1)*m){ // alja
				d[i][i-m] = 1;
				d[i][1] = 1;
			} 
			else {
				d[i][i-m] = 1;
				d[i][i+m] = 1;
			}
			d[i][i+1] = 1;
			d[i][i+m-1] = 1;
		}
		else if (i % m == 0){ // jobb szél
			if (i < m+1){ // teteje
				d[i][i+m] = 1;
				d[i][i + (n-1)*m] = 1;
			}
			else if (i > (n-1)*m){ // alja
				d[i][i-m] = 1;
				d[i][m] = 1;
			} 
			else {
				d[i][i-m] = 1;
				d[i][i+m] = 1;
			}
			d[i][i-1] = 1;
			d[i][i-m+1] = 1;
		}
		else if (i < m+1){ // teteje
				d[i][i+1] = 1;
				d[i][i-1] = 1;
				d[i][i+m] = 1;
				d[i][i + (n-1)*m] = 1;
		}
		else if (i > (n-1)*m){ // alja
				d[i][i+1] = 1;
				d[i][i-1] = 1;
				d[i][i-m] = 1;
				d[i][i - (n-1)*m] = 1;
		} 
		else {
				d[i][i+1] = 1;
				d[i][i-1] = 1;
				d[i][i-m] = 1;
				d[i][i + m] = 1;
		}
	}

	for (int k = 1; k <= n*m; k++){
		for (int i = 1; i <= n*m; i++){
			for (int j = 1; j <= n*m; j++){
				if (d[i][k] != INF && d[k][j] != INF){
					d[i][j] = min(d[i][j], d[i][k] + d[k][j]);
				}
			}
		}
	}

	for (int i = 1; i <= n*m; i++){
		for (int j = 1; j <= n*m; j++){
			if (d[i][j] == INF){
				cerr << i << " " << j << endl;
			}
		}
	}

	for (int _ = 0; _ < kk; _++){
		int ki, kj; cin >> ki >> kj;
		for (int i = 1; i <= n*m; i++){
			for (int j = 1; j <= n*m; j++){
				d[i][j] = min(d[i][j], d[i][ki] + d[kj][j] + 1);
				d[i][j] = min(d[i][j], d[i][kj] + d[ki][j] + 1);
			}
		}
		int ans = 0;
		for (int i = 1; i <= n*m; i++){
			for (int j = 1; j <= n*m; j++){
					ans = max(ans, d[i][j]);
			}
		}
		cerr << d[10][119] << endl;
		cout << ans << endl;
	}


}
SubtaskSumTestVerdictTimeMemory
base40/40
1Accepted0/03ms1808 KiB
2Accepted0/028ms2428 KiB
3Accepted2/23ms2228 KiB
4Accepted2/23ms2432 KiB
5Accepted2/23ms2668 KiB
6Accepted2/23ms3004 KiB
7Accepted2/24ms3172 KiB
8Accepted2/24ms3424 KiB
9Accepted2/24ms3552 KiB
10Accepted2/23ms3584 KiB
11Accepted2/24ms3712 KiB
12Accepted2/219ms4068 KiB
13Accepted2/214ms3832 KiB
14Accepted2/24ms3792 KiB
15Accepted2/214ms4104 KiB
16Accepted2/24ms3940 KiB
17Accepted2/210ms3964 KiB
18Accepted2/24ms3952 KiB
19Accepted2/23ms3896 KiB
20Accepted2/23ms4156 KiB
21Accepted2/26ms4416 KiB
22Accepted2/228ms4668 KiB