Skip to content

Commit 89a520d

Browse files
Created shortestRoute.cpp
Program to return the minimum time required by the i-th traveller to reach his destination, if it is possible.
1 parent ba23f48 commit 89a520d

File tree

1 file changed

+163
-0
lines changed

1 file changed

+163
-0
lines changed
+163
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,163 @@
1+
/*
2+
3+
CODECHEF PROBLEM:-
4+
PROBLEM CODE: SHROUTE
5+
LINK: https://www.codechef.com/JUNE21C/problems/SHROUTE
6+
7+
INPUT:
8+
The first line contains an integer T, the number of test cases. Then the test cases follow.
9+
Each test case contains three lines of input.
10+
The first line contains two integers N, M.
11+
The second line contains N integers A1,A2,…,AN.
12+
The third line contains M integers B1,B2,…,BM.
13+
14+
OUTPUT:
15+
For each test case, output M space-separated integers C1,C2,…,CM,
16+
where Ci is the minimum time required by the i-th traveller to reach his destination,
17+
if the i-th traveller can't reach his destination, Ci=−1.
18+
19+
SAMPLE OUTPUT:
20+
3
21+
5 1
22+
1 0 0 0 0
23+
5
24+
5 1
25+
1 0 0 0 2
26+
4
27+
5 2
28+
2 0 0 0 1
29+
3 1
30+
31+
*/
32+
33+
#include <iostream>
34+
using namespace std;
35+
#include <bits/stdc++.h>
36+
37+
int main() {
38+
// your code goes here
39+
ios_base::sync_with_stdio(false);
40+
cin.tie(NULL);
41+
long int t;
42+
cin >> t;
43+
while(t--)
44+
{
45+
long int n,m;
46+
cin >> n >> m;
47+
long int a[n];
48+
long int b[m];
49+
long int two = -1;
50+
long int one = -1;
51+
long int c = 0;
52+
for (int i=0;i<n;i++)
53+
{
54+
cin >> a[i];
55+
if (a[i]==2)
56+
{
57+
two = i;
58+
}
59+
}
60+
for (int i=0;i<n;i++)
61+
{
62+
if (a[i]==1)
63+
{
64+
one = i;
65+
break;
66+
}
67+
}
68+
for(int i=0;i<m;i++)
69+
{
70+
cin >> b[i];
71+
}
72+
int *copy1{ new int[n]{} };
73+
int *copy2{ new int[n]{} };
74+
//int *copy1 = new int[n-1]();
75+
//int *copy2 = new int[n-1]();
76+
if (one != -1)
77+
{
78+
for(int i=one;i<n;i++)
79+
{
80+
if (a[i] == 0)
81+
{
82+
c++;
83+
copy1[i] = c;
84+
}
85+
if (a[i] == 1)
86+
{
87+
c=0;
88+
copy1[i] = -1;
89+
}
90+
if (a[i] == 2)
91+
{
92+
c++;
93+
copy1[i] = -1;
94+
}
95+
}
96+
}
97+
if (two != -1)
98+
{
99+
for(int i=two;i>=0;i--)
100+
{
101+
if (a[i] == 0)
102+
{
103+
c++;
104+
copy2[i] = c;
105+
}
106+
if (a[i] == 2)
107+
{
108+
c=0;
109+
copy2[i] = -1;
110+
}
111+
if (a[i] == 1)
112+
{
113+
c++;
114+
copy2[i] = -1;
115+
}
116+
}
117+
}
118+
119+
for (int i=0;i<n;i++)
120+
{
121+
if (copy1[i]==0)
122+
{
123+
copy1[i]=INT_MAX;
124+
}
125+
if (copy2[i]==0)
126+
{
127+
copy2[i]=INT_MAX;
128+
}
129+
if (copy1[i]==-1)
130+
{
131+
copy1[i]=0;
132+
}
133+
if (copy2[i]==-1)
134+
{
135+
copy2[i]=0;
136+
}
137+
}
138+
for(int i=0;i<m;i++)
139+
{
140+
long int x = int(b[i]);
141+
if (a[x-1] != 0 || b[i] == 1)
142+
{
143+
cout << 0 << " ";
144+
}
145+
else
146+
{
147+
long int ans = min(copy1[x-1],copy2[x-1]);
148+
if (ans == INT_MAX)
149+
{
150+
cout << "-1 ";
151+
}
152+
else
153+
{
154+
cout << ans << " ";
155+
}
156+
}
157+
}
158+
cout << "\n";
159+
160+
161+
}
162+
return 0;
163+
}

0 commit comments

Comments
 (0)