#include <cstdio> #include <iostream> #include <cmath> #include <string> #include <sstream> #include <set> #include <algorithm> #include <deque> #include <map> using namespace std; long current; long n, q; map<long, map<long, long long> > graph; vector<long long> gains; vector<long long> costs; void print_gains() { for(int i=0; i<n; i++) { cout << gains[i] << " "; } cout << endl; } void print_graph() { for(int i=0; i<n; i++) { cout << i << ": "; for (int j=0; j<graph[i].size(); j++) { cout << j << ":" << graph[i][j] << ","; } cout << endl; } } void read_graph_and_gains() { scanf("%ld %ld\n", &n, &q); for (int i=0; i< n; i++) { long long g; scanf("%lld", &g); gains.push_back(g); map<long, long long> edges; graph[i] = edges; costs.push_back(0); } for(int i=0; i<n-1; i++) { long a, b; long long c; scanf("%ld %ld %lld\n", &a, &b, &c); a--; b--; graph[a][b] = c; graph[b][a] = c; } } void print_current(int i) { if (i == q-1) { printf("%ld\n", current+1); } else { printf("%ld ", current+1); } } void update_graph_and_gains() { long opt; scanf("%ld ", &opt); if (opt == 1) { long v; long long d; scanf("%ld %lld\n", &v, &d); gains[--v] = d; } else { long a,b; long long d; scanf("%ld %ld %lld\n", &a, &b, &d); --a; --b; graph[a][b] = d; graph[b][a] = d; } } long select_best() { bool is_init = false; long long max_bilans; long long max_bilans_id; for (int i=0; i<n; i++) { if (i != current) { long long bilans = gains[i] - costs[i]; if (is_init) { if (bilans > max_bilans) { max_bilans_id = i; max_bilans = bilans; } else if (bilans == max_bilans) { if (i < max_bilans_id) { max_bilans_id = i; } } } else { max_bilans = bilans; max_bilans_id = i; is_init = true; } } } return max_bilans_id; } void compute_costs() { vector<bool> visited(n); deque<long> que; que.push_back(current); costs[current] = 0; visited[current] = true; while (!que.empty()) { long c = que.front(); que.pop_front(); map<long, long long> edges = graph[c]; for (map<long, long long>::iterator it=edges.begin(); it !=edges.end(); it++) { if (!(visited[it->first])) { costs[it->first] = costs[c] + it->second; visited[it->first] = true; que.push_back(it->first); } } } } void print_costs() { cout << "costs: "; for (int i=0; i<costs.size(); i++) { cout << costs[i] << " "; } cout << endl; } int main() { current = 0; read_graph_and_gains(); //print_gains(); //print_graph(); for (int i=0; i<q; i++) { update_graph_and_gains(); // cout << "day: " << (i+1) << endl; //print_gains(); //print_graph(); compute_costs(); //print_costs(); current = select_best(); // cout << "selected: " << current << endl; print_current(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 144 145 146 147 148 149 150 151 152 153 154 | #include <cstdio> #include <iostream> #include <cmath> #include <string> #include <sstream> #include <set> #include <algorithm> #include <deque> #include <map> using namespace std; long current; long n, q; map<long, map<long, long long> > graph; vector<long long> gains; vector<long long> costs; void print_gains() { for(int i=0; i<n; i++) { cout << gains[i] << " "; } cout << endl; } void print_graph() { for(int i=0; i<n; i++) { cout << i << ": "; for (int j=0; j<graph[i].size(); j++) { cout << j << ":" << graph[i][j] << ","; } cout << endl; } } void read_graph_and_gains() { scanf("%ld %ld\n", &n, &q); for (int i=0; i< n; i++) { long long g; scanf("%lld", &g); gains.push_back(g); map<long, long long> edges; graph[i] = edges; costs.push_back(0); } for(int i=0; i<n-1; i++) { long a, b; long long c; scanf("%ld %ld %lld\n", &a, &b, &c); a--; b--; graph[a][b] = c; graph[b][a] = c; } } void print_current(int i) { if (i == q-1) { printf("%ld\n", current+1); } else { printf("%ld ", current+1); } } void update_graph_and_gains() { long opt; scanf("%ld ", &opt); if (opt == 1) { long v; long long d; scanf("%ld %lld\n", &v, &d); gains[--v] = d; } else { long a,b; long long d; scanf("%ld %ld %lld\n", &a, &b, &d); --a; --b; graph[a][b] = d; graph[b][a] = d; } } long select_best() { bool is_init = false; long long max_bilans; long long max_bilans_id; for (int i=0; i<n; i++) { if (i != current) { long long bilans = gains[i] - costs[i]; if (is_init) { if (bilans > max_bilans) { max_bilans_id = i; max_bilans = bilans; } else if (bilans == max_bilans) { if (i < max_bilans_id) { max_bilans_id = i; } } } else { max_bilans = bilans; max_bilans_id = i; is_init = true; } } } return max_bilans_id; } void compute_costs() { vector<bool> visited(n); deque<long> que; que.push_back(current); costs[current] = 0; visited[current] = true; while (!que.empty()) { long c = que.front(); que.pop_front(); map<long, long long> edges = graph[c]; for (map<long, long long>::iterator it=edges.begin(); it !=edges.end(); it++) { if (!(visited[it->first])) { costs[it->first] = costs[c] + it->second; visited[it->first] = true; que.push_back(it->first); } } } } void print_costs() { cout << "costs: "; for (int i=0; i<costs.size(); i++) { cout << costs[i] << " "; } cout << endl; } int main() { current = 0; read_graph_and_gains(); //print_gains(); //print_graph(); for (int i=0; i<q; i++) { update_graph_and_gains(); // cout << "day: " << (i+1) << endl; //print_gains(); //print_graph(); compute_costs(); //print_costs(); current = select_best(); // cout << "selected: " << current << endl; print_current(i); } return 0; } |