CSES - Datatähti 2022 alku - Results
Submission details
Task:Tietoverkko
Sender:mbezirgan
Submission time:2021-10-17 11:23:42 +0300
Language:C++ (C++17)
Status:READY
Result:0
Feedback
groupverdictscore
#10
#20
#30
Test results
testverdicttimegroup
#10.01 s1, 2, 3details
#20.01 s2, 3details
#30.01 s3details

Compiler report

input/code.cpp: In function 'int main()':
input/code.cpp:43:38: warning: large integer implicitly truncated to unsigned type [-Woverflow]
   computers[i] = Computer(33333333333);
                                      ^

Code

#include <iostream>
#include <string>
#include <vector>
#include <memory>

struct Connection;

struct Computer
{
	Computer() = default;
	Computer(uint32_t n)
	{
		// 1 is useless as it's its self, but is kept so that searching a connections is easy.
		computerConnections = new uint32_t[n];
	}

	uint32_t* computerConnections;
};

struct Connection
{
	Computer* a;
	Computer* b;
	uint64_t speed;
};

int main()
{
	uint64_t n;
	std::cin >> n;

	Computer* computers = new Computer[n];

	bool* checked = new bool[n]();

	uint64_t totalSpeed = 0;

	// For this to work the fastest way possible the input should be put in order

	// Initialize computers
	for (uint32_t i = 0; i < n; i++)
	{
		computers[i] = Computer(33333333333);
	}

	// First input
	{
		uint32_t a, b;
		std::cin >> a;
		std::cin >> b;

		uint32_t speed;
		std::cin >> speed;

		totalSpeed += speed;

		Computer* c1 = a - 1 + computers;
		Computer* c2 = b - 1 + computers;

		c1->computerConnections[b - 1] = speed;
		c2->computerConnections[a - 1] = speed;

		checked[a - 1] = true;
		checked[b - 1] = true;
	}

	for (uint32_t i = 1; i < n - 1; i++)
	{
		uint32_t a, b;
		std::cin >> a;
		std::cin >> b;

		uint32_t speed;
		std::cin >> speed;

		totalSpeed += speed;

		Computer* c1 = a - 1 + computers;
		Computer* c2 = b - 1 + computers;

		c1->computerConnections[b - 1] = speed;
		c2->computerConnections[a - 1] = speed;

		Computer* cA= checked[c1 - computers] ? c1 : c2;
		Computer* cB= checked[c1 - computers] ? c2 : c1;

		// Loop through all known computers
		for (uint32_t j = 0; j < i + 1; j++)
		{
			if (&computers[j] == cA)
				continue;

			uint32_t lowestSpeed = computers[j].computerConnections[cA - computers];
			lowestSpeed = lowestSpeed > speed ? speed : lowestSpeed;

			computers[j].computerConnections[cB - computers] = lowestSpeed;
			cB->computerConnections[j] = lowestSpeed;

			totalSpeed += lowestSpeed;
		}

		checked[cB - computers] = true;
	}

	std::cout << totalSpeed;

	delete[] computers;
}

Test details

Test 1

Group: 1, 2, 3

Verdict:

input
100
1 2 74
1 3 100
2 4 50
3 5 40
...

correct output
88687

user output
(empty)

Error:
terminate called after throwing an instance of 'std::bad_alloc'
  what():  std::bad_alloc

Test 2

Group: 2, 3

Verdict:

input
5000
1 2 613084013
1 3 832364259
2 4 411999902
3 5 989696303
...

correct output
1103702320243776

user output
(empty)

Error:
terminate called after throwing an instance of 'std::bad_alloc'
  what():  std::bad_alloc

Test 3

Group: 3

Verdict:

input
200000
1 2 613084013
1 3 832364259
2 4 411999902
3 5 989696303
...

correct output
1080549209850010931

user output
(empty)

Error:
terminate called after throwing an instance of 'std::bad_alloc'
  what():  std::bad_alloc