254492026-02-20 10:17:36KevinKaktusz túra (45 pont)cpp17Time limit exceeded 0/45592ms64000 KiB
#include <bits/stdc++.h>
using namespace std;
using ll = long long;

struct Edge {
    int to;
    ll weight;
    int id;
};

vector<vector<Edge>> adj;
vector<ll> edge_weights;
vector<int> vis; // 0: unvisited, 1: visiting, 2: visited
vector<int> parent_node;
vector<int> parent_edge_id;
ll total_dist = 0;

void find_cycles(int u, int p, int p_edge) {
    vis[u] = 1;
    parent_node[u] = p;
    parent_edge_id[u] = p_edge;

    for (auto& e : adj[u]) {
        if (e.id == p_edge) continue;
        if (vis[e.to] == 1) { // Kör talált
            // A kör élei (beleértve a visszafelé mutatót is) csak egyszer számítanak
            total_dist -= e.weight;
            edge_weights[e.id] = -e.weight; // "Megtakarítás" kalkulációhoz negatív
            int curr = u;
            while (curr != e.to) {
                int eid = parent_edge_id[curr];
                total_dist -= edge_weights[eid];
                edge_weights[eid] = -edge_weights[eid];
                curr = parent_node[curr];
            }
        } else if (vis[e.to] == 0) {
            find_cycles(e.to, u, e.id);
        }
    }
    vis[u] = 2;
}

int main() {
    ios::sync_with_stdio(false); cin.tie(0);
    int n, m; cin >> n >> m;
    adj.resize(n + 1);
    edge_weights.resize(m);
    vis.assign(n + 1, 0);
    parent_node.assign(n + 1, 0);
    parent_edge_id.assign(n + 1, -1);

    for (int i = 0; i < m; i++) {
        int u, v; ll w;
        cin >> u >> v >> w;
        adj[u].push_back({v, w, i});
        adj[v].push_back({u, w, i});
        edge_weights[i] = w;
        total_dist += 2 * w; // Alapeset: minden él oda-vissza
    }

    find_cycles(1, 0, -1);

    // Leghosszabb "megtakarítás" keresése Dijkstra-val (mivel nincsenek pozitív körök)
    vector<ll> dist(n + 1, -1e18);
    priority_queue<pair<ll, int>> pq;
    dist[1] = 0;
    pq.push({0, 1});
    ll max_savings = 0;

    while (!pq.empty()) {
        ll d = pq.top().first;
        int u = pq.top().second;
        pq.pop();

        if (d < dist[u]) continue;
        max_savings = max(max_savings, d);

        for (auto& e : adj[u]) {
            ll w = edge_weights[e.id];
            if (dist[e.to] < d + w) {
                dist[e.to] = d + w;
                pq.push({dist[e.to], e.to});
            }
        }
    }

    cout << total_dist - max_savings << endl;
    return 0;
}
SubtaskSumTestVerdictTimeMemory
base0/45
1Time limit exceeded0/0578ms33148 KiB
2Runtime error0/0389ms64000 KiB
3Time limit exceeded0/2578ms33160 KiB
4Runtime error0/2312ms64000 KiB
5Runtime error0/2270ms64000 KiB
6Time limit exceeded0/2587ms33420 KiB
7Time limit exceeded0/2591ms33152 KiB
8Runtime error0/2395ms64000 KiB
9Runtime error0/2400ms64000 KiB
10Time limit exceeded0/2592ms33400 KiB
11Runtime error0/2497ms64000 KiB
12Runtime error0/3402ms64000 KiB
13Runtime error0/3377ms64000 KiB
14Runtime error0/3291ms64000 KiB
15Runtime error0/3412ms64000 KiB
16Time limit exceeded0/3587ms35708 KiB
17Runtime error0/3393ms64000 KiB
18Time limit exceeded0/3582ms35708 KiB
19Runtime error0/3363ms64000 KiB
20Runtime error0/3421ms64000 KiB