Cod sursa(job #1709190)

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

#define DIM 150010
#define INF (1 << 30)

using namespace std;

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

pair<int, int> v[DIM];

int modul(int x) {

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

}

struct cmp {

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

        return a.second > b.second;

    }

};

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

bool vis[DIM];
int 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));

        int APM = 0;

        while(H.size()) {

            int node = H.top().first;
            int 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)
                    continue;
                int 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;

}