#include <iostream> #include <vector> #include <map> #include <stack> using namespace std; class Node { public: int visited; long long cost; long long gain; map<int, long long> edges; }; int main() { int n, d; cin >> n >> d; vector<Node> g(n); for(int i = 0; i < n; i++) cin >> g[i].gain; int a, b; long long c; for(int i = 0; i < n-1; i++) { cin >> a >> b >> c; g[a-1].edges[b-1] = c; g[b-1].edges[a-1] = c; } int pos = 0; for(int id = 1; id <= d; id++) { int zmiana; cin >> zmiana; if(zmiana == 1) { long vi; long long di; cin >> vi >> di; g[vi-1].gain = di; } else { long a, b; long long c; cin >> a >> b >> c; g[a-1].edges[b-1] = c; g[b-1].edges[a-1] = c; } stack<int> s; g[pos].visited = id; g[pos].cost = 0; s.push(pos); long long candidate; long long best = -1000000000000000000ll; while(!s.empty()) { int x = s.top(); g[x].visited = id; s.pop(); for(auto it = g[x].edges.begin(); it != g[x].edges.end(); it++) { if(g[it->first].visited == id) continue; g[it->first].cost = g[x].cost + it->second; candidate = g[it->first].gain - g[it->first].cost; if(candidate > best || (candidate == best && it->first < pos)) { pos = it->first; best = candidate; } s.push(it->first); } } cout << pos+1 << " "; } cout << endl; 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 | #include <iostream> #include <vector> #include <map> #include <stack> using namespace std; class Node { public: int visited; long long cost; long long gain; map<int, long long> edges; }; int main() { int n, d; cin >> n >> d; vector<Node> g(n); for(int i = 0; i < n; i++) cin >> g[i].gain; int a, b; long long c; for(int i = 0; i < n-1; i++) { cin >> a >> b >> c; g[a-1].edges[b-1] = c; g[b-1].edges[a-1] = c; } int pos = 0; for(int id = 1; id <= d; id++) { int zmiana; cin >> zmiana; if(zmiana == 1) { long vi; long long di; cin >> vi >> di; g[vi-1].gain = di; } else { long a, b; long long c; cin >> a >> b >> c; g[a-1].edges[b-1] = c; g[b-1].edges[a-1] = c; } stack<int> s; g[pos].visited = id; g[pos].cost = 0; s.push(pos); long long candidate; long long best = -1000000000000000000ll; while(!s.empty()) { int x = s.top(); g[x].visited = id; s.pop(); for(auto it = g[x].edges.begin(); it != g[x].edges.end(); it++) { if(g[it->first].visited == id) continue; g[it->first].cost = g[x].cost + it->second; candidate = g[it->first].gain - g[it->first].cost; if(candidate > best || (candidate == best && it->first < pos)) { pos = it->first; best = candidate; } s.push(it->first); } } cout << pos+1 << " "; } cout << endl; return 0; } |