#include <iostream> #include <vector> #include <algorithm> #include <climits> #include <stack> using namespace std; typedef int node_id_t; typedef unsigned long long ull; typedef long long ll; typedef ll dist_t; typedef ll price_t; const int MAX_NODES = 100 * 1024; struct edge_t { node_id_t neigh; dist_t dist; edge_t(node_id_t n, dist_t d) : neigh(n), dist(d) { } bool operator<(const edge_t& o) const { return neigh < o.neigh; } }; struct node_t { price_t price; vector<edge_t> edges; }; struct graph_t { node_id_t nodes_count; node_t nodes[MAX_NODES]; inline void modify_price(node_id_t city, price_t new_price) { nodes[city].price = new_price; } node_id_t bsearch(node_id_t src, node_id_t begin, node_id_t end, node_id_t dst) { node_id_t mid = begin + (end - begin) / 2; node_id_t val = nodes[src].edges[mid].neigh; if (dst < val) return bsearch(src, begin, mid, dst); if (dst > val) return bsearch(src, mid + 1, end, dst); return mid; } inline node_id_t find_pos(node_id_t src, node_id_t dst) { return bsearch(src, 0, nodes[src].edges.size(), dst); } inline void modify_dist(node_id_t src, node_id_t dst, dist_t new_dist) { node_id_t pos = find_pos(src, dst); nodes[src].edges[pos].dist = new_dist; pos = find_pos(dst, src); nodes[dst].edges[pos].dist = new_dist; } // void dump(){ // cout << "nodes: " << nodes_count << endl; // for(int i = 0; i < nodes_count; ++i){ // cout << "node: " << (i+1) << ", price: " << nodes[i].price << ": "; // for(int j = 0; j < nodes[i].edges.size(); ++j){ // cout << "[->" << (nodes[i].edges[j].neigh+1) << "(" << nodes[i].edges[j].dist << ")] "; // } // cout << endl; // } // cout << endl; // } } g; int days; void read() { cin >> g.nodes_count >> days; for (int i = 0; i < g.nodes_count; ++i) { cin >> g.nodes[i].price; } node_id_t src, dst; dist_t dist; for (node_id_t i = 1; i < g.nodes_count; ++i) { cin >> src >> dst >> dist; --src; --dst; g.nodes[src].edges.push_back(edge_t(dst, dist)); g.nodes[dst].edges.push_back(edge_t(src, dist)); } for (node_id_t i = 0; i < g.nodes_count; ++i) { sort(g.nodes[i].edges.begin(), g.nodes[i].edges.end()); } } inline void mod_price() { node_id_t city; price_t price; cin >> city >> price; --city; g.modify_price(city, price); } inline void mod_dist() { node_id_t src, dst; dist_t dist; cin >> src >> dst >> dist; --src; --dst; g.modify_dist(src, dst, dist); } inline void write(node_id_t pos) { cout << (pos + 1) << " "; } inline node_id_t next_move(node_id_t start_node) { vector<bool> visited(g.nodes_count, false); stack<node_id_t> nodes; stack<dist_t> dists; price_t price_back = g.nodes[start_node].price; g.nodes[start_node].price = LLONG_MIN + 4; ll best_gain = LLONG_MIN + 8; node_id_t best_node = INT_MAX; nodes.push(start_node); dists.push(0); while (!nodes.empty()) { node_id_t current_node = nodes.top(); nodes.pop(); dist_t current_dist = dists.top(); dists.pop(); visited[current_node] = true; ll new_gain = g.nodes[current_node].price - current_dist; if (best_gain < new_gain || (best_gain == new_gain && best_node > current_node)) { best_gain = new_gain; best_node = current_node; } vector<edge_t> & e = g.nodes[current_node].edges; for (unsigned i = 0; i < e.size(); ++i) { node_id_t neigh = e[i].neigh; if (!visited[neigh]) { nodes.push(neigh); dists.push(current_dist + e[i].dist); } } } g.nodes[start_node].price = price_back; write(best_node); return best_node; } inline void solve() { int type; node_id_t pos = 0; for (int i = 0; i < days; ++i) { cin >> type; if (type == 1) { mod_price(); } else { mod_dist(); } pos = next_move(pos); } } void end() { cout << endl; } //void test(){ // int type; // node_id_t pos = 0; // // for(int i = 0; i < days; ++i){ // cin >> type; // if(type == 1){ // mod_price(); // } else { // mod_dist(); // } // g.dump(); //// pos = next_move(pos); // } //} int main() { ios_base::sync_with_stdio(0); read(); // g.dump(); // test(); solve(); end(); 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 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 | #include <iostream> #include <vector> #include <algorithm> #include <climits> #include <stack> using namespace std; typedef int node_id_t; typedef unsigned long long ull; typedef long long ll; typedef ll dist_t; typedef ll price_t; const int MAX_NODES = 100 * 1024; struct edge_t { node_id_t neigh; dist_t dist; edge_t(node_id_t n, dist_t d) : neigh(n), dist(d) { } bool operator<(const edge_t& o) const { return neigh < o.neigh; } }; struct node_t { price_t price; vector<edge_t> edges; }; struct graph_t { node_id_t nodes_count; node_t nodes[MAX_NODES]; inline void modify_price(node_id_t city, price_t new_price) { nodes[city].price = new_price; } node_id_t bsearch(node_id_t src, node_id_t begin, node_id_t end, node_id_t dst) { node_id_t mid = begin + (end - begin) / 2; node_id_t val = nodes[src].edges[mid].neigh; if (dst < val) return bsearch(src, begin, mid, dst); if (dst > val) return bsearch(src, mid + 1, end, dst); return mid; } inline node_id_t find_pos(node_id_t src, node_id_t dst) { return bsearch(src, 0, nodes[src].edges.size(), dst); } inline void modify_dist(node_id_t src, node_id_t dst, dist_t new_dist) { node_id_t pos = find_pos(src, dst); nodes[src].edges[pos].dist = new_dist; pos = find_pos(dst, src); nodes[dst].edges[pos].dist = new_dist; } // void dump(){ // cout << "nodes: " << nodes_count << endl; // for(int i = 0; i < nodes_count; ++i){ // cout << "node: " << (i+1) << ", price: " << nodes[i].price << ": "; // for(int j = 0; j < nodes[i].edges.size(); ++j){ // cout << "[->" << (nodes[i].edges[j].neigh+1) << "(" << nodes[i].edges[j].dist << ")] "; // } // cout << endl; // } // cout << endl; // } } g; int days; void read() { cin >> g.nodes_count >> days; for (int i = 0; i < g.nodes_count; ++i) { cin >> g.nodes[i].price; } node_id_t src, dst; dist_t dist; for (node_id_t i = 1; i < g.nodes_count; ++i) { cin >> src >> dst >> dist; --src; --dst; g.nodes[src].edges.push_back(edge_t(dst, dist)); g.nodes[dst].edges.push_back(edge_t(src, dist)); } for (node_id_t i = 0; i < g.nodes_count; ++i) { sort(g.nodes[i].edges.begin(), g.nodes[i].edges.end()); } } inline void mod_price() { node_id_t city; price_t price; cin >> city >> price; --city; g.modify_price(city, price); } inline void mod_dist() { node_id_t src, dst; dist_t dist; cin >> src >> dst >> dist; --src; --dst; g.modify_dist(src, dst, dist); } inline void write(node_id_t pos) { cout << (pos + 1) << " "; } inline node_id_t next_move(node_id_t start_node) { vector<bool> visited(g.nodes_count, false); stack<node_id_t> nodes; stack<dist_t> dists; price_t price_back = g.nodes[start_node].price; g.nodes[start_node].price = LLONG_MIN + 4; ll best_gain = LLONG_MIN + 8; node_id_t best_node = INT_MAX; nodes.push(start_node); dists.push(0); while (!nodes.empty()) { node_id_t current_node = nodes.top(); nodes.pop(); dist_t current_dist = dists.top(); dists.pop(); visited[current_node] = true; ll new_gain = g.nodes[current_node].price - current_dist; if (best_gain < new_gain || (best_gain == new_gain && best_node > current_node)) { best_gain = new_gain; best_node = current_node; } vector<edge_t> & e = g.nodes[current_node].edges; for (unsigned i = 0; i < e.size(); ++i) { node_id_t neigh = e[i].neigh; if (!visited[neigh]) { nodes.push(neigh); dists.push(current_dist + e[i].dist); } } } g.nodes[start_node].price = price_back; write(best_node); return best_node; } inline void solve() { int type; node_id_t pos = 0; for (int i = 0; i < days; ++i) { cin >> type; if (type == 1) { mod_price(); } else { mod_dist(); } pos = next_move(pos); } } void end() { cout << endl; } //void test(){ // int type; // node_id_t pos = 0; // // for(int i = 0; i < days; ++i){ // cin >> type; // if(type == 1){ // mod_price(); // } else { // mod_dist(); // } // g.dump(); //// pos = next_move(pos); // } //} int main() { ios_base::sync_with_stdio(0); read(); // g.dump(); // test(); solve(); end(); return 0; } |