Submission #35134209


Source Code Expand

#include <bits/stdc++.h>



using namespace std;


// #pragma gcc optimize("ofast")
// #pragma gcc target("avx,avx2,fma")


#define int long long

#define all(x) (x).begin(), (x).end()


#define pb push_back
#define endl '\n'
#define fi first
#define se second

// const int mod = 1e9 + 7;

const int mod=998'244'353;


const long long INF = 2e18 + 10;
// const int INF=4e9+10;

#define readv(x, n)   \
    vector<int> x(n); \
    for (auto &i : x) \
        cin >> i;



template <typename t>
    using v = vector<t>;

template <typename t>
    using vv = vector<vector<t>>;

template <typename t>
    using vvv = vector<vector<vector<t>>>;

typedef vector<int> vi;
typedef vector<double> vd;
typedef vector<vector<int>> vvi;
typedef vector<vector<vector<int>>> vvvi;
typedef vector<vector<vector<vector<int>>>> vvvvi;
typedef vector<vector<double>> vvd;

typedef pair<int, int> pii;



int multiply(int a, int b, int in_mod) { return (int)(1ll * a * b % in_mod); }
int mult_identity(int a) { return 1; }



const double pi = acosl(-1);



auto power(auto a, auto b, const int in_mod)
{

    auto prod = mult_identity(a);
    auto mult = a % in_mod;
    while (b != 0)
    {
        if (b % 2)
        {
            prod = multiply(prod, mult, in_mod);
        }
        if(b/2)
        mult = multiply(mult, mult, in_mod);
        b /= 2;
    }

    return prod;
}
auto mod_inv(auto q, const int in_mod)
{
    return power(q, in_mod - 2, in_mod);
}


mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());



#define stp cout << fixed << setprecision(20);



struct DSU {
    std::vector<int> f, siz;
    DSU(int n) : f(n), siz(n, 1) { std::iota(f.begin(), f.end(), 0); }
    int leader(int x) {
        while (x != f[x]) x = f[x] = f[f[x]];
        return x;
    }
    bool same(int x, int y) { return leader(x) == leader(y); }
    bool merge(int x, int y) {
        x = leader(x);
        y = leader(y);

        if (x == y) return false;

        if(siz[x]< siz[y])
            swap(x,y);

        siz[x] += siz[y];
        f[y] = x;
        return true;
    }
    int size(int x) { return siz[leader(x)]; }
};

void solv(){
    int n, m;
    cin>>n>>m;

    readv(x,n);
    readv(y,n);
    x.insert(x.begin(),0);
    y.insert(y.begin(),0);

    v<tuple<int, int, int>> edges;
    for(int i = 0;i<m;i++){
        int a, b, z;
        cin>>a>>b>>z;
        edges.emplace_back(z, a, b);
    }
    sort(all(edges));
    DSU uf(n+ 1);


    vi mn(n + 1);
    for(int i =1 ;i<=n;i++)
        mn[i] = min(x[i], y[i]);
    multiset<int> mn_diff;

    for(int i =1 ;i<=n;i++){
        mn_diff.insert(max(x[i],y[i]));
    }
    int mn_sm = 0;
    for(auto i:mn)
        mn_sm += i;

    int x_sm = accumulate(all(x), (int)0);
    int y_sm = accumulate(all(y), (int)0);
    int  ans = INF;


    ans = mn_sm + *mn_diff.begin();
    ans = min(ans, x_sm);
    ans = min(ans, y_sm);


    int running_cost = 0;
    int comps = n;
    if(comps == 1)
        ans = min(ans, (int)0);
    for(auto [z, a, b]:edges){

        a = uf.leader(a);
        b = uf.leader(b);
        if(a == b)
            continue;
        comps--;


        mn_sm -= mn[a];
        mn_sm -= mn[b];

        mn_diff.erase(mn_diff.find(max(x[a] , y[a])));
        mn_diff.erase(mn_diff.find(max(x[b] , y[b])));


        int new_x = min(x[a], x[b]);
        int new_y = min(y[a], y[b]);
        x_sm -= x[a];
        y_sm -= y[a];
        
        x_sm -= x[b];
        y_sm -= y[b];

        uf.merge(a,b);
        a = uf.leader(a);
        mn[a] = min(new_y, new_x);
        x[a] = new_x;
        y[a] = new_y;
        x_sm += x[a];
        y_sm += y[a];
        mn_diff.insert(max(x[a] , y[a]));

        running_cost += z;

        mn_sm += mn[a];

        // mn_diff.erase(mn_diff.find(abs(x[a] - y[a])));

        int cst = running_cost + mn_sm;
        if(mn_diff.size())
            cst += *mn_diff.begin();
        ans = min(ans, cst);

        ans = min(ans, running_cost + x_sm);
        ans = min(ans, running_cost + y_sm);

        if(comps == 1){
            ans = min(ans, running_cost);
        }





    }
    cout<<ans<<endl;
}

void solve()
{

    int t = 1;
    // cin>>t;
    for(int T=1;T<=t;T++)
    {
        // cout<<"Case #"<<T<<": ";
        solv();
    }
}




signed main()
{

    // freopen("input.txt", "r", stdin);
    // freopen("output.txt", "w", stdout);


    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cerr.tie(NULL);

    auto clk = clock();
    // -------------------------------------Code starts here---------------------------------------------------------------------

    signed t = 1;
    // cin >> t;

    for (signed test = 1; test <= t; test++)
    {
        // cout<<"Case #"<<test<<": ";

        solve();
    }

    // -------------------------------------Code ends here------------------------------------------------------------------

    clk = clock() - clk;

    #ifndef ONLINE_JUDGE
    cerr << fixed << setprecision(6) << "\nTime: " << ((float)clk) / CLOCKS_PER_SEC << "\n";
    #endif
    return 0;
}

/*
000100

1000100

1 0 -1 -2 -1 -2 -3
*/

Submission Info

Submission Time
Task F - Transportation
User Wernier
Language C++ (GCC 9.2.1)
Score 0
Code Size 5377 Byte
Status WA
Exec Time 406 ms
Memory 25044 KiB

Compile Error

./Main.cpp: In function ‘long long int mult_identity(long long int)’:
./Main.cpp:58:23: warning: unused parameter ‘a’ [-Wunused-parameter]
   58 | int mult_identity(int a) { return 1; }
      |                       ^
./Main.cpp: At global scope:
./Main.cpp:66:12: warning: use of ‘auto’ in parameter declaration only available with ‘-fconcepts’
   66 | auto power(auto a, auto b, const int in_mod)
      |            ^~~~
./Main.cpp:66:20: warning: use of ‘auto’ in parameter declaration only available with ‘-fconcepts’
   66 | auto power(auto a, auto b, const int in_mod)
      |                    ^~~~
./Main.cpp:84:14: warning: use of ‘auto’ in parameter declaration only available with ‘-fconcepts’
   84 | auto mod_inv(auto q, const int in_mod)
      |              ^~~~

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 0 / 500
Status
AC × 3
AC × 20
WA × 21
Set Name Test Cases
Sample example_00.txt, example_01.txt, example_02.txt
All example_00.txt, example_01.txt, example_02.txt, hand_00.txt, hand_01.txt, hand_02.txt, hand_03.txt, hand_04.txt, hand_05.txt, hand_06.txt, hand_07.txt, random_00.txt, random_01.txt, random_02.txt, random_03.txt, random_04.txt, random_05.txt, random_06.txt, random_07.txt, random_08.txt, random_09.txt, random_10.txt, random_11.txt, random_12.txt, random_13.txt, random_14.txt, random_15.txt, random_16.txt, random_17.txt, random_18.txt, random_19.txt, random_20.txt, random_21.txt, random_22.txt, random_23.txt, random_24.txt, random_25.txt, random_26.txt, random_27.txt, random_28.txt, random_29.txt
Case Name Status Exec Time Memory
example_00.txt AC 6 ms 3576 KiB
example_01.txt AC 2 ms 3516 KiB
example_02.txt AC 2 ms 3472 KiB
hand_00.txt AC 240 ms 25000 KiB
hand_01.txt AC 236 ms 25036 KiB
hand_02.txt AC 233 ms 24980 KiB
hand_03.txt AC 91 ms 20376 KiB
hand_04.txt AC 234 ms 24980 KiB
hand_05.txt AC 233 ms 24988 KiB
hand_06.txt AC 236 ms 24980 KiB
hand_07.txt AC 245 ms 24904 KiB
random_00.txt WA 300 ms 24996 KiB
random_01.txt WA 139 ms 21328 KiB
random_02.txt AC 310 ms 24924 KiB
random_03.txt WA 138 ms 21004 KiB
random_04.txt WA 305 ms 24980 KiB
random_05.txt AC 153 ms 21312 KiB
random_06.txt WA 312 ms 24996 KiB
random_07.txt WA 143 ms 20936 KiB
random_08.txt AC 302 ms 25032 KiB
random_09.txt WA 169 ms 20996 KiB
random_10.txt WA 392 ms 24900 KiB
random_11.txt AC 170 ms 20936 KiB
random_12.txt WA 395 ms 24996 KiB
random_13.txt WA 180 ms 21004 KiB
random_14.txt AC 398 ms 24984 KiB
random_15.txt WA 171 ms 21060 KiB
random_16.txt WA 406 ms 24996 KiB
random_17.txt AC 200 ms 21248 KiB
random_18.txt WA 357 ms 25044 KiB
random_19.txt WA 157 ms 20948 KiB
random_20.txt AC 367 ms 24936 KiB
random_21.txt WA 158 ms 20952 KiB
random_22.txt WA 354 ms 24960 KiB
random_23.txt AC 165 ms 20952 KiB
random_24.txt WA 366 ms 24956 KiB
random_25.txt WA 160 ms 20996 KiB
random_26.txt AC 362 ms 24904 KiB
random_27.txt WA 172 ms 20952 KiB
random_28.txt WA 360 ms 24956 KiB
random_29.txt WA 165 ms 20980 KiB