79992024-01-12 09:56:50UnluckYCiklikus rácsháló gráfcpp17Elfogadva 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;
	}


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