// please can anyone provide a Code of this problem.
Janet is in an Uber on her way to an interview. The driver promises to take her to the venue as soon as possible. The driver is aware
that:-
-There are 'n' junctions in the city of Mumbai, numbered from '1' to 'n'.
-Janet's interview location is at junction 'n'. They are initially at junction '1'.
-There are 'm' bidirectional roads connecting some pairs of junctions, each one requiring some amount of time to pass through it.
At every junction, there are traffic lights denoting whether they are allowed to go further or to wait. Traffic lights have two colors,
'red' and 'green'. The driver can commute through junctions based on these conditions:
1. At any junction, if the traffic signal's light is green, then they can go immediately, otherwise, they have to wait until traffic signal
becomes green.
2. Traffic signal changes its color every 'k' seconds of time at all junctions simultaneously.
Initially, at the '0th' second, all traffic lights have changed to green color at all the junctions. If the cab driver reaches a junction at a second when the traffic light changes its color, then he sees the traffic light after the change.
Can you help the driver determine the least amount of time needed to reach the interview location?
Complete the function leastTimeToInterview which takes in three integers n,k and m and returns the least amount of time
needed to reach the interview location, in seconds. You need to take the information about the roads from the standard input. They
will be specified in 'm' lines, as described in the input format section below.
Input Format:-
-The first line contains an integer 'n', the number of junctions.
-The second line contains an integer 'k' denoting the time taken by a signal to change its color.
-The third line contains an integer 'm' denoting the number of roads.
-The next 'm' lines describe the roads. Each consist of three space-separated integers 'i','j' and 't' where 'i' and 'j' denotes a road between two junctions and 't' denotes time required to travel through it.
Constraints-:
- 1<= n <= pow(10,4)
- 1<= k <= pow(10,2)
- 1<= m <= pow(10,5)
- 1<= i,j <= n
- 1<= t <= pow(10,3)
- There can be self-loops, i.e., roads connecting the same pair of junctions.
- There is at least one path from junction '1' to junction 'n'.
Output Format-:
Print a single integer denoting the shortest amount of time required to reach junction 'n'.
- Junction number 1: The cab driver can visit any of the adjacent junctions. He chooses to visit junction 2. Since the traffic signal is
'green' at the '0th' second, He can visit junction 2 which takes 3 seconds.
- Junction number 2: The traffic signal is still 'green' since traffic signals change color every '4' seconds. The cab driver now chooses
to visit junction '3' which takes '1' second, and we are now at the '4th' second.
- Junction number 3: 4 seconds have passed, and the traffic signal has already become 'red'. They have to wait for 4 more seconds
until the signal again becomes 'green'.
- Junction number 5: The cab takes the route to junction 5 which takes 1 second. So far, 9 seconds have passed.
- Junction number 7: The traffic signal is still 'green' and the cab can go to junction 7. It takes 2 seconds to reach junction 7.
In total, it takes 11 seconds to go from junction 1 to junction 7 . It can be shown that this is the minimum amount of time possible.
#include <bits/stdc++.h>
using namespace std;
// Complete the leastTimeToInterview function below.
long leastTimeToInterview(int n, int k, int m) {
// Return the least amount of time needed to reach the interview location in seconds.
}
int main()
{
ofstream fout(getenv("OUTPUT_PATH"));
int n;
cin >> n;
cin.ignore(numeric_limits<streamsize>::max(), '\n');
int k;
cin >> k;
cin.ignore(numeric_limits<streamsize>::max(), '\n');
int m;
cin >> m;
cin.ignore(numeric_limits<streamsize>::max(), '\n');
long result = leastTimeToInterview(n, k, m);
fout << result << "\n";
fout.close();
return 0;
}
Ok. I try to live and let live, but did you really hijack another thread to link to this one because you waited a whole hour for an answer to a fairly complex question and got impatient? Please don't do that. Wait for it, someone will help if they can, when they can.
This question takes some time to digest ... about all I see so far is it looks like a graph theory question, some of which you can solve with just some hand-waving and vectors and others which require a full graph representation. But I am not 100% sure of what we have here, there may be some clever or simple thing that solves it without all that mess.
What do you have so far? Besides that fairly generic and unrelated to the meat of the problem code, I mean? Honestly, I don't even want to see your code, yet, I would rather hear you tell me how you plan to solve the problem, what algorithm, what data structure, etc. Coding comes much later.
how will me doing it for you make you better at doing graphs? (It won't, you will still be bad at them, and I will get a review of something I haven't looked at since college and don't really care much about).
So I will ask again... how will you solve this problem? Pretend you have in hand a working datastructure that you can just use, once you figure out HOW to use it. Talk through the approach you need. Start small. You have a good idea ... a file reader is one necessary part of the whole. What are the other parts?
I am just going to tell you right now that I won't do it for you. I will help, once you decide to own it and do it yourself.
You don't need a floating point variable. Integer division will do.
1 2
if (v != goal && D[v] / k % 2 == 1)
D[v] += k - D[v] % k;
Note that you must ensure that v is not the goal (the final node) since in that case you don't care if the light is red. That's probably what you're missing.