Cod sursa(job #1710086)

Utilizator UBB_RANDOMUBB Muntea Zsisku Adam UBB_RANDOM Data 28 mai 2016 14:58:58
Problema Metrou4 Scor 0
Compilator cpp Status done
Runda ONIS 2016 - Runda - 2 - ACM ICPC Romanian Programming Contest Marime 1.7 kb
#include <iostream>
#include <vector>
#include <queue>
#include <string.h>
#include <cstring>
#include <fstream>
#include <stdlib.h>
#include <algorithm>
#include <fstream>
#include <algorithm>
#define n_max 150001
using namespace std;

ifstream r("metrou4.in");
ofstream w("metrou4.out");

typedef struct{
    int x, y;
}coord;

typedef struct {
    int x, y, cost;
}muchie;

muchie m[300002];
coord v[150001];
bool viz[150001];
int t[150001];

int radacina(int nod) {
    if (t[nod]==0)
        return nod;
    else {
        t[nod]=radacina(t[nod]);
        return t[nod];
    }
}

int solve(int nr) {
    int i,tx,ty;
    int cost = 0;
    int k = 0;
    for (i=1; i<=nr; i++) {
        tx=radacina(m[i].x);
        ty=radacina(m[i].y);
        if (tx!=ty) {
            t[tx]=ty;
            cost += m[i].cost;
        }
    }
    return cost;
}


int dist(const coord& c1, const coord& c2) {
    return abs(c1.x - c2.x) + abs(c1.y - c2.y);
}

bool cmp(const muchie& m1, const muchie& m2) {
    return m1.cost < m2.cost;
}

int main()
{
    int tt, x, y, n;
    r >> tt;
    for(; tt--;) {
        r >> n;
        for(int i = 1; i <= n; i++) {
            r >> v[i].x >> v[i].y;
        }
        int nr = 0;
        for (int i = 1; i <= n; i++)
            for(int j = 1; j <=n; j++) {
                if (i != j ) {
                    nr++;
                    m[nr].x = i;
                    m[nr].y = j;
                    m[nr].cost = dist(v[i], v[j]);
                }
            }
        sort(m, m + nr + 1, cmp);
        w << solve(nr)<< endl;
        for(int i = 1; i <= n; i++) {
            t[i] = 0;
        }
    }
    return 0;
}