using
System;
using
System.Collections.Generic;
using
System.Linq;
namespace
GFG {
class
Program
{
static
int
v;
static
Dictionary<
int
, List<Tuple<
int
,
int
> > > adj;
static
Queue<
int
> q;
static
int
[] distance;
static
bool
[] is_in_queue;
static
int
source = 0;
static
List<
int
> desopo(
int
[, ] graph)
{
v = graph.GetLength(0);
adj =
new
Dictionary<
int
,
List<Tuple<
int
,
int
> > >();
for
(
int
i = 0; i < v; i++) {
for
(
int
j = i + 1; j < v; j++) {
if
(graph[i, j] != 0) {
if
(adj.ContainsKey(i)) {
adj[i].Add(
new
Tuple<
int
,
int
>(
graph[i, j], j));
}
else
{
adj.Add(
i,
new
List<Tuple<
int
,
int
> >());
adj[i].Add(
new
Tuple<
int
,
int
>(
graph[i, j], j));
}
if
(adj.ContainsKey(j)) {
adj[j].Add(
new
Tuple<
int
,
int
>(
graph[i, j], i));
}
else
{
adj.Add(
j,
new
List<Tuple<
int
,
int
> >());
adj[j].Add(
new
Tuple<
int
,
int
>(
graph[i, j], i));
}
}
}
}
q =
new
Queue<
int
>();
distance = Enumerable.Repeat(Int32.MaxValue, v)
.ToArray();
is_in_queue =
new
bool
[v];
source = 0;
distance = 0;
q.Enqueue(source);
is_in_queue =
true
;
while
(q.Count != 0)
{
int
u = q.Dequeue();
is_in_queue[u] =
false
;
foreach
(
var
e
in
adj[u])
{
if
(distance[e.Item2]
> distance[u] + e.Item1) {
distance[e.Item2]
= distance[u] + e.Item1;
if
(!is_in_queue[e.Item2])
{
if
(distance[e.Item2]
== Int32.MaxValue)
q.Enqueue(e.Item2);
else
q.Enqueue(e.Item2);
is_in_queue[e.Item2] =
true
;
}
}
}
}
return
distance.ToList();
}
static
void
Main(
string
[] args)
{
int
[, ] graph = { { 0, 4, 0, 0, 8 },
{ 0, 0, 8, 0, 11 },
{ 0, 8, 0, 2, 0 },
{ 0, 0, 2, 0, 1 },
{ 8, 11, 0, 1, 0 } };
foreach
(
var
i
in
desopo(graph))
{
Console.Write(i +
" "
);
}
}
}
}