using
System;
using
System.Collections;
using
System.Collections.Generic;
class
GFG
{
static
List<List<
int
>> graph=
new
List<List<
int
>>();
static
Dictionary<Tuple<
int
,
int
>,
int
> cost=
new
Dictionary<Tuple<
int
,
int
>,
int
>();
static
List<
int
> uniform_cost_search(List<
int
> goal,
int
start)
{
List<
int
> answer=
new
List<
int
>();
List<Tuple<
int
,
int
> > queue =
new
List<Tuple<
int
,
int
> >();
for
(
int
i = 0; i < goal.Count; i++)
answer.Add(
int
.MaxValue);
queue.Add(
new
Tuple<
int
,
int
>(0, start));
Dictionary<
int
,
int
> visited=
new
Dictionary<
int
,
int
>();
int
count = 0;
while
(queue.Count > 0) {
Tuple<
int
,
int
> q = queue[0];
Tuple<
int
,
int
> p =
new
Tuple<
int
,
int
>(-q.Item1,q.Item2);
queue.RemoveAt(0);
if
(goal.Contains(p.Item2)) {
int
index = goal.IndexOf(p.Item2);
if
(answer[index] ==
int
.MaxValue)
count++;
if
(answer[index] > p.Item1)
answer[index] = p.Item1;
queue.RemoveAt(0);
if
(count == goal.Count)
return
answer;
}
if
(!visited.ContainsKey(p.Item2))
for
(
int
i = 0; i < graph[p.Item2].Count; i++) {
queue.Add(
new
Tuple<
int
,
int
>((p.Item1 + (cost.ContainsKey(
new
Tuple<
int
,
int
>(p.Item2, graph[p.Item2][i])) ? cost[
new
Tuple<
int
,
int
>(p.Item2, graph[p.Item2][i])] : 0))*-1,
graph[p.Item2][i]));
}
visited[p.Item2] = 1;
}
return
answer;
}
public
static
void
Main(
params
string
[]args)
{
graph=
new
List<List<
int
>>();
for
(
int
i=0;i<7;i++)
{
graph.Add(
new
List<
int
>());
}
graph[0].Add(1);
graph[0].Add(3);
graph[3].Add(1);
graph[3].Add(6);
graph[3].Add(4);
graph[1].Add(6);
graph[4].Add(2);
graph[4].Add(5);
graph[2].Add(1);
graph[5].Add(2);
graph[5].Add(6);
graph[6].Add(4);
cost[
new
Tuple<
int
,
int
>(0, 1)] = 2;
cost[
new
Tuple<
int
,
int
>(0, 3)] = 5;
cost[
new
Tuple<
int
,
int
>(1, 6)] = 1;
cost[
new
Tuple<
int
,
int
>(3, 1)] = 5;
cost[
new
Tuple<
int
,
int
>(3, 6)] = 6;
cost[
new
Tuple<
int
,
int
>(3, 4)] = 2;
cost[
new
Tuple<
int
,
int
>(2, 1)] = 4;
cost[
new
Tuple<
int
,
int
>(4, 2)] = 4;
cost[
new
Tuple<
int
,
int
>(4, 5)] = 3;
cost[
new
Tuple<
int
,
int
>(5, 2)] = 6;
cost[
new
Tuple<
int
,
int
>(5, 6)] = 3;
cost[
new
Tuple<
int
,
int
>(6, 4)] = 7;
List<
int
> goal=
new
List<
int
>();
goal.Add(6);
List<
int
> answer = uniform_cost_search(goal, 0);
Console.Write(
"Minimum cost from 0 to 6 is = "
+ answer[0]);
}
}