255052026-02-20 12:44:44GervidKaktusz túra (45 pont)cpp17Wrong answer 0/4528ms2668 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 = false;
	for (array<int, 2> neighbour : g[node]) {
		if (parent == neighbour[0]) continue;
		imcycle |= cyclefind(neighbour[0], node);
	}
	if (imcycle) { //one child is part of a cycle, so I will be too
		cycleEdges.insert({ min(node, parent), max(node, parent) });
		if (node != imcycle) 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_MAX);
	int maxsave = 0;
	saves[0] = 0;
	while (!pq.empty()) {
		int node = pq.top()[1], s = pq.top()[0];
		pq.pop();
		for (array<int, 2> neighbour : g[node]) {
			if (saves[neighbour[0]] < s + neighbour[1]) {
				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;
}
SubtaskSumTestVerdictTimeMemory
base0/45
1Accepted0/01ms316 KiB
2Wrong answer0/028ms2608 KiB
3Wrong answer0/21ms512 KiB
4Wrong answer0/21ms316 KiB
5Wrong answer0/21ms316 KiB
6Wrong answer0/21ms316 KiB
7Wrong answer0/21ms316 KiB
8Wrong answer0/21ms388 KiB
9Wrong answer0/21ms316 KiB
10Wrong answer0/22ms320 KiB
11Wrong answer0/22ms316 KiB
12Wrong answer0/317ms2044 KiB
13Wrong answer0/318ms2128 KiB
14Wrong answer0/318ms2188 KiB
15Wrong answer0/327ms2612 KiB
16Wrong answer0/320ms2356 KiB
17Wrong answer0/328ms2612 KiB
18Wrong answer0/324ms2668 KiB
19Wrong answer0/328ms2612 KiB
20Wrong answer0/327ms2652 KiB