Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- namespace ConsoleApp1
- {
- class Program
- {
- public static void Main(string[] args)
- {
- int n, m;
- string? input = Console.ReadLine();
- if (input == null) return;
- n = Convert.ToInt32(input.Split(" ")[0]);
- m = Convert.ToInt32(input.Split(" ")[1]);
- List<List<Tuple<int, int>>> gov = new List<List<Tuple<int, int>>>(n);
- for (int i = 0; i < n; i++)
- {
- gov.Add(new List<Tuple<int, int>>());
- }
- int s;
- for (int i = 0; i < m; i++)
- {
- int e, c;
- input = Console.ReadLine();
- if (input == null) return;
- s = Convert.ToInt32(input.Split(" ")[0]);
- e = Convert.ToInt32(input.Split(" ")[1]);
- c = Convert.ToInt32(input.Split(" ")[2]);
- s--;
- e--;
- gov[s].Add(new Tuple<int, int>(e, c));
- gov[e].Add(new Tuple<int, int>(s, c));
- }
- s = 0;
- List<int> d = new List<int>(n);
- List<bool> u = new List<bool>(n);
- for (int i = 0; i < n; i++)
- {
- d.Add(1000000000);
- u.Add(false);
- }
- d[s] = 0;
- for (int i = 0; i < n; ++i)
- {
- int v = -1;
- for (int j = 0; j < n; ++j)
- if (!u[j] && (v == -1 || d[j] < d[v]))
- v = j;
- if (d[v] == 1000000000)
- break;
- u[v] = true;
- for (int j = 0; j < gov[v].Count; ++j)
- {
- int toGov = gov[v][j].Item1,
- length = gov[v][j].Item2;
- if (d[v] + length < d[toGov])
- {
- d[toGov] = d[v] + length;
- }
- }
- }
- int answer = 0;
- for (int i = 0; i < n; i++)
- answer += d[i];
- Console.WriteLine(answer);
- }
- }
- }
Add Comment
Please, Sign In to add comment