1 solutions
-
0_MJiH_ LV 4 MOD @ 2024-12-12 23:32:38
#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() ; }
- 1
Information
- ID
- 1097
- Difficulty
- 6
- Category
- DP | Graph_Theory Click to Show
- Tags
- # Submissions
- 25
- Accepted
- 10
- Accepted Ratio
- 40%
- Uploaded By