#include <iostream> #include <vector> #include <queue> #include <map> using namespace std; int main(){ ios::sync_with_stdio(false); int n, q; cin >> n >> q; vector<long long> earn(n+1); for(int i=1; i<=n; ++i) cin >> earn[i]; vector<int> connections[n+1]; map<pair<int,int>, long long> way_cost; for(int i=0; i<n-1; ++i){ int a, b, c; cin >> a >> b >> c; connections[a].push_back(b); connections[b].push_back(a); way_cost[make_pair(a, b)] = way_cost[make_pair(b, a)] = c; } int start = 1; while(q--){ int operation; cin >> operation; if(operation == 1){ int v, d; cin >> v >> d; earn[v] = d; } else{ int a, b, d; cin >> a >> b >> d; way_cost[make_pair(a, b)] = way_cost[make_pair(b, a)] = d; } queue<int> way; vector<long long> spent_on_way(n+1, -1); way.push(start); spent_on_way[start] = 0; int end_v = connections[start][0]; long long max_earn = earn[end_v] - way_cost[make_pair(start, end_v)]; while(!way.empty()){ int k = way.front(); way.pop(); for(unsigned i=0; i<connections[k].size(); ++i){ int v = connections[k][i]; if(spent_on_way[v] == -1){ way.push(v); spent_on_way[v] = spent_on_way[k] + way_cost[make_pair(k, v)]; long long how_much_will_I_earn = earn[v] - spent_on_way[v]; if(how_much_will_I_earn > max_earn){ end_v = v; max_earn = earn[v] - spent_on_way[v]; } else if(how_much_will_I_earn == max_earn && end_v > v){ end_v = v; } } } } start = end_v; cout << end_v << " "; } 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 | #include <iostream> #include <vector> #include <queue> #include <map> using namespace std; int main(){ ios::sync_with_stdio(false); int n, q; cin >> n >> q; vector<long long> earn(n+1); for(int i=1; i<=n; ++i) cin >> earn[i]; vector<int> connections[n+1]; map<pair<int,int>, long long> way_cost; for(int i=0; i<n-1; ++i){ int a, b, c; cin >> a >> b >> c; connections[a].push_back(b); connections[b].push_back(a); way_cost[make_pair(a, b)] = way_cost[make_pair(b, a)] = c; } int start = 1; while(q--){ int operation; cin >> operation; if(operation == 1){ int v, d; cin >> v >> d; earn[v] = d; } else{ int a, b, d; cin >> a >> b >> d; way_cost[make_pair(a, b)] = way_cost[make_pair(b, a)] = d; } queue<int> way; vector<long long> spent_on_way(n+1, -1); way.push(start); spent_on_way[start] = 0; int end_v = connections[start][0]; long long max_earn = earn[end_v] - way_cost[make_pair(start, end_v)]; while(!way.empty()){ int k = way.front(); way.pop(); for(unsigned i=0; i<connections[k].size(); ++i){ int v = connections[k][i]; if(spent_on_way[v] == -1){ way.push(v); spent_on_way[v] = spent_on_way[k] + way_cost[make_pair(k, v)]; long long how_much_will_I_earn = earn[v] - spent_on_way[v]; if(how_much_will_I_earn > max_earn){ end_v = v; max_earn = earn[v] - spent_on_way[v]; } else if(how_much_will_I_earn == max_earn && end_v > v){ end_v = v; } } } } start = end_v; cout << end_v << " "; } return 0; } |