/* Zadatak: Bliski povezivanje cvorova na grafu na temelju euklidske udaljenosti Složenost: kvadratna o(n^2) Datum: 29.11.2013. Autor: Kristijan Burnik, udruga informatičara Božo Težak Gmail: kristijanburnik */ #include #include #include #include #include #include #include #include #include using namespace std; typedef pair point; #define X first #define Y second vector name; vector< point > pos; int dist[100][100]; bool used[100]; int pointdist( point a, point b ) { int dx = a.X - b.X; int dy = a.Y - b.Y; return dx*dx + dy*dy; } bool cmp(int a, int b) { return name[a] < name[b]; } int main() { int n , r; cin >> n >> r; for (int i = 0; i < n; i++) { string node; point a; cin >> node >> a.X >> a.Y; name.push_back(node); for (int j = 0; j < pos.size(); j++) { point b = pos[j]; int d = pointdist( a, b ); dist[i][j] = dist[j][i] = d; } pos.push_back( a ); } int rr = r*r; vector output; // for all nodes find ones close for (int i = 0; i < n; i++) { if (used[i]) continue; vector group; for (int j = 0 ; j < n; j++) { if ( dist[i][j] <= rr) { used[j] = true; group.push_back(j); } } string out = ""; sort(group.begin(),group.end(),cmp); for (int k = 0; k < group.size(); k++) { out += name[group[k]]; if (k < group.size()-1) out += "-"; } output.push_back(out); } sort(output.begin(),output.end()); for (int i = 0 ; i < output.size(); i++) cout << output[i] << endl; return 0; }