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
#include <cstdio>
    #include <vector>
    #include <algorithm>
    #include <map>
    #include <limits>
    #include <queue>
    #include <utility>
     
    class City;
     
    class Path {
    public:
        City* next;
        mutable long long cost;
        int id;
    };
     
    bool operator<(const Path& p1, const Path& p2) {
    	return p1.id < p2.id;
    }
     
    class City {
    public:
        int id;
        long long revenue;
        std::vector<Path> connectedCities;
        void addPath(City* to, long long cost, int id) {
            Path p{to, cost, id};
            connectedCities.push_back(p);
        }
    };
     
    int getId(int c1, int c2) {
        return std::min(c1, c2)*100000 + std::max(c1, c2);
    }
     
     
    int nextCityReq(int c, std::vector<City>& cities, std::map<int, Path>& paths, long long currentCost, bool firstUse, int prev) {
        static int bestCity;
        static long long bestRevenue;
        if(firstUse) {
            bestCity = -1;
            bestRevenue = std::numeric_limits<long long>::lowest();
        }
       // printf("inside1 root city: %d\n", c);
        for(auto& pp : cities[c].connectedCities) {
            if(pp.next == nullptr) {
                continue;
            }
            if(pp.next->id == prev) {
                continue;
            }
     
            auto p = (paths.find(pp.id))->second;
     
            long long costToThisCity = currentCost + p.cost;
       // printf("inside2 costToThisCity: %lld\n", costToThisCity);
            long long revenue = pp.next->revenue - costToThisCity;
       // printf("inside3 revenue: %lld\n", revenue);
            if(revenue >= bestRevenue) {
                if(revenue == bestRevenue) {
                    if(bestCity > pp.next->id) {
                        bestCity = pp.next->id;
                    }
                }
                bestCity = pp.next->id;
                bestRevenue = revenue;
            }
        //printf("inside4 bestCity:%d\n", bestCity);
            nextCityReq(pp.next->id, cities, paths, costToThisCity, false, c); 
        }
        return bestCity;
    }
     
    int nextCity(int c, std::vector<City>& cities, std::map<int, Path>& paths, int prev) {
        int bestCity = -1;
        long long bestRevenue = std::numeric_limits<long long>::lowest();
       // printf("inside1 root city: %d\n", c);
     
        // first-previous second-current     
        std::queue<std::pair<int,int>> citiesToCheck;
     
        citiesToCheck.push(std::make_pair(-1, c));
        long long results[cities.size()];
        results[c] = 0;
     
        //int iterCount = 0;
     
        while(!citiesToCheck.empty()) {
            //++iterCount;
            auto p = citiesToCheck.front();
            for(auto& a : cities[p.second].connectedCities) {
                //printf("in loop\n");
                //if(pp.next == nullptr) {
                //    continue;
                //}
                int nextId = a.next->id;
                if(nextId == p.first) {
                    continue;
                }
                auto path = (paths.find(a.id))->second;
                long long costToThisCity = results[p.second] + path.cost;
       // printf("inside2 costToThisCity: %lld\n", costToThisCity);
     
                long long revenue = a.next->revenue - costToThisCity;
       // printf("inside3 revenue: %lld\n", revenue);
                if(revenue >= bestRevenue) {
                    if(revenue == bestRevenue) {
                        if(bestCity > nextId) {
                            bestCity = nextId;
                        }
                    } else {
                        bestCity = nextId;
                        bestRevenue = revenue;
                    }
                }
     
                results[nextId] = costToThisCity;
                citiesToCheck.push(std::make_pair(p.second, nextId));
            }
            citiesToCheck.pop();
        }
     
        //printf("iterCount: %d\n", iterCount);
     
        return bestCity;
    }
     
    int main() {
        int n,q;
        scanf("%d %d", &n, &q);
        std::vector<City> cities;
    	std::map<int, Path> paths;
        long long revenue;
        for(int i=0; i<n; ++i) {
            scanf("%lld", &revenue);
            City c;
            c.id = i;
            c.revenue = revenue;
            cities.push_back(c);
        }
     
     
     
        int c1, c2, id;
        long long cost;
        for(int i=0; i<n-1; ++i) {
            scanf("%d %d %lld", &c1, &c2, &cost);
            id = getId(c1, c2);
            City* city1 = &cities[c1-1];
            City* city2 = &cities[c2-1];
            city1->addPath(city2, cost, id);
            city2->addPath(city1, cost, id);
            Path p{nullptr, cost, id};
            paths[id] = p;
        }
     
       // for(auto c : cities) {
       //     printf("id:%d rev:%d\n", c.id, c.revenue);
       //     for(auto p : c.connectedCities) {
       //         printf("next_id: %d\n", p.next->id);
       //     }
       // }
     
        //printf("helo\n");
     
        int option;
        int currentCity = 0;
        int currentCityBuff;
        int previousCity = -1;
        for(int i=0; i<q; ++i) {
            scanf("%d", &option);
            if(1 == option) {
                 scanf("%d %lld", &c1, &revenue);
                 cities[c1-1].revenue = revenue;
            } else {
                 scanf("%d %d %lld", &c1, &c2, &cost);
                 id = getId(c1, c2);
                 auto p = paths.find(id);
                 p->second.cost = cost;
            }
    //        currentCity = nextCityReq(currentCity, cities, paths, 0, true, -1);
     
            currentCityBuff = currentCity;
            currentCity = nextCity(currentCity, cities, paths, previousCity);
            printf("%d ", currentCity+1);
            previousCity = currentCityBuff;
        }
        printf("\n");
        return 0;
    }