#4777. [USACO2017 Open] Switch Grass

[USACO2017 Open] Switch Grass

题目描述

Farmer John has recently been experimenting with cultivating different types of grass on his farm, realizing that different types of cows like different types of grass. However, he must be careful to ensure that different types of grass are planted sufficiently far away from each-other, in order to prevent them from being inextricably mixed.

FJ's farm consists of nn fields, where mm pairs of fields are connected by bi-directional pathways. Using these pathways, it is possible to walk from any field to any other field. Any pair of fields will be linked by at most one direct pathway.

In each field, FJ initially plants one of kk types of grass. Over time, however, he might decide to switch the grass in some field to a different type. He calls this an "update" peration. He might perform several updates over the course of time, which are all cumulative in nature.

After each update, FJ would like to know the length of the shortest path between two fields having different grass types. That is, among all pairs of fields having different grass types, he wants to know which two are closest. Ideally,

this number is large, so he can prevent grass of one type from mixing with grass of another type. It is guaranteed that the farm will always have at least two fields with different grass types.

In 3030 percent of the input cases, each field will be directly connected to at most 1010 pathways.

输入格式

The first line of input contains four integers, nn, mm, kk, and QQ, where QQ is the number of updates.
The next mm lines describe the paths; each one contains three integers aa, bb, and ll, indicating a path from field aa to field bb (both integers in the range 1n1 \ldots n) of length ll.
The next line indicates the initial type of grass growing in each field (nn integers in the range 1k1 \ldots k).
Finally, the last QQ lines each describe an update, specified by two integers aa and bb, where the grass in field aa is to be updated to type bb.

输出格式

For each update, print the length of the shortest path between two fields with different types of grass, after the update is applied.

3 2 3 4
1 2 3
2 3 1
1 1 2
3 3
2 3
1 2
2 2
1
3
3
1

数据规模与约定

对于 100%100\% 的数据,1kn1 \leq k \leq n1n,m2×1051\le n,m\le 2\times 10^51Q2×1051 \leq Q \leq 2\times 10^5
Each pathway has an integer length in the range [1,106][1,10^6].

题目来源

Platinum