Submission #1228394

#TimeUsernameProblemLanguageResultExecution timeMemory
1228394JerRailway Trip 2 (JOI22_ho_t4)C++20
0 / 100
2095 ms106052 KiB
#include <bits/stdc++.h>

using namespace std;

const int MAXN = 2005;
vector<int> con[MAXN];
bool vis[MAXN];
int n, k, m, q;

int query(int s, int t)
{
    memset(vis, false, sizeof vis);
    queue<int> q;
    q.push(s);

    int l = 0;
    while (!q.empty())
    {
        int len = q.size();
        for (int z = 0; z < len; z++)
        {
            int curr = q.front();
            q.pop();

            vis[curr] = true;

            if (curr == t)
                return l;

            for (auto i : con[curr])
                if (!vis[i])
                    q.push(i);
        }

        l++;
    }

    return -1;
}

int main()
{
    scanf("%d%d%d", &n, &k, &m);
    int a, b;

    for (int i = 0; i < m; i++)
    {
        scanf("%d%d", &a, &b);
        a--, b--;
        if (a < b)
        {
            for (int s = a; s <= min(a + k - 1, b - 1); s++)
                for (int j = s + 1; j <= b; j++)
                    con[s].push_back(j);
        }
        else
        {
            for (int s = a; s >= max(a - k + 1, b + 1); s--)
                for (int j = s - 1; j >= b; j--)
                    con[s].push_back(j);
        }
    }

    scanf("%d", &q);

    while (q--)
    {
        int s, t;
        scanf("%d%d", &s, &t);
        s--, t--;
        printf("%d\n", query(s, t));
    }

    return 0;
}

Compilation message (stderr)

Main.cpp: In function 'int main()':
Main.cpp:43:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   43 |     scanf("%d%d%d", &n, &k, &m);
      |     ~~~~~^~~~~~~~~~~~~~~~~~~~~~
Main.cpp:48:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   48 |         scanf("%d%d", &a, &b);
      |         ~~~~~^~~~~~~~~~~~~~~~
Main.cpp:64:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   64 |     scanf("%d", &q);
      |     ~~~~~^~~~~~~~~~
Main.cpp:69:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   69 |         scanf("%d%d", &s, &t);
      |         ~~~~~^~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...