9502022-02-03 22:01:53nmarciCiklikus rácsháló gráfcpp11Elfogadva 40/4019ms2516 KiB
#include <iostream>
#include <vector>
#include <map>
#include <queue>
#include <string>
#include <set>
#include <algorithm>
#include <list>
using namespace std;
using ll = long long int;

ll inf = 1e9+7;

ll dp[210][210];

int main(){
    int n,m,el;
    cin >> n >> m >> el;
    for(int i = 0; i <= n * m; ++i){
        for(int j = 0; j <= n * m; ++j){
            dp[i][j] = inf;
        }
        dp[i][i] = 0;
    }
    //construct graph
    for(int row = 0; row < n; ++row){
        for(int column = 1;  column <= m; ++column){
            if(column == 1){
                dp[row * m + 1][row * m + m] = dp[row * m + m][row * m + 1] = 1;
            }
            if(row == 0){
                dp[column][(n - 1) * m + column] = dp[(n - 1) * m + column][column] = 1;
            }
            if(column != m){
                dp[row * m + column][row * m + column + 1] = dp[row * m + column + 1][row * m + column] = 1;
            }
            if(row + 1 != n){
                dp[row * m + column][(row + 1) * m + column] = dp[(row + 1) * m + column][row * m + column] = 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){
                dp[i][j] = min(dp[i][j], dp[i][k] + dp[k][j]);
            }
        }
    }
    while(el--){
        int p, q;
        cin >> p >> q;
        dp[p][q] = dp[q][p] = 1;
        vector<int> v = {p,q};
        ll answ = 0;
        for(auto k : v){
            for(int i = 1; i <= n*m; ++i){
                for(int j = 1; j <= n * m; ++j){
                    dp[i][j] = min(dp[i][j], dp[i][k] + dp[k][j]);
                    //answ = max(answ, dp[i][j]);
                }
            }
        }
        for(int i = 1; i <= n * m; ++i){
            for(int j = 1; j <= n * m; ++j){
                answ = max(answ, dp[i][j]);
            }
        }
        cout << answ << endl;
    }
    return 0;
}
RészfeladatÖsszpontTesztVerdiktIdőMemória
base40/40
1Elfogadva0/02ms1764 KiB
2Elfogadva0/018ms2508 KiB
3Elfogadva2/21ms1868 KiB
4Elfogadva2/21ms1900 KiB
5Elfogadva2/21ms1904 KiB
6Elfogadva2/21ms1908 KiB
7Elfogadva2/23ms2164 KiB
8Elfogadva2/23ms2172 KiB
9Elfogadva2/23ms2176 KiB
10Elfogadva2/22ms1984 KiB
11Elfogadva2/24ms2176 KiB
12Elfogadva2/213ms2476 KiB
13Elfogadva2/29ms2400 KiB
14Elfogadva2/23ms2188 KiB
15Elfogadva2/29ms2408 KiB
16Elfogadva2/23ms2200 KiB
17Elfogadva2/28ms2264 KiB
18Elfogadva2/23ms2176 KiB
19Elfogadva2/21ms1976 KiB
20Elfogadva2/21ms1996 KiB
21Elfogadva2/24ms2200 KiB
22Elfogadva2/219ms2516 KiB