Pagini recente » Cod sursa (job #2529893) | Cod sursa (job #1933283) | Cod sursa (job #1911427) | Cod sursa (job #984684) | Cod sursa (job #1709473)
#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;
}