using
System;
using
System.Collections.Generic;
public
class
Edge
{
public
int
src;
public
int
nbr;
public
int
wt;
public
Edge(
int
src,
int
nbr,
int
wt)
{
this
.src = src;
this
.nbr = nbr;
this
.wt = wt;
}
}
public
class
Pair : IComparable<Pair>
{
public
int
wsf;
public
string
psf;
public
Pair(
int
wsf,
string
psf)
{
this
.wsf = wsf;
this
.psf = psf;
}
public
int
CompareTo(Pair o)
{
return
this
.wsf - o.wsf;
}
}
public
class
GFG
{
static
SortedSet<Pair> pq =
new
SortedSet<Pair>(
Comparer<Pair>.Create((p1, p2) => p1.wsf.CompareTo(p2.wsf)));
public
static
void
kthLargest(
List<Edge>[] graph,
int
src,
int
dest,
bool
[] visited,
int
k,
string
psf,
int
wsf)
{
if
(src == dest)
{
if
(pq.Count < k)
{
pq.Add(
new
Pair(wsf, psf));
}
else
if
(wsf > pq.Min.wsf)
{
pq.Remove(pq.Min);
pq.Add(
new
Pair(wsf, psf));
}
return
;
}
visited[src] =
true
;
foreach
(Edge e
in
graph[src])
{
if
(!visited[e.nbr])
{
kthLargest(graph, e.nbr, dest, visited, k, psf + e.nbr, wsf + e.wt);
}
}
visited[src] =
false
;
}
public
static
void
addEdges(List<Edge>[] graph)
{
graph[0].Add(
new
Edge(0, 1, 10));
graph[1].Add(
new
Edge(1, 0, 10));
graph[1].Add(
new
Edge(1, 2, 10));
graph[2].Add(
new
Edge(2, 1, 10));
graph[2].Add(
new
Edge(2, 3, 10));
graph[3].Add(
new
Edge(3, 2, 10));
graph[0].Add(
new
Edge(0, 3, 40));
graph[3].Add(
new
Edge(3, 0, 40));
graph[3].Add(
new
Edge(3, 4, 2));
graph[4].Add(
new
Edge(4, 3, 2));
graph[4].Add(
new
Edge(4, 5, 3));
graph[5].Add(
new
Edge(5, 4, 3));
graph[5].Add(
new
Edge(5, 6, 3));
graph[6].Add(
new
Edge(6, 5, 3));
graph[4].Add(
new
Edge(4, 6, 8));
graph[6].Add(
new
Edge(6, 4, 8));
}
public
static
void
kthLargestPathUtil(
int
N,
int
M,
int
src,
int
dest,
int
k)
{
List<Edge>[] graph =
new
List<Edge>[2 * N];
for
(
int
i = 0; i < 2 * N; i++)
{
graph[i] =
new
List<Edge>();
}
addEdges(graph);
bool
[] visited =
new
bool
[N];
kthLargest(graph, src, dest, visited, k, src +
""
, 0);
string
path = pq.Min.psf;
foreach
(
char
c
in
path)
{
Console.Write(c +
" "
);
}
}
public
static
void
Main()
{
int
N = 7, M = 8;
int
src = 0;
int
dest = 6;
int
k = 3;
kthLargestPathUtil(N, M, src, dest, k);
}
}