Skip to content

Commit d50d673

Browse files
author
V-KSSHAM\Administrator
committed
Add library and road algorithm
1 parent 70c06ad commit d50d673

File tree

1 file changed

+65
-0
lines changed

1 file changed

+65
-0
lines changed

LibraryAndRoad.cs

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
using System;
2+
using System.Diagnostics;
3+
4+
class Solution
5+
{
6+
static void Main(String[] args)
7+
{
8+
int q = Convert.ToInt32(Console.ReadLine());
9+
for (int a0 = 0; a0 < q; a0++)
10+
{
11+
string[] tokens_n = Console.ReadLine().Split(' ');
12+
long n = Convert.ToInt64(tokens_n[0]);
13+
long m = Convert.ToInt64(tokens_n[1]);
14+
long x = Convert.ToInt64(tokens_n[2]);
15+
long y = Convert.ToInt64(tokens_n[3]);
16+
17+
var paths = new bool[n][];
18+
for (int i = 0; i < n; i++)
19+
{
20+
paths[i] = new bool[n];
21+
}
22+
23+
for (int a1 = 0; a1 < m; a1++)
24+
{
25+
string[] tokens_city_1 = Console.ReadLine().Split(' ');
26+
int city_1 = Convert.ToInt32(tokens_city_1[0]);
27+
int city_2 = Convert.ToInt32(tokens_city_1[1]);
28+
paths[city_1 - 1][city_2 - 1] = true;
29+
paths[city_2 - 1][city_1 - 1] = true;
30+
}
31+
32+
if (x <= y)
33+
{
34+
Console.WriteLine(x * n);
35+
36+
continue;
37+
}
38+
39+
var hasPathToLibrary = new bool[n];
40+
long sum = 0;
41+
for (int i = 0; i < n; i++)
42+
{
43+
if (!hasPathToLibrary[i])
44+
{
45+
// build library
46+
hasPathToLibrary[i] = true;
47+
sum = sum + x;
48+
for (int j = 0; j < n; j++)
49+
{
50+
if (paths[i][j])
51+
{
52+
// build the road
53+
hasPathToLibrary[j] = true;
54+
sum += y;
55+
}
56+
}
57+
}
58+
}
59+
60+
Console.WriteLine(sum);
61+
}
62+
63+
Console.ReadKey();
64+
}
65+
}

0 commit comments

Comments
 (0)