# BZOJ4999 JZOJ5112【usaco2017_Mar Platinum】Switch Grass

#### Description

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 N$N$ fields (1N200,000$1 \leq N \leq 200,000$), where M$M$ pairs of fields are connected by bi-directional pathways (1M200,000$1 \leq M \leq 200,000$). Using these pathways, it is possible to walk from any field to any other field. Each pathway has an integer length in the range 11,000,000$1 \ldots 1,000,000$. Any pair of fields will be linked by at most one direct pathway.

In each field, FJ initially plants one of K$K$ types of grass (1KN$1 \leq K \leq N$). Over time, however, he might decide to switch the grass in some field to a different type. He calls this an “update” operation. 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 30 percent of the input cases, each field will be directly connected to at most 10 pathways.

#### Input

The first line of input contains four integers, N$N$, M$M$, K$K$, and Q$Q$, where Q$Q$ is the number of updates (1Q200,000$1 \leq Q \leq 200,000$). The next M$M$ lines describe the paths; each one contains three integers A$A$, B$B$, and L$L$, indicating a path from field A$A$ to field B$B$ (both integers in the range 1N$1 \ldots N$) of length L$L$. The next line indicates the initial type of grass growing in each field (N$N$ integers in the range 1K$1 \ldots K$). Finally, the last Q$Q$ lines each describe an update, specified by two integers A$A$ and B$B$, where the grass in field A$A$ is to be updated to type B$B$.

#### Output

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

#### 题解：

Code：

#include<map>
#include<set>
#include<cstdio>
#include<cstring>
#include<algorithm>
#define fo(i, x, y) for(int i = x; i <= y; i ++)
using namespace std;

const int Maxn = 200005, INF = 1e9;

typedef pair<int, int> P;
typedef multiset<int> T;
typedef map<int, T> M;
M a[Maxn];
multiset<P> b[Maxn];
T c;

struct node {
int x, y, z;
}bb[Maxn];
int n, m, k, q, x, y, z, color[Maxn], f[Maxn], bz[Maxn], fa[Maxn], fav[Maxn], d[Maxn];
int final[Maxn], tot;
struct egde {
int to, next, v;
}e[Maxn * 2];

char ch = ' '; x = 0;
for(;ch < '0' || ch > '9'; ch = getchar());
for(;ch >= '0' && ch <= '9'; ch = getchar()) x = x * 10 + ch - 48;
}

bool rank_b(node a, node b) {return a.z < b.z;}
int find(int x) {return f[x] == x ? x : (f[x] = find(f[x]));}
void link(int x, int y, int z) {
e[++ tot].next = final[x], e[tot].to = y, e[tot].v = z, final[x] = tot;
e[++ tot].next = final[y], e[tot].to = x, e[tot].v = z, final[y] = tot;
}

void Init() {
scanf("%d %d %d %d", &n, &m, &k, &q);
fo(i, 1, m) {
}
}

multiset<P> :: iterator it;

void Gai_d(int x) {
if(d[x] != INF) c.erase(c.find(d[x]));
P v = (*b[x].begin()); it = b[x].begin();
int o = b[x].size();
if((v.second == color[x] && o == 1) || !o) d[x] = INF; else {
if(v.second == color[x]) d[x] = (*(++ it)).first; else d[x] = (*it).first;
c.insert(d[x]);
}
}

void Gai(int x, int y, int z, int bz, int c1, int c2) {
int u = *a[x][c1].begin(), v = a[x][c1].size();
if(v) b[x].erase(b[x].find(make_pair(u, c1)));
if(bz) a[x][c1].erase(a[x][c1].find(z));
if(!a[x][c1].empty()) b[x].insert(make_pair(, c1));
if(!a[x][c2].empty()) b[x].erase(b[x].find(make_pair(*a[x][c2].begin(), c2)));
a[x][c2].insert(z);
b[x].insert(make_pair(*a[x][c2].begin(), c2));
Gai_d(x);
}

void dg_mak(int x) {
bz[x] = 1;
for(int k = final[x]; k; k = e[k].next) {
int y = e[k].to, z = e[k].v; if(bz[y]) continue;
Gai(x, y, z, 0, color[y], color[y]);
fa[y] = x; fav[y] = z; dg_mak(y);
}
}

void Build() {
sort(bb + 1, bb + m + 1, rank_b);
fo(i, 1, n) f[i] = i;
fo(i, 1, m) {
x = bb[i].x, y = bb[i].y, z = bb[i].z;
if(find(x) != find(y)) {
f[f[x]] = f[y];
}
}
fo(i, 1, n) d[i] = INF;
dg_mak(1);
}

void End() {
fo(i, 1, q) {
if(fa[x]) Gai(fa[x], x, fav[x], 1, color[x], y);
color[x] = y; Gai_d(x);
printf("%d\n", (*c.begin()));
}
}

int main() {
freopen("grass.in", "r", stdin);
freopen("grass.out", "w", stdout);
Init();
Build();
End();
}

©️2019 CSDN 皮肤主题: 深蓝海洋 设计师: CSDN官方博客