Untitled

mail@pastecode.io avatar
unknown
c_cpp
12 days ago
898 B
1
Indexable
Never
#include <bits/stdc++.h>

using i64 = long long;

int main() {
    std::ios::sync_with_stdio(false);
    std::cin.tie(nullptr);
    
    int n;
    std::cin >> n;
    
    std::vector<std::vector<std::pair<int, int>>> adj(n);
    for (int i = 0; i < n; i++) {
        int a, b, c;
        std::cin >> a >> b >> c;
        a--, b--;
        adj[a].push_back({b, 0});
        adj[b].push_back({a, c});
    }
    
    int ans = 1E9;
    for (auto [y, w] : adj[0]) {
        int x = 0;
        int sum = 0;
        for (int i = 0; i < n; i++) {
            for (auto [b, c] : adj[x]) {
                if (b != y) {
                    y = b;
                    sum += c;
                    break;
                }
            }
            std::swap(x, y);
        }
        ans = std::min(ans, sum);
    }
    std::cout << ans << "\n";
    
    return 0;
}
Leave a Comment