ENG  RUSTimus Online Judge
Online Judge
Problems
Authors
Online contests
About Online Judge
Frequently asked questions
Site news
Webboard
Links
Problem set
Submit solution
Judge status
Guide
Register
Update your info
Authors ranklist
Current contest
Scheduled contests
Past contests
Rules
back to board

Discussion of Problem 2109. Tourism on Mars

#include<iostream>
using namespace std;
int main()
{
    int CityCount, RoutesCount;

    cin >> CityCount;
    int *Road = new int[(CityCount - 1)*2];
    //Roads
    for (int i = 0; i < (CityCount - 1)*2; i++)
    {
        cin >> Road[i];
    }

    cin >> RoutesCount;
    int *Route = new int[RoutesCount*2];
    //Routes
    for (int i = 0; i < RoutesCount*2; i++)
    {
        cin >> Route[i];
    }
    //Road Matrix
    int j = 0;
    int **RMatrix= new int*[CityCount];
    for (int i = 0; i < CityCount; i++)
    {
        int *LineofMatrix = new int[CityCount];
        for (int i = 0; i < (CityCount - 1) * 2; i += 2)
        {
            if (Road[i] == j + 1)
                LineofMatrix[Road[i + 1] - 1] = 1;
            if (Road[i + 1] == j + 1)
                LineofMatrix[Road[i] - 1] = 1;
        }
        j++;
        RMatrix[i] = LineofMatrix;
    }
    //Output Road Matrix
    /*
    cout << endl;
    for (int i = 0; i < CityCount; i++)
    {
        for (int j = 0; j < CityCount; j++)
            if (RMatrix[i][j]>0)
                cout << RMatrix[i][j] << ' ';
            else
            {
                RMatrix[i][j] = 0;
                cout << 0 << ' ';
            }

        cout << endl;
    }
    cout << endl;
    */
    // alghoritm
    int *Value = new int[CityCount];
    int Vl = 0;
    int *Locked = new int[CityCount];
    int Lock = 0;
    bool BLock = false;

    int Bu = 0;
    int Final;
    int Cur;
    int PreCur=-1;
    int Iter=0;
    int min = INT_MAX;
    for (int i = 0; i < RoutesCount * 2; i+=2)
    {
        int *Buff = new int[CityCount * 2];
        Cur = Route[i] - 1;
        Final = Route[i + 1] - 1;
        Buff[Bu++] = Cur;
        Buff[Bu++] = Final;
        if (RMatrix[Cur][Final] == 1)
        {
            goto fin;
        }
        else
        {
            for (int j = 0; j < CityCount; j++)
            {
                if ((RMatrix[Cur][j] == 1) && (j != PreCur))
                {
                    PreCur = Cur;
                    Buff[Bu++] = j;
                    Iter++;
                    Cur = j;
                    j = -1;
                }
                if (RMatrix[Cur][Final] == 1)
                {
                    goto fin;
                }
                if (j == CityCount - 1)
                {
                    j = Cur;
                    Cur = PreCur;
                    Bu -= Iter;
                    Iter = 0;
                }
            }
        }
        fin:
        for (int z = 0; z < Bu; z++)
        {
            if (min > Buff[z])
            {
                min = Buff[z];
            }
        }
        Bu = 0;
        Iter = 0;
        cout << min+1 << endl;
        min = INT_MAX;
        delete[] Buff;
    }




    //system("pause");
    return 0;
}
I also WA#3 help me
Jane Soboleva (SumNU) Re: C++, What`s wrong with this code ? Test 3 fail. [1] // Problem 2109. Tourism on Mars 6 Mar 2017 14:38
I fixed mine by changing "writeln(answer)" to "if x = y then writeln(x) else writeln(answer)".
(program wasn't working correctly if start and end point were the same)
OK, solved this problem but get a RE on test#6.