Advertisement
SuitNdtie

Pipe

May 11th, 2019
172
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.20 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<algorithm>
  3. using namespace std;
  4. typedef long long int ll;
  5. ll const inf = 1e18;
  6. struct pos{
  7.     ll I;
  8.     ll J;
  9. };/*
  10. ll abs(ll x){
  11.     return (x < 0 ? x * -1 : x);
  12. }*/
  13. ll distM(pos a,pos b){
  14.     return abs(a.I - b.I) + abs(a.J - b.J);
  15. }
  16. int main()
  17. {
  18.     int n,k;
  19.     scanf("%d %d",&n,&k);
  20.     pos home[n];
  21.     ll dist[n];
  22.     bool visited[n];
  23.     int myset[n];
  24.     for(int i = 0 ; i < n ; i ++){
  25.         scanf("%lld %lld",&home[i].I,&home[i].J);
  26.         dist[i] = inf;
  27.         visited[i] = false;
  28.         myset[i] = i;
  29.     }
  30.     dist[0] = 0;
  31.     ll MST = 0;
  32.     int t = 0;
  33.     ll edge[n];
  34.     int bound = n - 1;
  35.     while(true){
  36.         //find key
  37.         ll mindist = inf + 1, u = -1;
  38.         int swappos;
  39.         for(int i = 0 ; i <= bound ; i ++){
  40.             if(dist[myset[i]] < mindist){
  41.                 mindist = dist[myset[i]];;
  42.                 u = myset[i];
  43.                 swappos = i;
  44.             }
  45.         }
  46.        
  47.         if(u == -1)break;
  48.         swap(myset[swappos],myset[bound]);
  49.         bound--;
  50.         MST += mindist;
  51.         edge[t++] = mindist;
  52.         for(int i = 0 ; i <= bound ; i ++){
  53.             ll w = distM(home[u],home[myset[i]]);
  54.             if(w < dist[myset[i]]){
  55.                 dist[myset[i]] = w;
  56.             }
  57.         }
  58.     }
  59.     sort(edge,edge+t,greater<ll>());
  60.     for(int i = 0 ; i < k - 1 ; i  ++){
  61.         MST -= edge[i];
  62.     }
  63.     printf("%lld",MST);
  64.     return 0;
  65. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement