16292022-11-30 12:25:081478Tom és Jerry 1 (80)cpp11Időlimit túllépés 64/80569ms16076 KiB
#include <bits/stdc++.h>

using namespace std;

int main()
{
    //ifstream cin("be.txt");
    int numberOfNodes, numberOfEdges, positionOfTom, numberOfTries, positionOfGoal;
    cin >> numberOfNodes >> numberOfEdges >> positionOfTom >> numberOfTries >> positionOfGoal;

    vector<vector<int>> tomList(numberOfNodes + 1, vector<int> (1)), 
    jerryList(numberOfNodes + 1, vector<int> (1));

    for (int i = 1; i <= numberOfEdges; i++)
    {
        int start, finish, mass;
        cin >> start >> finish >> mass;
        jerryList[start].push_back(finish);
        jerryList[finish].push_back(start);
        if (mass == 2)
        {
            tomList[start].push_back(finish);
            tomList[finish].push_back(start);
        }
    }

    int a;

    /// Tom BFS
    vector<int> tomDistance(numberOfNodes + 1);
    tomDistance[0] = 1;
    queue<int> q;
    tomDistance[positionOfTom] = 1;
    q.push(positionOfTom);
    while (!q.empty())
    {
        a = q.front();
        q.pop();
        for (int i = 1; i < tomList[a].size(); i++)
        {
            if (!tomDistance[tomList[a][i]])
            {
                tomDistance[tomList[a][i]] = tomDistance[a] + 1;
                q.push(tomList[a][i]);
            }
        }
    }

    ///for(int i=1; i<=numberOfNodes; i++) cout<<tomDistance[i]<<" ";

    /// Jerry BFS
    while (numberOfTries--)
    {
        int positionOfJerry;
        cin >> positionOfJerry;

        vector<int> jerryDistance(numberOfNodes + 1);
        q.push(positionOfJerry);
        jerryDistance[0] = 1;
        jerryDistance[positionOfJerry] = 1;

        while (!q.empty())
        {
            a = q.front();
            q.pop();

            for (int i = 1; i < jerryList[a].size(); i++)
            {
                if (!jerryDistance[jerryList[a][i]] && (tomDistance[jerryList[a][i]] == 0 || 
                jerryDistance[a] + 1 < tomDistance[jerryList[a][i]]))
                {
                    q.push(jerryList[a][i]);
                    jerryDistance[jerryList[a][i]] = jerryDistance[a] + 1;
                }
            }
        }

        if (jerryDistance[positionOfGoal] != 0)
            cout << "IGEN" << '\n';
        else
            cout << "NEM" << '\n';
    }

    return 0;
}
RészfeladatÖsszpontTesztVerdiktIdőMemória
base64/80
1Elfogadva0/03ms1812 KiB
2Elfogadva0/04ms2356 KiB
3Elfogadva4/42ms2264 KiB
4Elfogadva4/42ms2424 KiB
5Elfogadva4/42ms2668 KiB
6Elfogadva4/42ms2876 KiB
7Elfogadva4/43ms2916 KiB
8Elfogadva4/44ms3220 KiB
9Elfogadva4/44ms3440 KiB
10Elfogadva4/44ms3744 KiB
11Elfogadva4/410ms4456 KiB
12Elfogadva4/416ms5904 KiB
13Elfogadva4/427ms6420 KiB
14Elfogadva4/450ms9540 KiB
15Elfogadva4/475ms10188 KiB
16Elfogadva4/486ms16076 KiB
17Elfogadva4/4119ms14324 KiB
18Elfogadva4/475ms12408 KiB
19Időlimit túllépés0/4564ms8392 KiB
20Időlimit túllépés0/4550ms8396 KiB
21Időlimit túllépés0/4565ms7440 KiB
22Időlimit túllépés0/4569ms8952 KiB