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
#include <iostream>
#include <list>
#include <vector>
#include <unordered_map>
#include <queue>

using namespace std;

struct city;

struct connection
{
	city& to;
	long long cost;

	connection(city& to, long long cost) : to(to), cost(cost) {}
};

struct city
{
	int id;
	long long income = 0;
	long long currentCost = -1;
	vector<connection> connections;

	city(int id, long long income) : id(id), income(income) {}
	long long NetIncome() { return income - currentCost; }
};

bool NewIsBetter(city* oldCity, city& newCity)
{
	if (oldCity == nullptr)
		return true;

	if (oldCity->NetIncome() < newCity.NetIncome())
		return true;

	if (oldCity->NetIncome() > newCity.NetIncome())
		return false;
	
	return newCity.id < oldCity->id;
}

city* CalculateBest(city* currentCity)
{
	city* bestCity = nullptr;
	queue<city*> cities;
	cities.push(currentCity);
	currentCity->currentCost = 0;

	while (cities.empty() == false)
	{
		currentCity = cities.front();
		cities.pop();

		//search through tree
		for (auto& connection : currentCity->connections)
		{
			auto& city = connection.to;
			if (city.currentCost != -1)//visited
				continue;

			cities.push(&city);
			city.currentCost = currentCity->currentCost + connection.cost;

			if (NewIsBetter(bestCity, city))
				bestCity = &city;
		}
	}

	return bestCity;
}

int main()
{
	int n, q;
	cin >> n;
	cin >> q;

	vector<city> cities;
	unordered_map<int, unordered_map<int, connection*>> connectionsmap;
	vector<int> output;

	for (int i = 1; i <= n; i++)
	{
		long long income;
		cin >> income;

		cities.push_back({ i, income });
	}

	for (int i = 1; i < n; i++)
	{
		int from, to;
		long long cost;
		cin >> from;
		cin >> to;
		cin >> cost;

		from--;
		to--;

		cities[from].connections.push_back({ cities[to], cost });
		cities[to].connections.push_back({ cities[from], cost });
	}

	for (auto& city : cities)
	{
		for (auto& connection : city.connections)
		{
			connectionsmap[city.id][connection.to.id] = &connection;
		}
	}

	auto currentCity = &(cities.front());

	for (int i = 0; i < q; i++)
	{
		int type;
		cin >> type;
		switch (type)
		{
		case 1:
			int city;
			cin >> city;
			cin >> cities[city - 1].income;
			break;
		default:
			int from, to;
			long long cost;
			cin >> from;
			cin >> to;
			cin >> cost;
			connectionsmap[from][to]->cost = cost;
			connectionsmap[to][from]->cost = cost;
			break;
		}

		for (auto& city : cities)
			city.currentCost = -1;

		currentCity = CalculateBest(currentCity);

		output.push_back(currentCity->id);
	}

	for (auto& id : output)
		cout << id << " ";

    return 0;
}