Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- #include <vector>
- #include <cfloat>
- using namespace std;
- typedef struct city
- {
- string szCity;
- float x;
- float y;
- city(string _city, float _x, float _y) :
- szCity(_city), x(_x), y(_y)
- {}
- float distancef(const city& toCity) const
- {
- return (x - toCity.x)*(x - toCity.x) + (y - toCity.y)*(y - toCity.y);
- }
- } City;
- int main()
- {
- cin.sync_with_stdio(false);
- cout.sync_with_stdio(false);
- int nCount;
- cin >> nCount;
- vector<City> cityList;
- cityList.reserve(nCount);
- for (int i = 0; i < nCount; i++)
- {
- string szCity;
- int x, y;
- cin >> szCity >> x >> y;
- cityList.push_back(City(szCity, x, y));
- }
- string closestCities[2];
- float shortestDist = FLT_MAX;
- for (vector<City>::iterator it1 = cityList.begin(); it1 != cityList.end(); it1++)
- {
- for (vector<City>::iterator it2 = it1+1; it2 != cityList.end(); it2++)
- {
- float d = it1->distancef(*it2);
- if (d < shortestDist)
- {
- shortestDist = d;
- closestCities[0] = it1->szCity;
- closestCities[1] = it2->szCity;
- }
- }
- }
- cout << closestCities[0] << "-" << closestCities[1] << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment