35852023-03-01 09:14:00gergoRácsháló gráfcsharpAccepted 50/50144ms26792 KiB
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace racshalograf
{
    class Program
    {
        static void Main(string[] args)
        {
            int[] bemenet = Console.ReadLine().Split().Select(int.Parse).ToArray();
            int n = bemenet[0], m = bemenet[1], h = bemenet[2];

            
            int csucsok = n * m;

            int[,] matrix = new int[csucsok+1, csucsok+1];

            for (int i = 1; i <= csucsok; i++)
            {
                for (int j = 1; j <= csucsok;  j++)
                {
                    if (i!=j) matrix[i, j] = n+m;
                }
            }



            for (int i = 1; i <= n; i++) // (i-1)*m+j
            {
                for (int j = 1; j <= m; j++)
                {
                    int aktpont = (i - 1) * m + j;

                    int jobbra = aktpont + 1;
                    int balra = aktpont - 1;
                    int felette = aktpont - m;
                    int alatta = aktpont + m;

                    if (i == 1) felette = 0;
                    if (i == n) alatta = 0;
                    if (j == 1) balra = 0;
                    if (j == m) jobbra = 0;

                    matrix[aktpont, jobbra] = 1;
                    matrix[jobbra, aktpont] = 1;
                    matrix[alatta, aktpont] = 1;
                    matrix[aktpont, alatta] = 1;
                }
            }

            //for (int i = 0; i < csucsok; i++)
            //{
            //    for (int j = 0; j < csucsok; j++)
            //    {
            //        Console.Write(matrix[i, j] + "\t");
            //    }
            //    Console.WriteLine();
            //}




            for (int k = 1; k <= csucsok; k++) //Floyd-Warshall algoritmus
            {
                for (int i = 1; i <= csucsok; i++)
                {
                    for (int j = 1; j <= csucsok; j++)
                    {
                        if (matrix[i,j]>matrix[i,k]+matrix[k,j])
                        {
                            matrix[i, j] = matrix[i,k] + matrix[k,j];
                        }
                    }
                }
            }

            string ki = "";

            for (int cv = 0; cv < h; cv++)
            {
                bemenet = Console.ReadLine().Split().Select(int.Parse).ToArray();

                matrix[bemenet[0], bemenet[1]] = 1;
                matrix[bemenet[1], bemenet[0]] = 1;

                int k = bemenet[0];

                for (int i = 1; i <= csucsok; i++)
                {
                    for (int j = 1; j <= csucsok; j++)
                    {
                        if (matrix[i,j] > matrix[i,k] + matrix[k,j])
                        {
                            matrix[i, j] = matrix[i,k] + matrix[k,j];
                        }
                    }
                }

                k = bemenet[1];

                for (int i = 1; i <= csucsok; i++)
                {
                    for (int j = 1; j <= csucsok; j++)
                    {
                        if (matrix[i, j] > matrix[i,k] + matrix[k,j])
                        {
                            matrix[i, j] = matrix[i,k] + matrix[k,j];
                        }
                    }
                }


                int max = 0;
                for (int i = 1; i <= csucsok; i++)
                {
                    for (int j = 1; j <= csucsok; j++)
                    {
                        if (matrix[i, j] > max) max = matrix[i, j];
                    }
                }

                ki += max + "\r\n";
            }

            Console.WriteLine(ki);

            Console.ReadKey();
        }
    }
}
SubtaskSumTestVerdictTimeMemory
base50/50
1Accepted0/032ms22508 KiB
2Accepted0/0143ms22940 KiB
3Accepted2/232ms23136 KiB
4Accepted2/232ms23252 KiB
5Accepted2/234ms23676 KiB
6Accepted2/232ms23828 KiB
7Accepted2/243ms24096 KiB
8Accepted2/243ms24108 KiB
9Accepted2/243ms24540 KiB
10Accepted2/235ms24780 KiB
11Accepted2/241ms25312 KiB
12Accepted2/2112ms25716 KiB
13Accepted3/382ms25616 KiB
14Accepted3/343ms25380 KiB
15Accepted3/379ms25500 KiB
16Accepted3/345ms25668 KiB
17Accepted3/370ms25944 KiB
18Accepted3/343ms26192 KiB
19Accepted3/332ms26296 KiB
20Accepted3/334ms26792 KiB
21Accepted3/345ms26624 KiB
22Accepted3/3144ms26600 KiB