Minimizing Dark Travel
बनाया गया: 25 जनवरी 2025
बनाया गया: 25 जनवरी 2025
Contest Home
Quiz
Coding Problems
<> Problem
Submissions
Min Travel in Darkness
Accuracy: 0.0%
Submissions: 0
Points: 20
Alice and Bob live in a city represented as a 2D plane. Alice is currently at the coordinates ( a[0] a[1] ) and she wants to meet Bob, who is at the coordinates (b[0], b[1]).
The city has n streetlights, each illuminating a circular area. A streetlight located at (lights[i] [0], lights[i][1]) provides light within a circle of radius lights[i][2]. The illuminated areas of different streetlights may overlap. Alice can take any path on the 2D plane to meet Bob. She can move in any arbitrary direction at a constant speed of 1 unit per second. Since it is nighttime, Alice wants to avoid traveling through dark areas as much as possible. Determine the minimum time Alice has to spend traveling in the dark.
Finally, you need to return the greatest integer less than or equal to the result (i.e., the floor value of the answer). For example, If the answer is 4.97, you should return 4.
Examples:
Input: a[]= [- 2, 2] b[]= [2, - 2] . lights[] =[ [1, 1, 2] , [2, 2, 1] ]
Output: 2
Explanation: Alice needs to use the first streetlight with a radius of 2 to minimize the time spent in the dark. In this case, the minimum possible time will be 2.324 seconds, and the floor of 2.324 is 2.
Input: a[]= [- 1, - 1] , b[]= [1, 1] lights[] = [[0, 0, 1], [0, 0, 2] ]
Output: 0 code in c++
Analysis
We have:
Alice travels at speed 1 unit/second. We want to minimize the total time spent outside any illuminated region. Equivalently, if we think of “time in darkness” as “distance traveled outside the union of circles,” we want to find a path from to that minimizes the length of its portion outside all circles.
A common competitive-programming approach to problems like this is to build a (weighted) graph where:
Nodes:
0
).n+1
).1
through n
).Edges & Weights:
0
) and streetlight (node i
):
n+1
) and streetlight (node i
):
0
to n+1
as . (Because you could technically ignore all lights.)Once this graph is built (with nodes and up to edges), run a shortest path algorithm (Dijkstra) from node 0
to node n+1
. The result gives you the minimum total darkness distance. Since Alice moves at speed 1 unit/s, that distance is also the time in darkness. Finally, we take the floor of that value.
cpp#include <bits/stdc++.h> using namespace std; // A simple function to compute Euclidean distance. double dist(double x1, double y1, double x2, double y2) { double dx = x1 - x2; double dy = y1 - y2; return sqrt(dx*dx + dy*dy); } int main(){ ios::sync_with_stdio(false); cin.tie(nullptr); // Read the coordinates of A (start) and B (destination) double ax, ay; double bx, by; cin >> ax >> ay >> bx >> by; // Read the number of streetlights int n; cin >> n; // Read streetlights: each has (x, y, r) vector<double> lx(n), ly(n), lr(n); for(int i = 0; i < n; i++){ cin >> lx[i] >> ly[i] >> lr[i]; } // We will create a graph with (n+2) nodes: // node 0 = A // node n+1 = B // nodes [1..n] = streetlights // We'll store adjacency in a vector of vectors (or adjacency list). // Each entry: (neighbor, cost). vector<vector<pair<int,double>>> adj(n+2); // Helper lambda to add edges in both directions. auto add_edge = [&](int u, int v, double w){ // For an undirected setup, add both ways: adj[u].push_back({v, w}); adj[v].push_back({u, w}); }; // 1) Add edges between each pair of streetlights for(int i = 0; i < n; i++){ for(int j = i+1; j < n; j++){ double d = dist(lx[i], ly[i], lx[j], ly[j]); double cost = max(0.0, d - (lr[i] + lr[j])); add_edge(i+1, j+1, cost); } } // 2) Add edges between A (node 0) and each streetlight (node i+1) // cost = max(0, distance(A, center_i) - radius_i) for(int i = 0; i < n; i++){ double d = dist(ax, ay, lx[i], ly[i]); double cost = max(0.0, d - lr[i]); add_edge(0, i+1, cost); } // 3) Add edges between B (node n+1) and each streetlight (node i+1) for(int i = 0; i < n; i++){ double d = dist(bx, by, lx[i], ly[i]); double cost = max(0.0, d - lr[i]); add_edge(n+1, i+1, cost); } // 4) Also consider a direct edge from A to B (in case it's cheaper to go outside all lights). { double d = dist(ax, ay, bx, by); add_edge(0, n+1, d); } // Now we run Dijkstra from node 0 to node n+1. // distance array vector<double> minDark(n+2, numeric_limits<double>::infinity()); minDark[0] = 0.0; // priority queue for Dijkstra: (darkness so far, node) priority_queue<pair<double,int>, vector<pair<double,int>>, greater<pair<double,int>>> pq; pq.push({0.0, 0}); while(!pq.empty()){ auto [currDark, u] = pq.top(); pq.pop(); if(currDark > minDark[u]) { continue; } if(u == n+1) { // We can stop if we want the distance to B immediately break; } // Relax edges for(auto &edge : adj[u]){ int v = edge.first; double w = edge.second; double newDark = currDark + w; if(newDark < minDark[v]){ minDark[v] = newDark; pq.push({newDark, v}); } } } // The answer is minDark[n+1], the minimal "darkness distance" from A to B // Then we floor it as requested. double answer = floor(minDark[n+1]); cout << (long long)answer << "\n"; return 0; }
Nodes:
Edges:
Shortest Path:
We run Dijkstra (since edges can have 0 or positive cost) from node 0 to node . The distance we get is the minimal “dark distance.”
Output:
Finally, we take floor(...)
of the best distance found, and output it.
This solves the problem of minimizing time spent in darkness (equivalently, distance traveled out of coverage), returning the floor of that minimal value.