Submission #684004

# Submission time Handle Problem Language Result Execution time Memory
684004 2023-01-20T02:31:22 Z Ninja_Kunai Training (IOI07_training) C++17
30 / 100
112 ms 15944 KB
/**
*    Author :  Nguyen Tuan Vu
*    Created : 20.01.2023
**/

#pragma GCC optimize("O2")
#pragma GCC target("avx,avx2,fma")
#include<bits/stdc++.h>
#define MASK(x) ((1ll)<<(x))
#define BIT(x, i) (((x)>>(i))&(1))
#define ALL(v)  (v).begin(), (v).end()
#define REP(i, n)  for (int i = 0, _n = (n); i < _n; ++i)
#define FOR(i, a, b)  for (int i = (a), _b = (b); i <= _b; ++i) 
#define FORD(i, b, a)  for (int i = (b), _a = (a); i >= _a; --i)
#define db(val) "["#val" = "<<(val)<<"] "
#define TIME  (1.0 * clock() / CLOCKS_PER_SEC)

template <class X, class Y> bool minimize(X &a, Y b) {
    if (a > b) return a = b, true;
    return false;
}
template <class X, class Y> bool maximize(X &a, Y b) {
    if (a < b) return a = b, true;
    return false;
}

using namespace std;

mt19937 jdg(chrono::steady_clock::now().time_since_epoch().count());
int Rand(int l, int r) {return l + jdg() % (r - l + 1);}

void file(){
    #define TASK "TASK"
    if(fopen(TASK".inp", "r")) {
        freopen(TASK".inp", "r", stdin);
        freopen(TASK".out", "w", stdout);
    }
}

#define int long long
const int N = 1e3 + 5;
const int K = 11;
int n, m, par[N], high[N], dp[N][MASK(K)];
vector <int> adj[N];
vector <array <int, 3>> edges;
vector <array <int, 3>> cmd[N];

void dfs(int u) {
    for (auto v : adj[u]) if (par[u] != v) {
        par[v] = u;
        high[v] = high[u] + 1;

        dfs(v);
    }
}

int LCA(int u, int v) {
    if (high[u] < high[v]) swap(u, v);
    while (high[u] > high[v]) u = par[u];

    while (u != v) {
        u = par[u];
        v = par[v];
    }

    return u;
}

int getID(int u, int v) {
    return lower_bound(ALL(adj[u]), v) - adj[u].begin();
}

pair <int, int> jump(int st, int en) {
    int mask = MASK(K) - 1;
    int totalSum = 0;

    while (1) {
        //cout << en << '\n';
        totalSum += dp[en][mask];
        mask = (MASK(K) - 1) ^ MASK(getID(par[en], en));
        if (par[en] == st) break;
        en = par[en];
    }

    return make_pair(MASK(getID(par[en], en)), totalSum);
}

void _dfs(int u) {
    //cout << u << ' ';
    for (auto v : adj[u]) if (par[u] != v) _dfs(v);
    //return;
    vector <pair <int, int>> done;
    for (auto x : cmd[u]) if (u == x[0] || u == x[1]) {
        done.push_back(jump(u, u ^ x[0] ^ x[1]));
        done.back().second += x[2];
    }
    else {
        pair <int, int> Left = jump(u, x[0]);
        pair <int, int> Right = jump(u, x[1]);

        done.push_back({Left.first | Right.first, Left.second + Right.second + x[2]});
    }

    for (auto v : adj[u]) if (par[u] != v) done.push_back({MASK(getID(u, v)), dp[v][MASK(K) - 1]});
    REP(mask, MASK(K)) {
        REP(i, K) if (BIT(mask, i)) maximize(dp[u][mask], dp[u][mask ^ MASK(i)]);

        for (auto x : done) if ((mask | x.first) == mask) {
            maximize(dp[u][mask], dp[u][mask ^ x.first] + x.second);
        }
    }

    /*REP(mask, MASK(K)) if (dp[u][mask]) {
        cout << u << ' ' << mask << ' ' << dp[u][mask] << '\n';
    }

    cout << '\n';*/
}

main()
{
    ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
    file();
    cin >> n >> m;
    long long totalWeight = 0;

    FOR(i, 1, m) {
        int u, v, w; cin >> u >> v >> w;
        if (w == 0) {
            adj[u].push_back(v);
            adj[v].push_back(u);
        }
        else {
            edges.push_back({u, v, w});
            totalWeight += w;
        }
    }

    dfs(1);
    //return 0;
    for (auto x : edges) if (high[x[0]] % 2 == high[x[1]] % 2) {
        //cout << x[0] << ' ' << x[1] << ' ' << x[2] << ' ' << LCA(x[0], x[1]) << '\n';
        cmd[LCA(x[0], x[1])].push_back({x[0], x[1], x[2]});
    }

    //pair <int, int> tmp = jump(1, 3);
    //cout << tmp.first << ' ' << tmp.second << '\n';
    //return 0;
    _dfs(1);
    cout << totalWeight - dp[1][MASK(K) - 1];
    cerr << "Time elapsed: " << TIME << " s.\n";
    return 0;
}

/*
==================================================+
INPUT:                                            |
--------------------------------------------------|

--------------------------------------------------|
==================================================+
OUTPUT:                                           |
--------------------------------------------------|

--------------------------------------------------|
==================================================+
*/

Compilation message

training.cpp:120:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
  120 | main()
      | ^~~~
training.cpp: In function 'void file()':
training.cpp:35:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   35 |         freopen(TASK".inp", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
training.cpp:36:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   36 |         freopen(TASK".out", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 3 ms 468 KB Output is correct
2 Correct 3 ms 504 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 1108 KB Output is correct
2 Correct 6 ms 1108 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 103 ms 15668 KB Output is correct
2 Correct 112 ms 15944 KB Output is correct
# Verdict Execution time Memory Grader output
1 Incorrect 2 ms 468 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 3 ms 468 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 10 ms 896 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 25 ms 1548 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 43 ms 2548 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 91 ms 4940 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 44 ms 844 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 87 ms 1828 KB Output isn't correct
2 Halted 0 ms 0 KB -