/ SeriousOJ /

Record Detail

Accepted


  
# Status Time Cost Memory Cost
#1 Accepted 1ms 324.0 KiB
#2 Accepted 2ms 484.0 KiB
#3 Accepted 398ms 48.809 MiB
#4 Accepted 397ms 48.855 MiB
#5 Accepted 401ms 48.785 MiB
#6 Accepted 401ms 48.691 MiB
#7 Accepted 403ms 48.695 MiB
#8 Accepted 398ms 48.707 MiB
#9 Accepted 400ms 48.684 MiB
#10 Accepted 405ms 48.758 MiB

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
P1097 KuZ the Maximum spanning tree (Easy Version)
Language
C++20 (G++ 13.2.0)
Submit At
2024-09-07 12:31:00
Judged At
2024-11-11 02:55:31
Judged By
Score
100
Total Time
405ms
Peak Memory
48.855 MiB