#include <limits> #include <cmath> #include <cstdio> #include <cstring> #include <map> #define BAN_BRUT_ENABLED using namespace std; static const int N = 100000; static long long z[N + 1]; static map<int, long long> d[N + 1]; static inline long long readnum() { long long x; scanf("%lld", &x); return x; } static void readCost(map<int, long long> d[]) { const int a = readnum(); const int b = readnum(); const long long c = readnum(); d[a][b] = c; d[b][a] = c; } static void review(const int city, long long cost, long long costs[]) { int i = city; while(d[i].size() == 1) { const int k = d[i].begin()->first; cost += d[i].begin()->second; if (costs[k] == 0) { costs[k] = cost; review(k, cost, costs); } i = k; } for(map<int, long long>::const_iterator it = d[i].begin(); it != d[i].end(); it++) { const int k = it->first; if (costs[k] == 0) { const long long r = cost + it->second; costs[k] = r; review(k, r, costs); } } } static int nextCity(const int city, const int n) { static long long costs[N + 1]; memset(costs, 0, sizeof(costs)); costs[city] = numeric_limits<long long>::max(); review(city, 0, costs); int r = 0; long long income = numeric_limits<long long>::min(); for(int i = 1; i <= n; i++) { const long long inc = z[i] - costs[i]; if (inc > income) { income = inc; r = i; } } return r; } #ifdef BAN_BRUT_ENABLED int main() { const int n = readnum(); const int q = readnum(); for(int i = 1; i <= n; i++) { z[i] = readnum(); } for(int i = 1; i < n; i++) { readCost(d); } for(int i = 1, city = 1; i <= q; i++) { if (readnum() == 1) { const int v = readnum(); z[v] = readnum(); } else { readCost(d); } printf("%d ", city = nextCity(city, n)); } return 0; } #endif
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 | #include <limits> #include <cmath> #include <cstdio> #include <cstring> #include <map> #define BAN_BRUT_ENABLED using namespace std; static const int N = 100000; static long long z[N + 1]; static map<int, long long> d[N + 1]; static inline long long readnum() { long long x; scanf("%lld", &x); return x; } static void readCost(map<int, long long> d[]) { const int a = readnum(); const int b = readnum(); const long long c = readnum(); d[a][b] = c; d[b][a] = c; } static void review(const int city, long long cost, long long costs[]) { int i = city; while(d[i].size() == 1) { const int k = d[i].begin()->first; cost += d[i].begin()->second; if (costs[k] == 0) { costs[k] = cost; review(k, cost, costs); } i = k; } for(map<int, long long>::const_iterator it = d[i].begin(); it != d[i].end(); it++) { const int k = it->first; if (costs[k] == 0) { const long long r = cost + it->second; costs[k] = r; review(k, r, costs); } } } static int nextCity(const int city, const int n) { static long long costs[N + 1]; memset(costs, 0, sizeof(costs)); costs[city] = numeric_limits<long long>::max(); review(city, 0, costs); int r = 0; long long income = numeric_limits<long long>::min(); for(int i = 1; i <= n; i++) { const long long inc = z[i] - costs[i]; if (inc > income) { income = inc; r = i; } } return r; } #ifdef BAN_BRUT_ENABLED int main() { const int n = readnum(); const int q = readnum(); for(int i = 1; i <= n; i++) { z[i] = readnum(); } for(int i = 1; i < n; i++) { readCost(d); } for(int i = 1, city = 1; i <= q; i++) { if (readnum() == 1) { const int v = readnum(); z[v] = readnum(); } else { readCost(d); } printf("%d ", city = nextCity(city, n)); } return 0; } #endif |