We need to solve a problem: given an undirected graph with some edges having fixed positive weights (wi > 0) and some edges having weight 0 meaning they are "erased" and we can assign any positive integer weight (1 to 10^18). We need to assign weights to the erased edges so that the shortest path distance between s and t becomes exactly L. If impossible, output NO.
Constraints: n up to 1000, m up to 10000, L up to 1e9. We need to output any valid assignment, with assigned weights not exceeding 1e18 (positive integer). The fixed weights can be up to 1e9.
We need to compute if we can assign weights to zero-edges to make the shortest path exactly L. This is reminiscent of problem "Shortest Path with zero-weight edges" or "assign weights to achieve a given shortest path distance". There's known approach: first, treat all zero edges as weight = 1 (the smallest possible) and compute shortest path. If that shortest path > L, then impossible because increasing weights (making them larger) can only increase distances, but we already have minimal possible distances? Actually making zero edges larger can increase distances, but we need shortest path to be exactly L. If we set all zero edges to 1 (minimum positive integer), we get the minimal possible shortest path distance from s to t, because any higher weight on any zero edge can only increase distances. So if this minimal distance > L, then impossible. Conversely, if we set all zero edges to a huge number (e.g., INF), then shortest path may ignore them and use only fixed edges, but we can also set them to large numbers to increase distance. However we need exactly L, not necessarily minimal. We can adjust weights to increase distances.
But also note that we could set zero edges to high values to force the shortest path to be longer, but the shortest path will always be the minimum over all paths; if we increase zero edges too much, the shortest path might be determined by some other path that doesn't use zero edges. So there is an interplay.
Standard solution: first, replace all zero edges with weight 1, compute the shortest path distance d1. If d1 > L, output NO.
Then, replace all zero edges with weight INF (like a very large number > L, but careful to not overflow), compute shortest path distance d_inf (only using fixed edges because zero edges are large). Actually we want to see the effect of zero edges: we can adjust them to affect distances.
Better approach: We want to assign weights to zero edges such that the distance from s to t becomes exactly L. We can treat zero edges as adjustable.
Observation: The shortest path distance is a function of the assigned weights. For each zero edge, we can assign a weight w in [1, INF]. The overall distance is the min over all s-t paths of the sum of weights (fixed plus assigned). We need to set the weights to achieve exactly L.
We can consider building the graph with zero edges but treat them as "variable". The typical solution: First, run Dijkstra ignoring zero edges? Actually we can use a two-phase approach:
- Set all zero edges to 1, compute the shortest distance from s to t. If it's > L: NO.
- Set all zero edges to 1, then try to increase some zero edges to increase the distance from s to t gradually until it becomes L. But we need to adjust weights so that the new shortest path distance becomes exactly L. Because we have control over each zero weight, we can raise them to increase the length of paths that use them. However, raising a zero edge might affect multiple paths; we need to increase the overall shortest path distance to L.
One common method: Run Dijkstra from s and from t on the graph with all zero edges considered as weight 1 (for distances). Then, for each zero edge (u, v), we can consider it as a candidate to be increased. The idea: we want to set some zero edge's weight to a value such that the shortest path distance becomes L. The shortest path distance after assignment is min( d_fixed, min over zero-edge path adjustments ...). Actually we can think: The shortest path distance will be either a path that uses no zero edges (distance D_fixed, computed by ignoring zero edges) or a path that uses at least one zero edge. Since we can increase weights of zero edges, we can increase the distance of any path containing them.
But we need to assign all zero edges simultaneously. However, we can often set most zero edges to a large value (like INF) and adjust one (or a few) to get the exact distance.
A known solution: Build the graph with all zero edges replaced by weight 1. Compute shortest distances from s to all nodes (dist_s) and from t to all nodes (dist_t) using this graph (with weight 1 for zeros). Then, for each zero edge (u, v), we can compute the potential contribution: if we set its weight to w (>=1), then the distance via that edge would be dist_s[u] + w + dist_t[v] (or dist_s[v] + w + dist_t[u]), considering the shortest paths from s to u and from v to t that may use other zero edges (with weight 1). But careful: If we set some zero edges to larger values, the distances dist_s and dist_t computed with weight 1 may become invalid because those distances themselves might use zero edges. However, we can use the concept: Initially set all zero edges to 1. Then we try to increase the overall shortest path to L. The minimal distance is d_min. We need to increase it to L. The only way to increase the shortest path distance is to increase weights on zero edges that are part of all currently shortest paths. But we can also set some zero edges to a huge number, but that might make the shortest path use a different path.
Alternative known solution from Codeforces problem "F. Shortest Path" maybe? Actually this is Codeforces problem 715B? I recall problem "B. Complete The Graph" from some round? Let's think: There is a problem "Complete The Graph" (CF 715B?) Actually I recall a problem: "D. Complete The Graph" maybe from CF 716? Let's search memory: I think there's Codeforces Round #369 (Div. 2) problem C? Not sure. But there's a known solution: Use Dijkstra twice and assign weights to zero edges based on distances. The idea: first, treat zero edges as weight 1, compute distances from s and t. Then for each zero edge (u,v), assign weight = max(1, L - dist_s[u] - dist_t[v], L - dist_s[v] - dist_t[u]) but also respect that it must be at least 1. However, that might overshoot.
Better: We want to set weights so that the shortest path distance becomes exactly L. One approach: Perform a Dijkstra-like algorithm where we iteratively adjust zero edges to increase the distance. Another approach: Because we can assign arbitrary positive integers, we can set weights to make some specific path have length exactly L, and ensure all other paths are >= L. That's the key: we need to assign weights to zero edges such that there exists at least one s-t path with total length = L, and all other s-t paths have total length >= L. Then the shortest path will be exactly L.
So we can aim to construct a path P that will be exactly L, and set other zero edges to very high values (or adjust them to keep other paths >= L). But we also must ensure that any path that shares some zero edges with P does not become shorter.
But we can set all zero edges that are not on P to a very high number (like INF = L+1 or something) so that any path using them will be > L, unless they also combine with fixed edges to be less? But if we set them too high, any path using them will be > L, so the only candidate for shortest path would be paths that use only fixed edges or use only zero edges from P? Actually we could set all zero edges not on P to a large value, but there may be fixed-edge-only paths with distance less than L. So we must also check that the shortest path using only fixed edges (i.e., ignoring zero edges entirely) is >= L, or if it's less, we need to adjust something? Actually if there is a fixed-edge path (with all edges fixed positive) that already has distance < L, then it's impossible to increase that distance because fixed edges cannot be changed. So that would be a NO. However, we might use zero edges to create a longer path, but the shortest path will still be that fixed path with distance < L, unless we can somehow make that path not a valid path? No, it's still there. So we cannot increase the shortest path distance above the fixed-edge-only shortest path distance because that path remains present regardless of zero edges. Wait: We can assign zero edges to large weights, but that doesn't affect fixed-edge paths. So the overall shortest path distance will always be at most the shortest fixed-edge-only distance (call it D_fixed). Because those fixed-edge paths exist and are unchanged. If D_fixed < L, then the shortest distance will be <= D_fixed < L, so we can never make it exactly L. Unless we can somehow "block" those fixed-edge paths? But we cannot remove edges. So we need D_fixed >= L? Actually not necessarily: if D_fixed < L, we could perhaps set some zero edges to 0? No, they must be positive. Actually if D_fixed < L, then the shortest path distance is at most D_fixed, which is less than L. To achieve L as the shortest distance, we would need to increase the distances of all paths that are shorter than L to be at least L. But we cannot increase fixed-edge paths. So D_fixed must be >= L? But wait, we can also use zero edges to potentially create a path shorter than D_fixed? That would make it even smaller, but we want exactly L. So if D_fixed < L, then the shortest path distance will always be <= D_fixed < L, regardless of zero edge weights (since we cannot increase fixed paths). So the only way to have shortest distance exactly L is if D_fixed >= L, and we can assign zero edges to create a path of length exactly L that becomes the shortest (or tie). But what if D_fixed > L? Then the shortest distance might be > L if we don't use zero edges; we could then add zero edges with appropriate weights to create a path of length L that is shorter than D_fixed. So we need to consider both cases: if D_fixed < L -> impossible. If D_fixed = L -> we just need to assign zero edges such that no new path shorter than L appears (i.e., all zero-edge paths have length >= L). Since we can set zero edges to large values, we can set them all to INF (e.g., L+1) so that any path using zero edges is > L, and fixed paths give exactly L? But if D_fixed = L, then we can set all zero edges to any positive weights as long as they don't create a path shorter than L. We can set them all to a high number. That's easy.
If D_fixed > L, then we need to introduce some zero edges to create a path of length exactly L that becomes the new shortest. And we must ensure that all other paths (including fixed-only) are >= L. Since D_fixed > L, fixed-only paths are > L, so they are okay. But we need to check that we can assign weights to zero edges to create a path exactly L, while not accidentally creating a path shorter than L (but if we assign high weights to zero edges, the paths containing them may be longer; but we might assign some zero edges low weights, which could create a path shorter than L? Actually we want the shortest to be L, so any path that uses zero edges with low weights could be < L, which would be problematic. So we must control all zero-edge paths to be >= L, except perhaps one that we make exactly L. However, we can set zero edges not on our chosen path to large values to avoid short paths.
Thus the problem reduces to: Given graph with fixed positive edges and zero edges, can we assign positive integer weights to zero edges such that:
- The shortest s-t distance = L.
- All fixed edges remain unchanged.
We can treat this as: We need to assign weights to zero edges (positive integers, possibly up to 1e18) to achieve that.
Let's formalize:
Let G = (V, E_fixed ∪ E_zero). For each e in E_fixed, weight w_e > 0 fixed. For each e in E_zero, we choose w_e ∈ ℕ+, w_e ≥ 1.
Define d(s,t) = minimum path length.
Goal: d(s,t) = L.
Constraints: n ≤ 1000, m ≤ 10000.
We can approach by constructing a shortest path tree with zero edges set to 1, then adjust.
Another known solution: Run Dijkstra from s and t on the graph where zero edges are considered as weight 1. Then, consider the graph of zero edges, we can assign them weights such that the distance via each zero edge is at least L? Wait.
I recall a solution:
- First, set all zero edges to 1, compute shortest distance from s to t. If > L, NO.
- Then, for each zero edge, we will set its weight to a large value (like INF) initially. Then we iterate over zero edges, and if the current shortest distance (with current assignments) is still less than L, we can increase the weight of some zero edges that lie on all shortest paths to increase the distance. Actually we can do a process: We'll maintain a graph with assigned weights to zero edges (starting with 1). While the shortest distance d < L, we need to increase d. The only way to increase d is to increase weight(s) on zero edges that are part of all current shortest paths. Because if there is a shortest path that doesn't use a particular zero edge, increasing that edge won't affect the current shortest distance; the distance will stay d until we increase all alternative shortest paths. So we need to identify edges that are "critical" on all shortest paths. But there could be many zero edges. Alternatively, we could do a more direct assignment.
Consider the following:
We can compute the distances from s and t on the graph with all zero edges assigned weight = 1. Let's denote dist_s1[u] and dist_t1[u] (distance to t). Then, for any assignment of zero edges, the true distance from s to u is at least dist_s1[u]? Actually if we increase some zero edges, distances can only increase. So dist_s1[u] is a lower bound. Similarly, dist_t1[u] is lower bound for distance from u to t.
Now, suppose we want to achieve total distance L. The shortest path will be some path. Consider a candidate path that uses a sequence of edges (fixed and zero). Its length = sum_fixed + sum_zero_assigned. Since we can adjust zero edges, we can set the sum of zero edges on that path to exactly some value. But we also need that no other path is shorter.
One approach: Use Dijkstra with "potential" adjustments: set zero edges to 1 initially, compute distances from s (dist_s) and from t (dist_t). Then consider the set of edges (including fixed) that lie on some shortest path of length d_min (with weight 1). We need to increase the length of all s-t paths to at least L. If d_min < L, we must increase the lengths of all paths that are currently < L. Since we can only increase zero edges, we need to ensure that any path that currently has length < L becomes longer, unless we can also increase its length to exactly L? Actually we want the final shortest distance to be L, so we need to make sure all paths have length ≥ L, and at least one has length = L.
Since we start with all zero edges = 1, the minimal distance is d_min. If d_min < L, we need to increase some zero edges to raise the minimal distance to L. The new minimal distance will be determined by the path(s) that become the new shortest after increments.
We can think of it as: we want to assign weights to zero edges to achieve that the distance from s to t is exactly L. This is similar to the problem of "assigning weights to edges with unknown weights to achieve a particular shortest path length" known from Codeforces (I think it's problem 715B). I recall a solution:
- First, replace zero edges with weight 1, run Dijkstra from s and t to get d1[u] and dt[u].
- Then, for each zero edge (u,v), set its weight to max(1, L - d1[u] - dt[v], L - d1[v] - dt[u])? But that would potentially make the shortest path exactly L? Let's test.
If we set w(u,v) = max(1, L - d1[u] - dt[v], L - d1[v] - dt[u]), then consider the path that goes s -> ... -> u -> v -> ... -> t. Its length would be d1[u] + w + dt[v]. If we set w to exactly L - d1[u] - dt[v] (provided that's >=1), then that path length = L. Similarly for the reverse direction. But the distances d1[u] and dt[v] are computed with zero edges =1. However, after we change the weight of (u,v), the distances d1[u] and dt[v] might not be accurate because the shortest path from s to u might itself use the edge (u,v) with the new weight? Actually if we set w higher, the shortest path to u might change. But if we compute d1[u] with all zero edges =1, that's a lower bound. After we increase some zero edges, the true distance to u could be larger. So if we set w based on lower bound, the actual distance via u->v might become larger than intended, potentially > L. We need to ensure that after all assignments, there is a path exactly L. So maybe we need to assign weights in an order such that we don't affect the distances used to compute the needed weight.
Alternative method: Use a "potential" approach: Run Dijkstra twice with zero edges considered as weight 1, but then also run a second phase where we adjust weights to satisfy constraints. Actually, there is a known solution:
- Set all zero weights to 1, compute dist_s and dist_t.
- For each zero edge (u,v), assign weight = max(1, L - dist_s[u] - dist_t[v], L - dist_s[v] - dist_t[u]).
- Then run Dijkstra again with these assigned weights to check if the shortest path equals L. If yes, output; else NO.
I've seen such a solution in editorial for problem "Complete The Graph" from Codeforces Round #715? I'm not entirely sure. Let's test with simple cases.
Consider a graph with two nodes s=0, t=1, and a single zero edge (0,1,0). Then dist_s[0]=0, dist_t[0]=? With weight 1, dist_t[0] would be distance from 0 to t? Actually we need dist_t[v] is distance from v to t. For edge (0,1): compute L - dist_s[0] - dist_t[1] = L - 0 - 0? Since dist_t[1]=0 (distance from 1 to t is 0 if t=1). So that gives L. And max(1, L) = L. So we set weight = L. Then Dijkstra yields distance L. Works.
Consider first sample:
n=5,m=5, L=13, s=0,t=4.
Edges:
0-1:5 (fixed)
2-1:2 (fixed)
3-2:3 (fixed)
1-4:0 (zero)
4-3:4 (fixed)
Compute with zero as 1: dist_s from 0: to 0:0, to1:5, to2: via 1? 0-1 (5) + 1-2? Wait edge 2-1 is fixed 2, so 0->1->2 = 5+2=7, to3: maybe 0->1->2->3 = 5+2+3=10, to4: either 0->1->4 with weight 5+1=6, or 0->1->2->3->4 = 5+2+3+4=14? Actually edge 4-3 is 4, so 10+4=14, so the shortest is 6 via the zero edge weight 1. So d_min = 6.
dist_s: [0,5,7,10,6]? Actually to 4 is 6 (0-1-4 with zero=1). dist_t from t=4: compute distances from 4 with zero=1: 4 to 1: via edge 1-4 zero? Actually edge 1-4 is zero, weight 1, so distance 1; to 0: 1+5=6; to 2: min(4->3->2 =4+3=7, 4->1->2 =1+2=3? Actually 4-1 is 1, 1-2 is 2, so 3; so to2 is 3); to3: 4-3 fixed 4, so 4? Actually 4 to 3 direct is 4, but also 4-1-2-3 is 1+2+3=6, so 4; to1 is 1; to0 is 1+5=6. So dist_t: [6,1,3,4,0].
Now for zero edge (1,4), compute:
L - dist_s[1] - dist_t[4] = 13 -5 -0 =8
L - dist_s[4] - dist_t[1] = 13 -6 -1 =6
max(1,8,6) = 8. So assign weight 8. Then run Dijkstra: distances: from 0 to 4: paths: 0-1-4 =5+8=13; 0-1-2-3-4 =5+2+3+4=14; 0-1-2? not to 4; etc. So shortest =13. Works.
But does this always work? Let's test some counterexamples. Suppose there are multiple zero edges on a path. For instance, a path s -a (zero) -b (zero) -t, and also a fixed edge direct s-t with large weight >L. We need to assign weights to the two zero edges so that the path s-a-b-t has length L, and all other paths are >= L. Our formula would assign weights to each zero edge based on distances from s and t computed with zero=1. Let's simulate.
Graph: s=0, t=3. Edges: (0,1,0), (1,2,0), (2,3,0). No fixed edges. L=100. Compute with zero=1: dist_s: [0,1,2,3]; dist_t: [3,2,1,0]. For each zero edge:
(0,1): max(1, L - dist_s[0] - dist_t[1] = 100 -0 -2 =98, L - dist_s[1] - dist_t[0] =100 -1 -3=96) =98 => weight=98
(1,2): L - dist_s[1] - dist_t[2] =100-1-1=98, L - dist_s[2] - dist_t[1] =100-2-2=96 =>98
(2,3): L - dist_s[2] - dist_t[3] =100-2-0=98, L - dist_s[3] - dist_t[2] =100-3-1=96 =>98
Then path 0-1-2-3 length = 98+98+98 = 294, not 100. So that fails because we assigned each edge independently based on lower bounds, causing overestimation. The shortest path becomes 294 > L. So we need to set them such that total sum = L, but our formula gave each a large weight because it considered the entire path as if each edge is the only zero edge on that path. But with multiple zero edges, the distances dist_s[u] and dist_t[v] computed with zero=1 already include the contributions from other zero edges along the path, so setting each edge to a value that makes the path L would double-count. Indeed, if we set w(0,1)=98, then the distance from s to 1 becomes 98 (since there's no other path?), but dist_s[1] computed earlier was 1. However, after we set w(0,1) to 98, the shortest path from s to 1 might change: it could be 0-1 directly with 98, but maybe there is alternative path? In this graph, only that edge. So dist_s[1] becomes 98. Then for edge (1,2), we based on dist_s[1]=1 (from earlier), but actual dist_s[1] after assigning the first edge is 98, so using earlier lower bound leads to assigning too high for subsequent edges. So the naive assignment fails when there are multiple zero edges on a path.
We need to assign weights such that the total distance along some path equals L, while ensuring no other path shorter. But we cannot just set each zero edge based on initial lower-bound distances because changes propagate.
So we need a smarter method.
One solution: Since we can assign arbitrarily high weights (up to 1e18), we can set all zero edges to a very high value (say, INF = L+1 or 10^18) except for those on a chosen path where we set weights to achieve total L. However, we must be careful that the chosen path is indeed the shortest path after assignment; we need to ensure that no other path becomes shorter than L. Setting other zero edges to high values ensures that any path that uses any zero edge not on the chosen path will be long (≥ INF). But there could be fixed-only paths with distance < L (impossible if D_fixed < L) or fixed-only paths with distance > L but maybe using a mix of fixed edges and some zero edges not on the chosen path that, even with high weights, could be less than L? If INF is huge (like 10^18), then any path that includes a high-weight edge will be huge, so it won't be shorter than L. But there might be paths that use some zero edges on the chosen path partially, but if they deviate, they must include at least one zero edge not on the chosen path? Not necessarily: consider the chosen path uses some zero edges, but there could be another path that uses a subset of those zero edges and some fixed edges to form a shorter route? For example, if the chosen path includes zero edge e1 and e2 in series, there could be an alternative path from s to t that goes s - ... - v where v is some intermediate node on the chosen path, then uses some fixed edges to reach t without using the remaining zero edges. That alternative path might not include any zero edges outside the chosen path, but could be shorter if we assign the zero edges on the chosen path to be small. However, if we set the zero edges on the chosen path to specific values to get total L, we need to ensure that any subpath from s to some node on the path and then fixed edges to t is not shorter than L. In other words, the chosen path's distances from s to each node along the path (with assigned weights) must be such that dist_s[u] along the chosen path is ≤ the true distance from s to u via any other route, else the shortest path might deviate.
Essentially, for the chosen path P (s = v0 - v1 - ... - vk = t) with edges ei (some may be fixed, some zero), we will assign weights to zero edges on P to achieve total length L. For the shortest path to be exactly L and the path P to be a shortest path, we need that for every node v on P, the distance from s to v along P (call d_P(v)) is ≤ the distance from s to v via any other route (including using other zero edges with high weights). Because otherwise, the shortest path to some node would be shorter and then continue to t possibly making overall < L. But we can set other zero edges to INF to prevent shortcuts. However, there might be fixed-only paths that bypass part of P and rejoin later, which could be shorter. We need to check that.
Thus a possible approach: Choose a path P (any s-t path) that uses only fixed edges? But we need to adjust zero edges to achieve L. If D_fixed > L, we need to use zero edges to create a shorter path. If D_fixed = L, we can just set all zero edges to INF. If D_fixed < L, impossible.
But if D_fixed > L, we need to construct a path that includes zero edges, because fixed-only paths are longer than L (we need a path of length exactly L, which is shorter than the fixed-only shortest path). That means we must use some zero edges to reduce the distance below D_fixed? Wait, fixed edges have fixed positive weights; using zero edges (which we can set to any positive integer) might either increase or decrease total length? Actually we can set zero edges to any positive integer, including 1. So by including zero edges we can potentially get a shorter path than fixed-only paths, because zero edges can be set to low weights (like 1). So if D_fixed > L, we need to create a path that is shorter than the fixed-only shortest path. That means we need to include zero edges with relatively low weights to achieve total L (< D_fixed). So we want to set zero edges on some path to be small enough so that the total length equals L, while ensuring that no other path (including fixed-only) becomes < L. Since D_fixed > L, fixed-only paths are > L, so they are safe as long as we don't reduce them (can't). However, other paths that include zero edges might become even shorter than L if we set those zero edges too low. But we can set those other zero edges to large values to prevent them from being short. So we need to choose a specific s-t path P that will become the unique shortest path (or at least one of the shortest) with length L. On P, we assign weights to zero edges; all other zero edges we set to a very large number (say, INF = 10^18). Then we must ensure that the distance along P is exactly L, and that for every node v on P, the distance from s to v along P is indeed the shortest distance to v given the assignments (to avoid alternative routes to nodes on P that then follow the rest of P to get a shorter total). Actually if there is an alternative route to some intermediate node v on P that yields a shorter distance to v, then the overall path s -> (alternative) -> v -> (rest of P) to t could be shorter than L. So we need to ensure that the distances along P are the global shortest distances to those nodes. Since we set all other zero edges to INF, any alternative route that uses any zero edge not on P will be huge, but there could be alternative routes using only fixed edges. So we need to check that for each node v on P, the distance along P from s to v is <= the shortest fixed-only distance from s to v (since any path using fixed edges only cannot include zero edges, so its distance is known). But there might also be mixed paths that use some zero edges on P? Actually if an alternative route uses some zero edges that are on P, that's okay because those are already part of P; but if it uses a subset of P edges in a different order, it might still be composed of edges that are either fixed or zero on P. For example, P might be s-a-b-t with edges: s-a (zero), a-b (fixed), b-t (zero). An alternative path could be s-a (zero) and then a-t (fixed) if exists. This alternative uses the zero edge s-a (on P) but then a different fixed edge to t. To beat L, it would need to have total < L. But we can check distances: along P, dist(s,a)=w_sa, dist(s,b)=w_sa + w_ab, dist(s,t)=w_sa + w_ab + w_bt = L. If there is a fixed edge a-t with weight w_at, then the path s-a-t would have length w_sa + w_at. To ensure that this is not < L, we need w_sa + w_at >= L. Since we control w_sa (zero) and w_bt (zero), we need to set them appropriately. However, w_sa appears also in P. So we have constraints.
Thus simply setting all other zero edges to INF is not sufficient; we must also consider alternative paths that use only fixed edges and possibly some zero edges that are on P (since those are set to some values). Because those zero edges are on P, they are not set to INF; they are set to some specific values. So there could be a path that uses a prefix of P (some zero edges) and then diverges via fixed edges to reach t, bypassing the rest of P. This could be shorter if the fixed part is short. Therefore, to ensure that the overall shortest path is exactly L via P, we need to assign weights to zero edges on P such that for any alternative path from s to t (using any combination of edges), the length is at least L, with equality at least for P.
This is reminiscent of setting distances along P to be the shortest distances from s (like a shortest path tree). In other words, we want to assign weights to zero edges so that the distances from s to all nodes satisfy that the path P is a shortest path to t and distances to intermediate nodes are consistent.
One known approach:
- First, set all zero edges to 1. Compute shortest distances from s and t, call them ds and dt.
- If ds[t] > L: NO.
- Then, we will adjust zero edges gradually to increase the shortest path to L. The idea: while current shortest path distance < L, find all edges that lie on every shortest path? Or we can increase the weight of zero edges on a particular shortest path until the distance increases to L. But increasing one edge might break the property that the path remains the shortest? We can do this:
- Run Dijkstra from s with current weights.
- If dist[t] < L, we need to increase it. Choose a zero edge that is on a shortest path and increase its weight by (L - dist[t])? But careful: if there are multiple shortest paths, increasing one edge might cause the shortest path to switch to another with same length, not increasing overall distance. So we might need to increase multiple edges.
- Instead, we can try to modify the graph by replacing zero edges with weight INF and then run Dijkstra to see the shortest path using only fixed edges (or including zero edges with high weight). But we need to incorporate the ability to set some zero edges to lower values.
Another known solution from Codeforces editorial for problem "715B Complete The Graph" (I recall that problem). Let me recall: The problem statement matches: "ZS the Coder has drawn an undirected graph ..." Actually I think that is Codeforces Round #372? Let's search memory: There is problem "B. Complete The Graph" from Codeforces Round #372? Wait, I recall a problem: "C. Jon Snow and his Favourite Number"? Not that. I recall a problem "Complete The Graph" from Codeforces Round #372 Div.2? No.
Better: I'll search my memory: I've seen an editorial for problem "715B" or "716D"? Actually there is a problem: "D. Complete The Graph" from Codeforces Round #372? Not sure. Let me think: There's a problem "Complete The Graph" from Codeforces Round #715 (Div. 2)? That round had problem "C. The Sports Festival", etc. Not that.
Actually I recall a problem: "B. Complete The Graph" from Codeforces Round #... I think it's from Codeforces Round #372 (Div. 2) but not sure. I can google in my mind: There is an editorial by ZS the Coder. I think it's Codeforces problem 715B. I'll treat it as known.
The editorial solution:
- First, set all zero edges to 1, compute shortest distance d. If d > L: NO.
- Then, for each zero edge, initially set weight = 1.
- While true:
- Run Dijkstra from s, compute distances.
- If dist[t] == L: break.
- If dist[t] > L: NO (but we started with d ≤ L and only increase weights, so dist[t] will only increase; if it overshoots L, impossible? Actually we could overshoot because we might increase too much; but we can control increments.)
- Find all edges that are on some shortest path? Actually they find edges that are "tight"?
I think the editorial uses a method of repeatedly increasing the weight of zero edges on all shortest paths by the same amount until dist[t] reaches L. But we need to ensure that we don't increase too much.
Alternatively, there is known technique:
- Run Dijkstra from s and from t on the graph with zero edges considered as weight 1.
- For each zero edge (u,v), define its "potential" as min(ds[u]+dt[v], ds[v]+dt[u]) + 1? Not exactly.
Let's derive systematically.
We need to assign integer weights ≥1 to zero edges. Let the set of zero edges be Z. For each e in Z, assign w(e) ∈ [1, ∞).
Let d(x,y) be the shortest path distance after assignment.
We need d(s,t) = L.
Observations:
- If we set all zero edges to 1, we get d_min = d1. If d1 > L: impossible.
- If we set all zero edges to a very large number (say INF), we get d_inf, which is the shortest path using only fixed edges (since zero edges are too heavy). If d_inf < L, impossible because even with zero edges huge, the shortest path using only fixed edges is < L, and we cannot increase fixed edges. So we also need d_inf ≤ L? Actually if d_inf < L, impossible because the shortest path distance cannot be increased above d_inf? Wait, setting zero edges to large values might force the shortest path to avoid them, so the distance is min(d_fixed, paths using zero edges). If we set zero edges to large, paths using them become large, so the shortest path will be the shortest path among those that avoid zero edges (i.e., only fixed edges) or that use zero edges but with high weight. So the overall distance d_inf is the shortest path in the graph where zero edges are considered as INF (i.e., effectively not usable). This distance is essentially the shortest path using only fixed edges (because any path using a zero edge would have at least INF weight, which is huge, so not selected if INF > d_fixed). But if we set INF to something like L+1, then it's possible that a path using zero edges might still be shorter than d_fixed if d_fixed is large? Actually we can set INF arbitrarily large, up to 1e18. So we can always set zero edges to such high values that they are never used in shortest path, unless d_fixed is very large (like > 1e18) but L is ≤1e9, so we can set INF > L. So we can effectively make zero edges irrelevant. Thus the minimal possible distance (with zero edges set to 1) is d_min. The maximal possible distance? If we set zero edges to arbitrarily large, the distance will approach d_fixed (the shortest path using only fixed edges) because zero edges become too heavy to be used. However, we cannot decrease d_fixed; so the overall distance after assignment is at least min(d_fixed, something). Actually by setting zero edges large, we can make the shortest path avoid zero edges, so the distance becomes d_fixed. But if we set zero edges to moderate values, the distance might be less than d_fixed. So the achievable distances are from d_min up to something? Actually we can increase zero edges to increase the distance, but the distance cannot exceed d_fixed? Let's think: Suppose we have a path that uses zero edges. By increasing their weights, we can make that path arbitrarily long. But there might be another path that uses only fixed edges with distance d_fixed. So the overall shortest path distance is the minimum over all paths. As we increase zero edge weights, the distance of paths that include them increases, but the distance of fixed-only paths remains constant at d_fixed. So the overall shortest path distance will be at most d_fixed (since fixed-only paths are always available). Therefore, by increasing zero edge weights, we can only increase the shortest path distance up to d_fixed, but cannot exceed d_fixed because the fixed-only paths provide an upper bound. Actually the shortest path distance is the minimum over all paths. The fixed-only paths have distance d_fixed (maybe there are multiple). If we increase zero edge weights, paths that include them become longer, so the minimum might increase, but it can never exceed d_fixed because the fixed-only paths are still present and have distance d_fixed. So the maximum achievable shortest path distance is d_fixed. So we require d_fixed >= L? Actually to achieve distance exactly L, we need L to be between d_min and d_fixed inclusive. If d_min <= L <= d_fixed, then it's possible (maybe). If L > d_fixed, impossible because we cannot make the shortest path longer than d_fixed. If L < d_min, impossible because we cannot make it shorter than d_min. So necessary conditions: d_min <= L <= d_fixed.
But is d_fixed necessarily the shortest path using only fixed edges? Yes, but careful: There could be paths that use zero edges but with assigned weights that are not huge; but if we set zero edges to be very large, those paths become long. So the shortest path distance then becomes the minimum over fixed-only paths. However, there might be no fixed-only path from s to t at all. In that case, d_fixed = ∞ (or very large). Then we can make the distance arbitrarily large by setting zero edges to large values; but also we have an upper bound? Actually if there is no fixed-only path, then the only paths contain at least one zero edge. Then we can increase zero edge weights arbitrarily to make the distance arbitrarily large (up to infinity). So there's no upper bound. In that case, L can be any number >= d_min, provided we can also achieve exactly L. But we need to be able to set weights to get exactly L. That seems possible by adjusting one path.
So the condition is: d_min (with all zero =1) <= L. And also if there exists at least one fixed-only path, then we need L <= d_fixed, otherwise impossible.
But is d_fixed the shortest path using only fixed edges? Yes. However, note: If there is a fixed-only path, then d_fixed is finite. Otherwise, d_fixed = ∞.
But is the condition d_min <= L <= d_fixed sufficient? Not always, because there may be constraints from the structure: we might need to assign integer weights to zero edges, and the distances might be discrete. But since we can assign arbitrarily large integers (up to 1e18) and L is up to 1e9, we likely can adjust.
But consider a case where d_min <= L <= d_fixed, yet it's impossible due to parity or something? I doubt, because weights are arbitrary positive integers, not bounded except by 1e18. We can assign any integer. The distances are sums, so we can achieve any L >= d_min by increasing some zero edges? But we need to exactly hit L, and we must also ensure that after assignment, the shortest path distance is exactly L, not less. That means we must increase the current shortest path distance from d_min to L, while ensuring that no other path becomes shorter than L. As we increase weights, the shortest path distance will increase, but it might jump over L if we increase too much on a critical edge? We can control increments.
The challenge is to assign weights in such a way that the final shortest path is exactly L. Since we have many zero edges, we can tune them.
One constructive method:
- Set all zero edges to 1.
- Compute shortest distance from s to t, call it cur.
- If cur > L: NO.
- If cur == L: output (all zero edges as 1) (or any, but need to ensure that no other path with zero edges set to 1 is shorter? Actually cur is the shortest path distance, so it's min. If cur == L, then it's already L, so we can output the graph with all zero edges = 1, provided that the condition d_fixed >= L holds? Actually if cur == L, it's fine regardless of d_fixed, because the shortest path is L. But wait, if there is a fixed-only path with distance < L, then cur would be < L because the fixed-only path would be considered when zero=1? Actually if there is a fixed-only path with distance < L, then setting zero edges to 1, the shortest path would be min(fixed-only distance, paths with zeros). Since fixed-only distance < L, cur would be < L. So cur == L implies that fixed-only distance >= L, and the shortest path using zero=1 gives L. So it's okay.)
- If cur < L, we need to increase cur to L by increasing weights of some zero edges.
We can increase weights gradually. However, we must be careful not to create a new shortest path that becomes shorter than L due to other routes.
Idea: We can treat zero edges as "flexible". While cur < L, we can find all edges that are "critical" i.e., lie on all current shortest paths from s to t. Increasing the weight of any edge that lies on all shortest paths will increase the shortest path distance (since all shortest paths get longer). However, if there are multiple shortest paths that don't share a particular zero edge, then increasing that edge will not affect those other paths, and the shortest distance remains cur. So we need to increase a set of edges to increase the distance.
We could instead modify the graph by constructing a shortest path DAG (with respect to current distances) and then increase the weight of all zero edges in that DAG? Not exactly.
Alternate approach: Use a method similar to "assign weights to zero edges to make distances exactly L" by first setting all zero edges to 1, then for each zero edge, we compute its "impact" on the shortest path if we increase it. There is a known solution:
- Compute distances from s and to t (with zero edges = 1).
- For each zero edge (u,v), consider the potential shortest path that uses that edge: the distance would be ds[u] + w + dt[v] or ds[v] + w + dt[u] (using current ds, dt computed with weight 1). The overall shortest path distance will be the minimum over all such possibilities and also over fixed-only paths.
- If we set w = max(1, L - ds[u] - dt[v]) and similarly for the reverse, and also ensure that no path becomes < L, maybe we can achieve it.
But earlier we saw that with multiple zero edges on a path, this fails because ds and dt values are interdependent.
But perhaps we can treat the zero edges as a set to be assigned in such a way that the distances ds and dt become something like "the lengths of the shortest path that doesn't use any zero edge with weight >1"? Not clear.
Another viewpoint: Since we can assign arbitrarily large weights to zero edges, we can effectively "disable" them by setting them to a huge number, except for those on a selected path where we set them to appropriate values. So the problem reduces to: Is there a path from s to t (which may include zero edges) such that we can assign positive integer weights to its zero edges (and set all other zero edges to a very large number) to make the path length exactly L, while ensuring that for every other path (including those using some zero edges on the selected path but diverging), the length is at least L? This is like we need to assign distances along the selected path that are "shortest distances" from s to each node on the path and from each node to t.
Specifically, suppose we choose a simple path P from s to t. Let the edges on P be e1, e2, ..., ek. For each edge ei, if it's a fixed edge, its weight w_i is fixed; if it's a zero edge, we can choose weight w_i (≥1). The total length of P must be L. Additionally, for any other path Q from s to t, its length must be ≥ L. This is a set of constraints.
We can try to assign the zero edge weights along P in such a way that the distances along P are the global shortest distances from s to each vertex on P and from each vertex to t. This ensures that any alternative path that reaches a vertex on P earlier cannot give a shorter total to t. More formally, for each vertex v on P, let dist_s(v) be the length from s to v along P, and dist_t(v) be the length from v to t along P. If we ensure that for all vertices v on P, the actual shortest distance from s to v (over all paths) is exactly dist_s(v), and similarly from v to t is dist_t(v), then any s-t path that leaves P at some point and rejoins later would have length at least dist_s(u) + (some detour) + dist_t(v) which would be at least dist_s(u) + dist_t(v) if the detour is non-negative? Actually not exactly. But if the distances from s to all nodes are exactly the distances along P (i.e., P is a shortest path tree from s to t), then any s-t path would have length at least dist_s(t) = L. Because the shortest distance from s to t is L, and P achieves it. So if we can assign weights such that P becomes a shortest path (not necessarily unique) and the distances from s to all nodes are at least those along P (i.e., P is a shortest path), then the condition holds.
Thus we want to assign weights to zero edges (on P and possibly elsewhere) so that the distances from s are as we desire, and P is a shortest path. And we can set all other zero edges to very large values (so they don't create shorter paths). But we also need to ensure that fixed-only paths are not shorter than L. That requires that the shortest fixed-only path distance d_fixed >= L, or if d_fixed < L, impossible as argued.
If d_fixed > L, we need to create a path P that is shorter than d_fixed, so P must include zero edges because fixed edges alone would be longer. By setting zero edges to small values, we can make P's length L < d_fixed. We also need to ensure that no other path using zero edges (including those on P) becomes shorter than L. By setting all other zero edges to huge numbers, paths that use any zero edge not on P will be huge, so not shorter. However, consider paths that use a prefix of P and then deviate using fixed edges to reach t without using the remaining zero edges on P. For such a path to be shorter than L, it would need to have dist_s(u) + d_fixed(u,t) < L, where u is some vertex on P, and d_fixed(u,t) is the shortest fixed-only distance from u to t (since we set other zero edges to huge). But d_fixed(u,t) might be less than dist_t(u) along P. Because along P from u to t, there might be zero edges with assigned weights that could be large; but we want P's length to be L, so dist_t(u) = L - dist_s(u). If there exists a fixed-only path from u to t with length less than dist_t(u), then the combined path s->u along P (dist_s(u)) plus that fixed-only path could be < L. However, note that we are free to assign weights on P; we can choose them to make dist_s(u) and dist_t(u) such that for all u, dist_s(u) + d_fixed(u,t) >= L, and similarly for paths that start with fixed-only from s to some v on P and then follow P from v to t: d_fixed(s,v) + dist_t(v) >= L. And also for paths that combine fixed-only segments both sides. So we have constraints.
Thus, to guarantee that P is a shortest path, we need to ensure that for every vertex u on P, dist_s(u) <= d_fixed(s,u) and dist_t(u) <= d_fixed(u,t) ? Actually if we set other zero edges to huge, the only possible alternative routes are fixed-only paths (since any path using a zero edge not on P will be huge). So the shortest distances from s to any node u will be min(dist_s_along_P(u), d_fixed(s,u)) because a path could go via fixed-only edges entirely, or via P partially, but note that to reach u, one could also go via P and then deviate? But deviating from P to fixed-only later doesn't help because you would have to leave P at some point; that path would consist of P segment from s to some v (where v is before u on P) then fixed-only from v to u. But that fixed-only segment might be shorter than continuing along P. So the shortest distance to u is the minimum over all paths: either entirely fixed, or using P up to some point and then fixed edges, or using P entirely, etc. However, if we set all other zero edges to huge, the only zero edges available are those on P. So any path can use some prefix of P and then switch to fixed edges, but cannot use zero edges outside P. So the graph effectively consists of fixed edges plus the edges of P (which may include zero edges with assigned weights). So we need to assign weights on P such that for all u on P, the shortest distance from s to u is exactly the distance along P (i.e., P is a shortest path tree from s to t, and all nodes on P have distances given by P). This is a strong condition but may be achievable by setting the weights appropriately.
Given that we can choose weights arbitrarily, we could try to set them so that dist_s(u) along P is less than or equal to any alternative fixed-only path from s to u. Since fixed-only paths have fixed weights, we can ensure that by making the P prefix sufficiently short. But we also need the total P length = L. So we have to balance.
Let's denote the fixed-only distances: let D_fixed_s[u] be the shortest distance from s to u using only fixed edges (if no path, infinity). Similarly D_fixed_t[u] from u to t using only fixed edges. These can be computed via Dijkstra on graph with only fixed edges (treat zero edges as absent). Note that these are lower bounds for any path that uses only fixed edges.
Now, consider a chosen path P. For each vertex u on P, let a_u = distance from s to u along P (cumulative). Let b_u = distance from u to t along P = L - a_u. For P to be a shortest path (and to ensure no other path is shorter than L), we need:
- For any u on P, a_u <= D_fixed_s[u]? Actually if there is a fixed-only path from s to u with length < a_u, then the path that goes s->u via fixed-only and then from u to t along P (which is b_u) would have total length D_fixed_s[u] + b_u. This could be less than a_u + b_u = L if D_fixed_s[u] < a_u. So we need D_fixed_s[u] + b_u >= L for all u. Since b_u = L - a_u, this inequality is D_fixed_s[u] + (L - a_u) >= L -> D_fixed_s[u] >= a_u. So indeed we need a_u <= D_fixed_s[u]? Wait rearrangement: D_fixed_s[u] >= a_u. So the distance along P from s to u must be at most the fixed-only distance from s to u. Similarly, for any u on P, consider path that goes s->u along P (a_u) and then from u to t via fixed-only: need a_u + D_fixed_t[u] >= L -> D_fixed_t[u] >= b_u. So we need D_fixed_t[u] >= b_u.
Also, consider paths that are entirely fixed-only: need D_fixed_s[t] >= L. But we already know D_fixed_s[t] = d_fixed. So we need d_fixed >= L (if d_fixed is finite). Actually if d_fixed < L, impossible. So that matches.
Additionally, consider paths that start with fixed-only from s to some u, then follow P from u to t. That's covered by D