255142026-02-20 13:14:20GervidKaktusz túra (45 pont)cpp17Wrong answer 7/4541ms2776 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]]) {
				pq.push({ s + neighbour[1] * (cycleEdges.count({min(node, neighbour[0]), max(node, neighbour[0])}) ? -1 : 1), neighbour[0] });
				saves[neighbour[0]] = s + neighbour[1];
				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
base7/45
1Wrong answer0/02ms316 KiB
2Wrong answer0/039ms2536 KiB
3Accepted2/21ms552 KiB
4Accepted2/22ms316 KiB
5Wrong answer0/22ms316 KiB
6Wrong answer0/21ms316 KiB
7Wrong answer0/22ms316 KiB
8Wrong answer0/21ms316 KiB
9Wrong answer0/22ms316 KiB
10Wrong answer0/21ms316 KiB
11Wrong answer0/22ms316 KiB
12Accepted3/313ms1640 KiB
13Wrong answer0/317ms1704 KiB
14Wrong answer0/317ms1756 KiB
15Wrong answer0/341ms2556 KiB
16Wrong answer0/332ms2368 KiB
17Wrong answer0/339ms2616 KiB
18Wrong answer0/334ms2776 KiB
19Wrong answer0/339ms2588 KiB
20Wrong answer0/339ms2612 KiB