using
System;
using
System.Collections.Generic;
using
System.Linq;
public
class
Solution
{
public
int
Dijkstra(List<List<Tuple<
int
,
int
>>> adj,
int
src,
int
dest,
int
n)
{
PriorityQueue<Tuple<
int
,
int
>> pq =
new
PriorityQueue<Tuple<
int
,
int
>>((x, y) => x.Item1.CompareTo(y.Item1));
int
[] dist =
new
int
[n];
Array.Fill(dist,
int
.MaxValue);
dist[src] = 0;
pq.Enqueue(
new
Tuple<
int
,
int
>(0, src));
while
(pq.Count > 0)
{
Tuple<
int
,
int
> tuple = pq.Dequeue();
int
d = tuple.Item1;
int
node = tuple.Item2;
foreach
(Tuple<
int
,
int
> neighbor
in
adj[node])
{
int
adjNode = neighbor.Item1;
int
adjWeight = neighbor.Item2;
if
(dist[adjNode] > d + adjWeight)
{
dist[adjNode] = d + adjWeight;
pq.Enqueue(
new
Tuple<
int
,
int
>(dist[adjNode], adjNode));
}
}
}
return
dist[dest];
}
public
List<List<
int
>> ModifyEdges(
int
n, List<List<
int
>> edges,
int
src,
int
dest,
int
target)
{
List<List<Tuple<
int
,
int
>>> adj =
new
List<List<Tuple<
int
,
int
>>>();
foreach
(
var
_
in
Enumerable.Range(0, n))
{
adj.Add(
new
List<Tuple<
int
,
int
>>());
}
foreach
(
var
edge
in
edges)
{
int
weight = edge[2];
if
(weight != -1)
{
int
u = edge[0];
int
v = edge[1];
adj[u].Add(
new
Tuple<
int
,
int
>(v, weight));
adj[v].Add(
new
Tuple<
int
,
int
>(u, weight));
}
}
int
shortestPath = Dijkstra(adj, src, dest, n);
if
(shortestPath < target)
{
return
new
List<List<
int
>>();
}
if
(shortestPath == target)
{
foreach
(
var
edge
in
edges)
{
int
weight = edge[2];
if
(weight == -1)
{
edge[2] =
int
.MaxValue;
}
}
return
edges;
}
foreach
(
var
edge
in
edges)
{
int
weight = edge[2];
if
(weight == -1)
{
edge[2] = 1;
int
u = edge[0];
int
v = edge[1];
adj[u].Add(
new
Tuple<
int
,
int
>(v, 1));
adj[v].Add(
new
Tuple<
int
,
int
>(u, 1));
int
newShortestPath = Dijkstra(adj, src, dest, n);
if
(newShortestPath <= target)
{
edge[2] += (target - newShortestPath);
foreach
(
var
edge2
in
edges)
{
int
weight2 = edge2[2];
if
(weight2 == -1)
{
edge2[2] =
int
.MaxValue;
}
}
return
edges;
}
}
}
return
new
List<List<
int
>>();
}
static
void
Main(
string
[] args)
{
Solution obj =
new
Solution();
int
n = 4;
List<List<
int
>> edges =
new
List<List<
int
>> {
new
List<
int
> { 1, 0, 4 },
new
List<
int
> { 1, 2, 3 },
new
List<
int
> { 2, 3, 5 },
new
List<
int
> { 0, 3, -1 } };
int
src = 0;
int
dest = 2;
int
target = 6;
List<List<
int
>> result = obj.ModifyEdges(n, edges, src, dest, target);
if
(result.Count == 0)
{
Console.WriteLine(
"No valid modification found to achieve the target distance."
);
}
else
{
Console.WriteLine(
"Modified Edges:"
);
foreach
(
var
edge
in
result)
{
Console.WriteLine($
"{edge[0]} -> {edge[1]} : {edge[2]}"
);
}
}
}
}
public
class
PriorityQueue<T>
{
private
readonly
List<T> _heap;
private
readonly
Comparison<T> _compare;
public
PriorityQueue(Comparison<T> compare)
{
_heap =
new
List<T>();
_compare = compare;
}
public
int
Count => _heap.Count;
public
void
Enqueue(T item)
{
_heap.Add(item);
int
i = _heap.Count - 1;
while
(i > 0)
{
int
parent = (i - 1) / 2;
if
(_compare(_heap[i], _heap[parent]) >= 0)
break
;
Swap(i, parent);
i = parent;
}
}
public
T Dequeue()
{
int
n = _heap.Count - 1;
T result = _heap[0];
_heap[0] = _heap[n];
_heap.RemoveAt(n);
n--;
int
i = 0;
while
(
true
)
{
int
left = 2 * i + 1;
int
right = 2 * i + 2;
if
(left > n)
break
;
int
next = left;
if
(right <= n && _compare(_heap[left], _heap[right]) > 0)
next = right;
if
(_compare(_heap[next], _heap[i]) >= 0)
break
;
Swap(i, next);
i = next;
}
return
result;
}
private
void
Swap(
int
i,
int
j)
{
T temp = _heap[i];
_heap[i] = _heap[j];
_heap[j] = temp;
}
}