Artem_Chepurov

Ex4 Tink Back

Jun 29th, 2022 (edited)
178
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.20 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3.  
  4. namespace ConsoleApp1
  5. {
  6.     class Program
  7.     {
  8.         public static void Main(string[] args)
  9.         {
  10.             int n, m;
  11.             string? input = Console.ReadLine();
  12.             if (input == null) return;
  13.             n = Convert.ToInt32(input.Split(" ")[0]);
  14.             m = Convert.ToInt32(input.Split(" ")[1]);
  15.             List<List<Tuple<int, int>>> gov = new List<List<Tuple<int, int>>>(n);
  16.             for (int i = 0; i < n; i++)
  17.             {
  18.                 gov.Add(new List<Tuple<int, int>>());
  19.             }
  20.             int s;
  21.             for (int i = 0; i < m; i++)
  22.             {
  23.                 int e, c;
  24.                 input = Console.ReadLine();
  25.                 if (input == null) return;
  26.                 s = Convert.ToInt32(input.Split(" ")[0]);
  27.                 e = Convert.ToInt32(input.Split(" ")[1]);
  28.                 c = Convert.ToInt32(input.Split(" ")[2]);
  29.                 s--;
  30.                 e--;
  31.                 gov[s].Add(new Tuple<int, int>(e, c));
  32.                 gov[e].Add(new Tuple<int, int>(s, c));
  33.             }
  34.             s = 0;
  35.  
  36.             List<int> d = new List<int>(n);
  37.             List<bool> u = new List<bool>(n);
  38.             for (int i = 0; i < n; i++)
  39.             {
  40.                 d.Add(1000000000);
  41.                 u.Add(false);
  42.             }
  43.             d[s] = 0;
  44.             for (int i = 0; i < n; ++i)
  45.             {
  46.                 int v = -1;
  47.                 for (int j = 0; j < n; ++j)
  48.                     if (!u[j] && (v == -1 || d[j] < d[v]))
  49.                         v = j;
  50.                 if (d[v] == 1000000000)
  51.                     break;
  52.                 u[v] = true;
  53.  
  54.                 for (int j = 0; j < gov[v].Count; ++j)
  55.                 {
  56.                     int toGov = gov[v][j].Item1,
  57.                         length = gov[v][j].Item2;
  58.                     if (d[v] + length < d[toGov])
  59.                     {
  60.                         d[toGov] = d[v] + length;
  61.                     }
  62.                 }
  63.             }
  64.             int answer = 0;
  65.             for (int i = 0; i < n; i++)
  66.                 answer += d[i];
  67.             Console.WriteLine(answer);
  68.         }
  69.     }
  70. }
Add Comment
Please, Sign In to add comment