/ SeriousOJ /

Record Detail

Runtime Error


  
# Status Time Cost Memory Cost
#1 Runtime Error 2ms 576.0 KiB
#2 Runtime Error 2ms 540.0 KiB

Code

#include <bits/stdc++.h>
using namespace std;
#define SC               scanf
#define PF               printf
#define ull              unsigned long long
#define ld               long double
#define F                first
#define S                second
#define pb               push_back
#define sort_a(a)        sort(a.begin(),a.end());
#define sort_d(a)        sort(a.rbegin(),a.rend());
#define READ(f)          freopen(f, "r", stdin)
#define WRITE(f)         freopen(f, "w", stdout)
#define rev(s)           reverse(s.begin(),s.end())
#define P(ok)            cout << (ok ? "YES\n": "NO\n")
#define __Heart__              ios_base :: sync_with_stdio(false); cin.tie(NULL);
#define ll long long
typedef pair< ll , ll>                   PII;
const int MX = 2e5 + 5 ;
ll par[MX], cnt, cost;
struct heart
{
    ll u, v, w , f , s;

};
bool compare(heart a, heart b)
{

    return a.w > b.w ;

}
vector < heart > edge ;
int find_par(ll id)
{
    return (par[id] == id)? id : par[id] = find_par(par[id]) ;
}
ll mst(ll n)
{
    sort(edge.begin(), edge.end(),compare) ;
    cost = 0, cnt = 0 ;
    for(int i = 0 ; i <= n ; i++)
    {
        par[i] = i ;
    }
    for(int i = 0 ; i< edge.size() ; i++)
    {

        ll u = find_par(edge[i].u) ;
        ll v = find_par(edge[i].v) ;
        if(u != v)
        {
            par[u] = v ;
            cost +=edge[i].w ;

        }
    }
    return cost ;
}
void solve()
{
    ll n, d ;
    cin >> n >> d ;
    ll arr[n + 5][d + 5] ;
    for(int i = 0 ; i < n ; i++)
    {
        for(int j = 0 ; j < d ; j++)
        {
            cin >> arr[i][j] ;
        }
    }
    heart dp[25] ;
    for(int i = 0 ; i < 35 ; i++)
    {
        dp[i].f = INT_MIN ;
        dp[i].s = -1 ;
    }
    for(int i = 0 ; i < n ; i++)
    {
        for(int j = 0 ; j < (1 << d) ; j++)
        {
            ll cur_cost = 0 ;
            for(int k = 0 ; k < d ; k++)
            {
                ll bit = (j & (1 << k)) ;
                cur_cost +=  (bit ?  arr[i][k] : -arr[i][k]) ;
            }
            if(cur_cost > dp[j].f)
            {
                dp[j].f = cur_cost ;
                dp[j].s = i ;
            }
        }
    }
    for(int i = 0 ; i < n ; i++)
    {
        for(int j = 0 ; j < (1 << d) ; j++)
        {
            ll cur_cost = 0 ;
            for(int k = 0 ; k < d ; k++)
            {
                int bit = (j & (1 << k)) ;
               cur_cost +=  (bit ?  arr[i][k] : -arr[i][k]) ;
            }
            ll cur = abs(cur_cost - dp[j].f) ;
            if(cur) edge.pb({i , dp[j].s ,cur ,0,0}) ;

        }
    }
   // for(auto it : edge) cout << it.u << " " << it.v << " " << it.w << endl ;

    cout << mst(n) << endl ;

}
int main()
{
     __Heart__
     //READ("input9.txt") ;
     //WRITE("output9.txt") ;
     int t ; t = 1 ; while(t--) solve() ;
}

Information

Submit By
Type
Submission
Problem
P1098 KuZ the Maximum spanning tree
Language
C++20 (G++ 13.2.0)
Submit At
2024-09-07 12:58:36
Judged At
2024-11-11 02:55:11
Judged By
Score
0
Total Time
2ms
Peak Memory
576.0 KiB