Switch Grass
Time limit2sMemory limit512 MB
A weighted connected graph has a color at each vertex; after each of Q point color updates, report the shortest distance between two vertices of different colors.
- Level
Hard9 of 10
- Topics
- Graph, Shortest path, Divide and conquer, Dynamic programming
- Solved
- No attempts yet
Problem
Farmer John plants several kinds of grass on his farm, because different cows like different grass. Grass of different kinds has to be planted far enough apart. Planted close together, two kinds mix and cannot be separated again.
The farm has fields (), and pairs of fields are joined by two-way pathways (). Using only these pathways, you can walk from any field to any other field. Each pathway has an integer length between and , and at most one pathway joins any given pair of fields.
John plants one of kinds of grass in each field (). Later he sometimes replaces the grass in one field with another kind. Call that an update. Several updates happen over time, and every update stays in effect.
After each update John wants the length of the shortest path between two fields whose grass kinds differ. That is, among all pairs of fields with different kinds, the distance of the closest pair. A large value means one kind is unlikely to mix with another. After every update the farm always has at least two fields with different kinds of grass.
In 30 percent of the inputs, each field is directly joined to at most 10 pathways.
Input
The first line contains four integers , , , and , where is the number of updates ().
Each of the next lines describes one pathway with three integers , , and , meaning a pathway of length between field and field (, ).
The next line contains integers, the kind of grass first planted in fields through in order. Each value is between and .
Each of the last lines describes one update with two integers and , meaning the grass in field becomes kind (, ).
Output
For each update, print on its own line the length of the shortest path between two fields with different kinds of grass, after the update is applied.