#include <bits/stdc++.h>
using namespace std;
int dist[2505][2505], rdist[2505][2505], ans[200005];
vector<int> adj[200005], radj[200005];
queue<int> q;
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n, m, val;
cin >> n;
for (int i=1; i<=n; i++)
{
int l, r;
cin >> l >> r;
for (int j=l; j<=r; j++)
{
adj[i].push_back(j);
radj[j].push_back(i);
}
}
cin >> m >> val;
for (int i=1; i<=n; i++)
{
if (i!=1 && i!=n && i!=val)
continue;
for (int j=1; j<=n; j++)
dist[i][j]=1e8;
dist[i][i]=0;
q.push(i);
while (!q.empty())
{
int u=q.front();
q.pop();
for (int v:adj[u])
{
if (dist[i][u]+1<dist[i][v])
{
dist[i][v]=dist[i][u]+1;
q.push(v);
}
}
}
for (int j=1; j<=n; j++)
rdist[i][j]=1e8;
rdist[i][i]=0;
q.push(i);
while (!q.empty())
{
int u=q.front();
q.pop();
for (int v:radj[u])
{
if (rdist[i][u]+1<rdist[i][v])
{
rdist[i][v]=rdist[i][u]+1;
q.push(v);
}
}
}
}
for (int i=1; i<=n; i++)
{
if (i!=val)
continue;
ans[i]=min(dist[i][1]+dist[1][n], dist[i][n]+dist[n][1]);
for (int j=2; j<n; j++)
ans[i]=min(ans[i], dist[i][j]+rdist[1][j]+rdist[n][j]-1);
}
if (ans[val]<5e7)
cout << ans[val];
else
cout << -1;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
9728 KB |
Output is correct |
2 |
Correct |
5 ms |
9684 KB |
Output is correct |
3 |
Correct |
5 ms |
9724 KB |
Output is correct |
4 |
Execution timed out |
2111 ms |
965952 KB |
Time limit exceeded |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
9684 KB |
Output is correct |
2 |
Correct |
5 ms |
9684 KB |
Output is correct |
3 |
Correct |
5 ms |
9720 KB |
Output is correct |
4 |
Correct |
5 ms |
9684 KB |
Output is correct |
5 |
Correct |
7 ms |
9684 KB |
Output is correct |
6 |
Correct |
6 ms |
9728 KB |
Output is correct |
7 |
Correct |
5 ms |
9688 KB |
Output is correct |
8 |
Correct |
4 ms |
9720 KB |
Output is correct |
9 |
Correct |
4 ms |
9668 KB |
Output is correct |
10 |
Correct |
4 ms |
9724 KB |
Output is correct |
11 |
Correct |
5 ms |
9940 KB |
Output is correct |
12 |
Correct |
4 ms |
9812 KB |
Output is correct |
13 |
Correct |
6 ms |
10452 KB |
Output is correct |
14 |
Correct |
5 ms |
10196 KB |
Output is correct |
15 |
Correct |
5 ms |
9848 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
9684 KB |
Output is correct |
2 |
Correct |
5 ms |
9684 KB |
Output is correct |
3 |
Correct |
5 ms |
9720 KB |
Output is correct |
4 |
Correct |
5 ms |
9684 KB |
Output is correct |
5 |
Correct |
7 ms |
9684 KB |
Output is correct |
6 |
Correct |
6 ms |
9728 KB |
Output is correct |
7 |
Correct |
5 ms |
9688 KB |
Output is correct |
8 |
Correct |
4 ms |
9720 KB |
Output is correct |
9 |
Correct |
4 ms |
9668 KB |
Output is correct |
10 |
Correct |
4 ms |
9724 KB |
Output is correct |
11 |
Correct |
5 ms |
9940 KB |
Output is correct |
12 |
Correct |
4 ms |
9812 KB |
Output is correct |
13 |
Correct |
6 ms |
10452 KB |
Output is correct |
14 |
Correct |
5 ms |
10196 KB |
Output is correct |
15 |
Correct |
5 ms |
9848 KB |
Output is correct |
16 |
Correct |
33 ms |
25528 KB |
Output is correct |
17 |
Correct |
6 ms |
10196 KB |
Output is correct |
18 |
Correct |
61 ms |
43680 KB |
Output is correct |
19 |
Correct |
59 ms |
41832 KB |
Output is correct |
20 |
Correct |
6 ms |
9940 KB |
Output is correct |
21 |
Correct |
13 ms |
14548 KB |
Output is correct |
22 |
Correct |
55 ms |
50180 KB |
Output is correct |
23 |
Correct |
54 ms |
37088 KB |
Output is correct |
24 |
Correct |
45 ms |
36852 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
9684 KB |
Output is correct |
2 |
Correct |
5 ms |
9684 KB |
Output is correct |
3 |
Correct |
5 ms |
9720 KB |
Output is correct |
4 |
Correct |
5 ms |
9684 KB |
Output is correct |
5 |
Correct |
7 ms |
9684 KB |
Output is correct |
6 |
Correct |
6 ms |
9728 KB |
Output is correct |
7 |
Correct |
5 ms |
9688 KB |
Output is correct |
8 |
Correct |
4 ms |
9720 KB |
Output is correct |
9 |
Correct |
4 ms |
9668 KB |
Output is correct |
10 |
Correct |
4 ms |
9724 KB |
Output is correct |
11 |
Correct |
5 ms |
9940 KB |
Output is correct |
12 |
Correct |
4 ms |
9812 KB |
Output is correct |
13 |
Correct |
6 ms |
10452 KB |
Output is correct |
14 |
Correct |
5 ms |
10196 KB |
Output is correct |
15 |
Correct |
5 ms |
9848 KB |
Output is correct |
16 |
Correct |
33 ms |
25528 KB |
Output is correct |
17 |
Correct |
6 ms |
10196 KB |
Output is correct |
18 |
Correct |
61 ms |
43680 KB |
Output is correct |
19 |
Correct |
59 ms |
41832 KB |
Output is correct |
20 |
Correct |
6 ms |
9940 KB |
Output is correct |
21 |
Correct |
13 ms |
14548 KB |
Output is correct |
22 |
Correct |
55 ms |
50180 KB |
Output is correct |
23 |
Correct |
54 ms |
37088 KB |
Output is correct |
24 |
Correct |
45 ms |
36852 KB |
Output is correct |
25 |
Incorrect |
5 ms |
9684 KB |
Output isn't correct |
26 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
9728 KB |
Output is correct |
2 |
Correct |
5 ms |
9684 KB |
Output is correct |
3 |
Correct |
5 ms |
9724 KB |
Output is correct |
4 |
Execution timed out |
2111 ms |
965952 KB |
Time limit exceeded |
5 |
Halted |
0 ms |
0 KB |
- |