255172026-02-20 13:19:40GervidKaktusz túra (45 pont)cpp17Wrong answer 17/4541ms2640 KiB
#include <iostream>
#include <vector>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <limits.h>
#include <algorithm>
#include <math.h>
#include <array>

using namespace std;

vector<vector<array<int, 2>>> g; //g[node] -> neighbours: neighbour[i] -> {id, weight}
set<array<int, 2>> cycleEdges; //smaller, bigger
vector<bool> visited;

int cyclefind(int node, int parent) {
	if (visited[node]) { //just refound this node => cycle found
		cycleEdges.insert({ min(node, parent), max(node, parent) });
		return node;
	}

	visited[node] = true;
	int imcycle = -1;
	for (array<int, 2> neighbour : g[node]) {
		if (parent == neighbour[0] || cycleEdges.count({min(node, neighbour[0]), max(node, neighbour[0])})) continue;
		int temp = cyclefind(neighbour[0], node);
		if (temp != -1) imcycle = temp;
	}
	if (imcycle != -1 && node != imcycle) { //one child is part of a cycle, so I will be too
		cycleEdges.insert({ min(node, parent), max(node, parent) });
		return imcycle;
	}
	return -1;
}

int main()
{
	iostream::sync_with_stdio(0);
	cin.tie(0);

	int n, m, sumcost = 0;
	cin >> n >> m;
	g.resize(n);
	for (int i = 0; i < m; i++) {
		int a, b, c;
		cin >> a >> b >> c;
		a--, b--;
		g[a].push_back({ b, c });
		g[b].push_back({ a, c });
	}
	visited.resize(n, false);
	cyclefind(0, -1);

	for (int node = 0; node < n; node++) {
		for (array<int, 2> neighbour : g[node]) {
			if (!cycleEdges.count({ node, neighbour[0] })) { //normal edges counted twice, cycle edges only ones
				sumcost += neighbour[1];
			}
		}
	}

	priority_queue < array<int, 2>, vector<array<int, 2>>> pq;
	pq.push({ 0, 0 });
	vector<int> saves(n, INT_MIN);
	int maxsave = 0;
	saves[0] = 0;
	while (!pq.empty()) {
		int node = pq.top()[1], s = pq.top()[0];
		pq.pop();
		visited[node] = false;
		for (array<int, 2> neighbour : g[node]) {
			if (visited[neighbour[0]]) {
				saves[neighbour[0]] = s + neighbour[1] * (cycleEdges.count({ min(node, neighbour[0]), max(node, neighbour[0]) }) ? -1 : 1);
				pq.push({ saves[neighbour[0]], neighbour[0] });
				maxsave = max(maxsave, saves[neighbour[0]]);
			}
		}
	}
	cout << sumcost - maxsave;
}
//5 5
//1 2 1
//2 3 1
//3 4 1
//2 4 1
//3 5 2
SubtaskSumTestVerdictTimeMemory
base17/45
1Accepted0/02ms316 KiB
2Wrong answer0/041ms2604 KiB
3Accepted2/21ms500 KiB
4Accepted2/22ms508 KiB
5Wrong answer0/21ms316 KiB
6Accepted2/21ms316 KiB
7Accepted2/21ms316 KiB
8Wrong answer0/22ms316 KiB
9Wrong answer0/22ms364 KiB
10Wrong answer0/22ms316 KiB
11Wrong answer0/22ms316 KiB
12Accepted3/314ms1648 KiB
13Wrong answer0/317ms1588 KiB
14Wrong answer0/317ms1748 KiB
15Accepted3/339ms2528 KiB
16Accepted3/332ms2612 KiB
17Wrong answer0/339ms2432 KiB
18Wrong answer0/334ms2640 KiB
19Wrong answer0/337ms2616 KiB
20Wrong answer0/339ms2484 KiB