#include <iostream>
#include <vector>
#include <map>
#include <algorithm>
#include <string>
using namespace std;
int n = 17;
vector < string > ports (n, "");
vector < string > checked (ports);
multimap < string, string > routes;
void check_routes (string x)
{
// checked.erase (std::remove (checked.begin (), checked.end (), x));
for (vector < string >::iterator it1 = checked.begin ();it1 != checked.end (); it1++)
{
if (*it1 == x)
{
checked.erase (it1);
}
}
for (multimap < string, string >::iterator it = routes.begin ();
it != routes.end (); it++)
{
if (it->first == x)
{
check_routes (it->second);
}
}
}
int main ()
{
int ans = 0;
ports.push_back ("BUD");
ports.push_back ("BGI");
ports.push_back ("DEL");
ports.push_back ("DOH");
ports.push_back ("DSM");
ports.push_back ("EWR");
ports.push_back ("EYW");
ports.push_back ("HND");
ports.push_back ("ICN");
ports.push_back ("JFK");
ports.push_back ("LGA");
ports.push_back ("LHR");
ports.push_back ("ORD");
ports.push_back ("BSAN");
ports.push_back ("SFO");
ports.push_back ("SIN");
ports.push_back ("TLV");
routes.insert (make_pair ("DMS", "ORD"));
routes.insert (make_pair ("ORD", "BGI"));
routes.insert (make_pair ("BGI", "LGA"));
routes.insert (make_pair ("SIN", "CDG"));
routes.insert (make_pair ("CDG", "SIN"));
routes.insert (make_pair ("CDG", "BUD"));
routes.insert (make_pair ("DEL", "DOH"));
routes.insert (make_pair ("DEL", "CDG"));
routes.insert (make_pair ("TLV", "DEL"));
routes.insert (make_pair ("EWR", "HND"));
routes.insert (make_pair ("HND", "ICN"));
routes.insert (make_pair ("HND", "JFK"));
routes.insert (make_pair ("ICN", "JFK"));
routes.insert (make_pair ("JFK", "LGA"));
routes.insert (make_pair ("EYW", "LHR"));
routes.insert (make_pair ("LHR", "SFO"));
routes.insert (make_pair ("SFO", "SAN"));
routes.insert (make_pair ("SFO", "DSM"));
routes.insert (make_pair ("SAN", "EYW"));
for (int i = 0; !(checked.empty ()); i++)
{
check_routes (checked[i]);
ans++;
}
cout << ans;
return 0;
}
So I was trying to solve the airport connections problem I found online and this is my take on it. But I cannot figure out where the fault in the code lies. I'd be grateful if someone could help me with that. (P.S. The problem goes like this - I have list of airports(vector ports) and another one of one way connections between these ports(multimap routes) and I need to find out the minimum number of connections I need to make so as to connect all the ports(length of the connection does not matter)..................... What this code is supposed to do is find the number of individual chains/networks of interconnected ports, which, I believe, will give me the required answer since all I need to do is connect one node from each of those chains)