Cod sursa(job #1709473)

Utilizator UNIBUC_Costan_Iordache_MagureanuGangster Teddy Bears Trio UNIBUC_Costan_Iordache_Magureanu Data 28 mai 2016 12:28:06
Problema Metrou4 Scor 0
Compilator cpp Status done
Runda ONIS 2016 - Runda - 2 - ACM ICPC Romanian Programming Contest Marime 1.68 kb
#include <fstream>
#include <set>
#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);

}

set< pair<long long, int> > H;
set< pair<long long, int> >::iterator it;

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;

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

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

        long long APM = 0;

        while(H.size()) {

            int node = H.begin()->second;
            long long cost = H.begin()->first;

            H.erase(H.begin());

            if(vis[node])
                continue;

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

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

                if(child == node || vis[child])
                    continue;

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

                if (dist < dp[child]) {

                    it = H.find(make_pair(dp[child], child));
                    if(it != H.end())
                        H.erase(it);
                    dp[child] = dist;
                    H.insert(make_pair(dist, child));

                }

            }

        }

        fout << APM << "\n";

    }

    return 0;

}