/ SeriousOJ /

Record Detail

Memory Exceeded


  
# Status Time Cost Memory Cost
#1 Accepted 1ms 332.0 KiB
#2 Accepted 2ms 592.0 KiB
#3 Accepted 327ms 50.836 MiB
#4 Accepted 327ms 50.863 MiB
#5 Accepted 326ms 50.852 MiB
#6 Accepted 324ms 50.852 MiB
#7 Accepted 323ms 50.863 MiB
#8 Accepted 326ms 50.863 MiB
#9 Accepted 326ms 50.906 MiB
#10 Memory Exceeded ≥1102ms ≥1.0 GiB
#11 Memory Exceeded ≥1092ms ≥1.0 GiB

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 ;

};
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]) ;
}
int mst(ll n)
{
    sort(edge.begin(), edge.end(),compare) ;
    cost = 0, cnt = 0 ;
    for(int i = 1; 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 ;
            cnt ++ ;
            cost +=edge[i].w ;
        }
    }
    return cost ;
}
void solve()
{
    ll n, d ;
    cin >> n >> d ;
    ll arr[n + 5][d + 5] ;
    for(int i = 1 ; i <= n ; i++)
    {
        for(int j = 1 ; j <= d ; j++)
        {
            cin >> arr[i][j] ;
        }
    }
    for(int i = 1 ; i <= n ; i++)
    {
        for(int j = i + 1 ; j <= n ; j++)
        {
            ll cur_cost = 0 ;
            for(int k = 1 ; k <= d ; k++)
            {
                cur_cost += abs(arr[i][k] - arr[j][k]) ;
            }
            edge.pb({i, j, cur_cost}) ;
        }
    }
   // for(auto it : edge) cout << it.u << " "  << it.v << " " << it.w << endl ;
    cout << mst(n) << endl ;

}
int main()
{
     __Heart__
     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 23:11:50
Judged At
2024-12-03 04:53:27
Judged By
Score
33
Total Time
≥1102ms
Peak Memory
≥1.0 GiB