#include "stdafx.h" #include #include #include #include #include #include #include #include using namespace std; /* minut to sec 1 min = 60 sec hour to minut 1h = 60 min hour to sec 1h = 3600sec */ struct Node { Node(string _from) { from = _from; } string from; vector to = {}; vector dist_time = {}; void add(Node* toAdd, int toAddDist) { to.push_back(toAdd); dist_time.push_back(toAddDist); } }; int hhmmssTosec(string time) { return ((stoi(time.substr(0, 2)) * 3600) + (stoi(time.substr(3, 2)) * 60) + stoi(time.substr(6, 2))); } string secTohhmmss(int sec) { int h = sec / 3600; int temp = sec - h * 3600; int m = temp / 60; temp = temp - m * 60; int s = temp; string h_s = h < 10 ? "0" + to_string(h) : to_string(h); string m_s = m < 10 ? "0" + to_string(m) : to_string(m); string s_s = s < 10 ? "0" + to_string(s) : to_string(s); return h_s + ":" + m_s + ":" + s_s; } std::pair, vector> readFromFile(string fname) { vector < std::tuple > lines; vector vars; vector nodes; string fir, sec, thi, kontrollaeg, maxKontrollYletusAeg, trahviSaamisVahemik, trahviPunkt, v6imalikuTeed; int count = 0; ifstream fin(fname); for (string line; getline(fin, line);) { if (count == 0) { stringstream ss1(line); ss1 >> kontrollaeg >> maxKontrollYletusAeg >> trahviSaamisVahemik >> trahviPunkt; vars.push_back(hhmmssTosec(kontrollaeg)); vars.push_back(stoi(maxKontrollYletusAeg)); vars.push_back(stoi(trahviSaamisVahemik)); vars.push_back(stoi(trahviPunkt)); } else if (count == 1) { stringstream ss2(line); ss2 >> v6imalikuTeed; vars.push_back(stoi(v6imalikuTeed)); } else { stringstream ss3(line); ss3 >> fir >> sec >> thi; lines.push_back(std::make_tuple(fir, sec, thi)); } count++; } fin.close(); for (size_t i = 0; i < lines.size(); i++ ) { bool exists1 = false; bool exists2 = false; for (size_t j = 0; j < nodes.size();j++) { if (nodes[j].from == std::get<0>(lines[i])) { exists1 = true; } } if (!exists1) { nodes.push_back(*new Node(std::get<0>(lines[i]))); } for (size_t j = 0; j < nodes.size(); j++) { if (nodes[j].from == std::get<1>(lines[i])) { exists2 = true; } } if (!exists2) { nodes.push_back(*new Node(std::get<1>(lines[i]))); } //cout << std::get<0>(lines[i]) << ", " << std::get<1>(lines[i]) << ", " << std::get<2>(lines[i]) << endl; } for (auto i : nodes) { cout << i.from << endl; } for (size_t i = 0; i < nodes.size(); i++) { for (auto item : lines) { if (nodes[i].from == std::get<0>(item)) { // "200" == "200" (kohal "200", "201", 00:00:00) int secondNodePos = 0; for (size_t j = 0; j < nodes.size(); j++) { if (std::get<1>(item) == nodes[j].from) { secondNodePos = j; break; } }; nodes[i].add(&nodes[secondNodePos], hhmmssTosec(std::get<2>(item))); } } } for (auto i : nodes) { cout << i.from << ", "; for (size_t j = 0; j < i.to.size(); j++) { cout << i.to[j]->from << ", "; cout << secTohhmmss(i.dist_time[j]) << ", "; } cout << endl; } std::pair, vector> paar = std::make_pair(vars, nodes); return paar; } vector> kuvaTeekonnad(Node *nood, int points = 0, int timeSum = 0, vector> sumAndPointsTrace = {}) { int count = 0; while (nood->to.size() != count) { sumAndPointsTrace.push_back(std::make_tuple(nood->from, points, timeSum)); if (nood->to[count]->from != "F") { sumAndPointsTrace = kuvaTeekonnad(nood->to[count], (stoi(nood->to[count]->from) / 100 + points), ((nood->dist_time[count]) + timeSum), sumAndPointsTrace ); } else { sumAndPointsTrace.push_back(std::make_tuple("F", points, (nood->dist_time[count] + timeSum))); } count++; } return sumAndPointsTrace; } string leiaParimTeekond(vector> teekonnad, int kontrollaeg, int maxYletusAeg, int trahviSaamisVahemik, int trahviPunkt) { vector currTeekond, winnerTeekond; int points = 0, bestTime = 0, trahviPoints = 0, bestTotalPoints = 0, tempTrahviPoints = 0; string winner = ""; for (auto item : teekonnad) { currTeekond.push_back(std::get<0>(item)); if (std::get<0>(item) == "F") { if (std::get<2>(item) < (kontrollaeg + maxYletusAeg)) { if (std::get<2>(item) > kontrollaeg) { tempTrahviPoints = (((std::get<2>(item) - kontrollaeg) / trahviSaamisVahemik) * trahviPunkt); int tempPoints = std::get<1>(item) - tempTrahviPoints; if (tempPoints > bestTotalPoints && std::get<2>(item) > bestTime) { points = std::get<1>(item); bestTotalPoints = tempPoints; bestTime = std::get<2>(item); trahviPoints = tempTrahviPoints; winnerTeekond = currTeekond; } } else { if (std::get<1>(item) > bestTotalPoints && std::get<2>(item) > bestTime) { trahviPoints = 0; points = std::get<1>(item); bestTotalPoints = std::get<1>(item); bestTime = std::get<2>(item); winnerTeekond = currTeekond; } } } currTeekond = { "S" }; } } winner = to_string(points) + " " + secTohhmmss(bestTime) + " " + to_string(trahviPoints) + " " + to_string(bestTotalPoints)+"\n"; for (auto item : winnerTeekond) { winner += item+" "; } return winner; } int main() { /*int kontrollaeg = (8/*h* * 3600); //sec int maxKontrollYletusAeg = (30/*m* * 60); //sec int trahviSaamisVahemik = 60; //sec int trahviPunkt = 1; //yhik int v6imalikuTeed = 6; //yhik Node esi = {"S"}; Node tesi = { "200" }; esi.add(&tesi, hhmmssTosec("00:12:30")); Node kosi = { "201" }; Node nesi = { "301" }; Node viisi = { "1309" }; tesi.add(&kosi, hhmmssTosec("00:07:12")); tesi.add(&nesi, hhmmssTosec("00:20:21")); tesi.add(&viisi, hhmmssTosec("01:20:00")); nesi.add(&kosi, hhmmssTosec("00:18:10")); Node kuusi = { "F" }; kosi.add(&kuusi, hhmmssTosec("00:05:00"));*/ std::pair, vector> fileInput = readFromFile("input_001.txt"); vector vars = fileInput.first; vector nodes = fileInput.second; for (auto i : nodes) { cout << i.from << ", "; for (size_t j = 0; j < i.to.size(); j++) { cout << i.to[j]->from << ", "; cout << i.dist_time[j] << ", "; } cout << endl; } cout << leiaParimTeekond(kuvaTeekonnad(&nodes[0]), vars[0], vars[1], vars[2], vars[3]) << endl; //kuvaTeekonnad(&nodes[0]); //vector> asd = kuvaTeekonnad(&esi); /*for (auto i:asd) { cout << std::get<0>(i) << ", " << std::get<1>(i) << ", " << std::get<2>(i) << endl; }*/ //cout << leiaParimTeekond(asd, kontrollaeg, maxKontrollYletusAeg, trahviSaamisVahemik, trahviPunkt) << endl; return 0; }