#include <iostream> #include <limits> #include <map> #include <set> #include <stack> struct Data { long long profit{0}; std::set<int> neighbors; }; struct StackData { StackData(int prevcity, int currcity, long long length) : prevcity(prevcity) , currcity(currcity) , length(length) {} int prevcity{0}; int currcity{0}; long long length{0}; }; std::pair<int, int> sorted(int a, int b) { return a < b ? std::make_pair(a, b) : std::make_pair(b, a); } int search(int current_city, const std::map<int, Data> &cities, const std::map<std::pair<int, int>, long long> &charges) { int result = 0; long long max = std::numeric_limits<long long>::min(); std::stack<StackData> stack; for (const auto &i : cities.find(current_city)->second.neighbors) { stack.push({current_city, i, charges.find(sorted(current_city, i))->second}); } while (stack.empty() == false) { // StackData s = stack.top(); stack.pop(); // long long profit = cities.find(s.currcity)->second.profit - s.length; if (max < profit || (max == profit && s.currcity < result)) { max = profit; result = s.currcity; } // for (const auto &i : cities.find(s.currcity)->second.neighbors) { if (i == s.prevcity) { continue; } stack.push({s.currcity, i, s.length + charges.find(sorted(s.currcity, i))->second}); } } return result; } int main() { std::ios_base::sync_with_stdio(0); // int N = 0; int Q = 0; std::cin >> N; std::cin >> Q; std::map<int, Data> cities; std::map<std::pair<int, int>, long long> charges; for (int i = 0; i < N; ++i) std::cin >> cities[i + 1].profit; for (int i = 0; i < N - 1; ++i) { int a = 0, b = 0; long long charge = 0; std::cin >> a; std::cin >> b; std::cin >> charge; cities[a].neighbors.insert(b); cities[b].neighbors.insert(a); charges[sorted(a, b)] = charge; } // int current_city = 1; for (int i = 0; i < Q; ++i) { int op = 0; std::cin >> op; if (op == 1) { int city = 0; long long profit = 0; std::cin >> city; std::cin >> profit; cities[city].profit = profit; } else { int a = 0; int b = 0; long long charge = 0; std::cin >> a; std::cin >> b; std::cin >> charge; charges[sorted(a, b)] = charge; } // current_city = search(current_city, cities, charges); printf("%d ", current_city); // DEBUG //fprintf(stderr, "%d\n", i); } return 0; }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 | #include <iostream> #include <limits> #include <map> #include <set> #include <stack> struct Data { long long profit{0}; std::set<int> neighbors; }; struct StackData { StackData(int prevcity, int currcity, long long length) : prevcity(prevcity) , currcity(currcity) , length(length) {} int prevcity{0}; int currcity{0}; long long length{0}; }; std::pair<int, int> sorted(int a, int b) { return a < b ? std::make_pair(a, b) : std::make_pair(b, a); } int search(int current_city, const std::map<int, Data> &cities, const std::map<std::pair<int, int>, long long> &charges) { int result = 0; long long max = std::numeric_limits<long long>::min(); std::stack<StackData> stack; for (const auto &i : cities.find(current_city)->second.neighbors) { stack.push({current_city, i, charges.find(sorted(current_city, i))->second}); } while (stack.empty() == false) { // StackData s = stack.top(); stack.pop(); // long long profit = cities.find(s.currcity)->second.profit - s.length; if (max < profit || (max == profit && s.currcity < result)) { max = profit; result = s.currcity; } // for (const auto &i : cities.find(s.currcity)->second.neighbors) { if (i == s.prevcity) { continue; } stack.push({s.currcity, i, s.length + charges.find(sorted(s.currcity, i))->second}); } } return result; } int main() { std::ios_base::sync_with_stdio(0); // int N = 0; int Q = 0; std::cin >> N; std::cin >> Q; std::map<int, Data> cities; std::map<std::pair<int, int>, long long> charges; for (int i = 0; i < N; ++i) std::cin >> cities[i + 1].profit; for (int i = 0; i < N - 1; ++i) { int a = 0, b = 0; long long charge = 0; std::cin >> a; std::cin >> b; std::cin >> charge; cities[a].neighbors.insert(b); cities[b].neighbors.insert(a); charges[sorted(a, b)] = charge; } // int current_city = 1; for (int i = 0; i < Q; ++i) { int op = 0; std::cin >> op; if (op == 1) { int city = 0; long long profit = 0; std::cin >> city; std::cin >> profit; cities[city].profit = profit; } else { int a = 0; int b = 0; long long charge = 0; std::cin >> a; std::cin >> b; std::cin >> charge; charges[sorted(a, b)] = charge; } // current_city = search(current_city, cities, charges); printf("%d ", current_city); // DEBUG //fprintf(stderr, "%d\n", i); } return 0; } |