CSES - Datatähti 2022 alku - Results
Submission details
Task:Tietoverkko
Sender:mbezirgan
Submission time:2021-10-17 00:45:47 +0300
Language:C++ (C++17)
Status:READY
Result:0
Feedback
groupverdictscore
#10
#20
#30
Test results
testverdicttimegroup
#10.38 s1, 2, 3details
#20.39 s2, 3details
#30.62 s3details

Code

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

struct Connection;

struct Computer
{
	std::vector<Connection*> connections;
};

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

struct FunctionData
{
	const Computer* computer;
	Computer* computers;
	uint64_t* speed;
	bool* checked;
	Connection* connection;
	Computer* distr;
};

void FindAllPaths(uint64_t lowestSpeed, FunctionData &data)
{
	if (data.checked[data.computer - data.computers])
	{
		if (data.distr->connections.size() == 1)
		{
			return;
		}
	}

	for (auto& c : data.distr->connections)
	{
		if (c != data.connection)
		{

			uint64_t loopLowestSpeed = lowestSpeed;
			if (loopLowestSpeed > c->speed)
				loopLowestSpeed = c->speed;

			if (&data.computers[c->a] != data.distr)
			{
				data.distr = &data.computers[c->a];
				data.connection = c;
				FindAllPaths(loopLowestSpeed, data);
				if (!data.checked[c->a])
				{
					*data.speed += loopLowestSpeed;
				}
			}
			else
			{
				data.distr = &data.computers[c->b];
				data.connection = c;
				FindAllPaths(loopLowestSpeed, data);
				if (!data.checked[c->b])
				{
					*data.speed += loopLowestSpeed;
				}
			}
		}
	}

	if (data.computer == data.distr)
	{
		data.checked[data.computer - data.computers] = true;
	}
}

// Generates all connection lengths (can be easily made to also save for what computers they are)
inline void FindConnetionsSpeed(uint64_t n, Computer* computers, uint64_t* speed)
{
	bool* checked = new bool[n]();

	FunctionData fd;

	for (uint64_t i = 0; i < n - 1; i++)
	{
		fd = { &computers[i], computers, speed, checked, nullptr, &computers[i] };
		FindAllPaths(UINT32_MAX, fd);
	}

	delete[] checked;
}

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

	Computer* computers = new Computer[n]();
	Connection* connections = new Connection[n - 1];

	for (uint64_t i = 0; i < n - 1; i++)
	{
		auto& c = connections[i];
		uint64_t a, b;
		std::cin >> a;
		std::cin >> b;

		c.a = a - 1;
		c.b = b - 1;

		computers[c.a].connections.push_back(&c);
		computers[c.b].connections.push_back(&c);

		std::cin >> c.speed;
	}

	uint64_t speed = 0;
	FindConnetionsSpeed(n, computers, &speed);

	std::cout << speed;

	delete[] connections;
	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)

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)

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)