Cod sursa(job #1709387)

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

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

using namespace std;

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

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

long long modul(long long x) {

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

}

struct cmp {

    bool operator ()(pair<long long, long long> &a, pair <long long, long long> &b) {

        return a.second > b.second;

    }

};

priority_queue< pair<long long, long long>, vector< pair<long long, long long> >, cmp> H;

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

int main() {

    int testsCount;
    fin >> testsCount;

    while (testsCount--) {

        int n;
        fin >> n;

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

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

        dp[1] = 0;
        H.push(make_pair(1, 0));

        long long APM = 0;

        while(H.size()) {

            long long node = H.top().first;
            long long cost = H.top().second;

            H.pop();

            if(vis[node])
                continue;

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

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

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

                if (dist < dp[child]) {

                    dp[child] = dist;
                    H.push(make_pair(child, dist));

                }

            }

        }

        fout << APM << "\n";

    }

    return 0;

}