Ergashov Muxriddin 111-20 guruh
/
using System;
//minimal kengayuvchi daraxt nomlar maydoni
class Program
{
public static readonly int M = 999999; // erishib bo'lmaydigan masofani ko'rsatadi
static int[,] map = new int[,] {
{ 0,81,63,74,27,69 },
{ 81,0,41, 24,73,77},
{ 63,41,0,79,75,86 },
{ 74,24,79,0,18,33 },
{ 27,73,75,18,0,38},
{69,77,86,33,38,0 }
};
public static readonly int N = (int)Math.Sqrt(map.Length);
static bool[] visit = new bool[N];
static int[] closest = new int[N];
static int[] lowcost = new int[N];
static readonly int start = 0;
static void Main(string[] args)
{
kruska();
string str1 = "Eng yaqin qiymat:";
string str2 = "Arzon narxdagi qiymat:";
int sum = 0;
for (int i = 0; i < N; i++)
{
str1 += closest[i] + " ";
str2 += lowcost[i] + " ";
sum += lowcost[i];
}
Console.WriteLine(str1);
Console.WriteLine(str2);
Console.WriteLine("Yig'indi:" + sum);
Console.ReadKey();
}
static void kruska()
{
visit[start] = true;
for (int i = 0; i < N; i++)
{
if (i != start)
{
lowcost[i] = map[start, i]; // Og'irliklarni boshqa qirralardan boshlang
closest[i] = start; // Eng yaqin o'zgarishning boshlanish nuqtasini belgilang
visit[i] = false; // Boshlashdan boshqa elementlarni ishga tushirish U to'plamiga tegishli emas
}
else
lowcost[i] = 0;
}
for (int i = 0; i < N; i++)
{
int temp = M;
int t = start;
for (int j = 0; j < N; j++) // V-U to'plamida U to'plamiga eng yaqin t cho'qqisini toping
{
if ((!visit[j]) && (lowcost[j] < temp))
{
t = j;
temp = lowcost[j];
}
}
if (t == start)
break;
visit[t] = true;
for (int j = 0; j < N; j++)
{
if ((!visit[j]) && (map[t, j] < lowcost[j]))
{
lowcost[j] = map[t, j];
closest[j] = t;
}
}
}
}
}
Dostları ilə paylaş: |