1st question




2nd question





3rd question




4th question



Another slot
1st question









#include <iostream>
#include <vector>
#include <climits>
using namespace std;
int findBestCity(int distanceThreshold, int city_nodes, vector<int>& city_from, vector<int>& city_to, vector<int>& city_weight) {
// Initialize graph with infinity values
vector<vector<int>> graph(city_nodes, vector<int>(city_nodes, INT_MAX));
// Set distance to self as 0
for (int i = 0; i < city_nodes; i++) {
graph[i][i] = 0;
}
// Fill in the direct distances between cities
for (int i = 0; i < city_from.size(); i++) {
graph[city_from[i] - 1][city_to[i] - 1] = city_weight[i];
graph[city_to[i] - 1][city_from[i] - 1] = city_weight[i];
}
// Floyd-Warshall algorithm to find all pairs shortest path
for (int k = 0; k < city_nodes; k++) {
for (int i = 0; i < city_nodes; i++) {
for (int j = 0; j < city_nodes; j++) {
if (graph[i][k] != INT_MAX && graph[k][j] != INT_MAX) {
graph[i][j] = min(graph[i][j], graph[i][k] + graph[k][j]);
}
}
}
}
int best_city = -1;
int min_neighbours = INT_MAX;
// Find the city with the fewest reachable neighbours within the distance threshold
for (int i = 0; i < city_nodes; i++) {
int neighbours = 0;
for (int j = 0; j < city_nodes; j++) {
if (graph[i][j] <= distanceThreshold && i != j) {
neighbours++;
}
}
if (neighbours < min_neighbours || (neighbours == min_neighbours && i + 1 > best_city)) {
min_neighbours = neighbours;
best_city = i + 1; // Convert back to 1-based index
}
}
return best_city;
}
int main() {
int distanceThreshold = 4;
int city_nodes = 4;
vector<int> city_from = {1, 1, 2, 3};
vector<int> city_to = {2, 3, 3, 4};
vector<int> city_weight = {1, 4, 1, 2};
int result = findBestCity(distanceThreshold, city_nodes, city_from, city_to, city_weight);
cout << "Best city: " << result << endl;
return 0;
}
2nd question








// Helper function to convert time string to an integer that can be compared easily
int convertTime(const string& time) {
size_t plusPos = time.find('+');
if (plusPos != string::npos) {
int regularTime = stoi(time.substr(0, plusPos));
int extraTime = stoi(time.substr(plusPos + 1));
return regularTime * 100 + extraTime;
} else {
return stoi(time) * 100;
}
}
// Struct to store event details
struct Event {
string teamName;
string employeeName;
int time;
char eventName;
string secondEmployeeName;
// For sorting: first by time, then by event type, then by team name, and finally by employee name
bool operator<(const Event& other) const {
if (time != other.time)
return time < other.time;
if (eventName != other.eventName)
return eventName < other.eventName;
if (teamName != other.teamName)
return teamName < other.teamName;
return employeeName < other.employeeName;
}
// Convert the event back to the string format
string toString() const {
stringstream ss;
ss << teamName << " " << employeeName << " " << time / 100;
if (time % 100 > 0) {
ss << "+" << time % 100;
}
ss << " " << eventName;
if (eventName == 'S') {
ss << " " << secondEmployeeName;
}
return ss.str();
}
};
vector<string> getEventsOrder(string team1, string team2, vector<string> events1, vector<string> events2) {
vector<Event> allEvents;
// Helper lambda to parse events
auto parseEvents = [&](const string& teamName, const vector<string>& events) {
for (const string& event : events) {
istringstream iss(event);
string employeeName, timeStr, eventName, secondEmployeeName;
iss >> employeeName >> timeStr >> eventName;
if (eventName == "S") {
iss >> secondEmployeeName;
}
allEvents.push_back({teamName, employeeName, convertTime(timeStr), eventName[0], secondEmployeeName});
}
};
// Parse both teams' events
parseEvents(team1, events1);
parseEvents(team2, events2);
// Sort events based on the defined order
sort(allEvents.begin(), allEvents.end());
// Convert sorted events back to string format
vector<string> result;
for (const Event& event : allEvents) {
result.push_back(event.toString());
}
return result;
}
3rd question