Cod sursa(job #1709367)

Utilizator UNIBUC_Costan_Iordache_MagureanuGangster Teddy Bears Trio UNIBUC_Costan_Iordache_Magureanu Data 28 mai 2016 11:58:24
Problema Metrou4 Scor 0
Compilator cpp Status done
Runda ONIS 2016 - Runda - 2 - ACM ICPC Romanian Programming Contest Marime 2.89 kb
#include <fstream>
#include <cstring>

#define DIM 150010
#define INF (1LL << 40)

using namespace std;

ifstream fin("metrou4.in");
ofstream fout("metrou4.out");

pair<long long, long long> v[DIM];

long long heap[DIM], pos[DIM], val[DIM];
long long len;

void push(long long x) {

    long long c = x, p = x / 2;

    while (p) {

        if (val[heap[p]] <= val[heap[c]])
            break;

        swap(heap[p], heap[c]);

        pos[heap[p]] = p;
        pos[heap[c]] = c;

        c = p; p = c / 2;

    }

}

void pop(long long x) {

    long long p = x, c = 2 * x;

    while (c <= len) {

        if (c < len && val[heap[c + 1]] <= val[heap[c]])
            ++c;

        if (val[heap[p]] <= val[heap[c]])
            break;

        swap(heap[p], heap[c]);

        pos[heap[p]] = p;
        pos[heap[c]] = c;

        p = c, c = 2 * p;

    }

}

bool vis[DIM], isInHeap[DIM];
long long dp[DIM];


long long modul(long long x) {

    return (x < 0 ? -x : x);

}

int main() {

    int testsCount;
    fin >> testsCount;

    while (testsCount--) {

        int n;
        fin >> n;

        len = 0;

        for(int i = 1; i <= n; i++)
            fin >> v[i].first >> v[i].second;

        for(int i = 1; i <= n; i++)
            dp[i] = INF, vis[i] = false, isInHeap[i] = false;

        dp[1] = 0;
        ++len;
        val[1] = 0;
        heap[len] = 1;
        isInHeap[1] = true;
        pos[1] = len;

        push(len);

        long long APM = 0;

        while(len) {

            int node = heap[1];
            long long cost = dp[node];

            val[node] = -1;
            push(pos[node]);

            pos[heap[1]] = 0;
            heap[1] = heap[len--];
            pos[heap[1]] = 1;

            pop(1);

            if(vis[node])
                continue;

            vis[node] = true;
            APM += cost;

            for(int child = 1; child <= n; child++) {

                if(vis[child])
                    continue;

                long long dist = modul(v[child].first - v[node].first) + modul(v[child].second - v[node].second);

                if (dist < dp[child]) {

                    if(isInHeap[child]) {
                        val[child] = -1;
                        push(pos[child]);

                        pos[heap[1]] = 0;
                        heap[1] = heap[len--];
                        pos[heap[1]] = 1;

                        pop(1);

                    }

                    isInHeap[child] = true;
                    dp[child] = dist;

                    ++len;
                    val[child] = dist;
                    heap[len] = child;
                    pos[child] = len;

                    push(len);

                }

            }

        }

        fout << APM << "\n";

    }

    return 0;

}