loj #136

最小瓶颈路
做最小生成树是进行特判即可
时间复杂度 n * k

#include <bits/stdc++.h>

const int N = 1010, M = 1e5 + 10;

struct Node {
	int u, v, w;
	bool operator < (const Node a) const {return w < a.w;}
} E[M];
int X[N], Y[N];
int n, m, k;
int fa[N];
int Answer[N];

int Get(int x) {return x == fa[x] ? x : fa[x] = Get(fa[x]);}

int main() {
	std:: cin >> n >> m >> k;
	for(int i = 1; i <= m; i ++) std:: cin >> E[i].u >> E[i].v >> E[i].w;
	for(int i = 1; i <= k; i ++) std:: cin >> X[i] >> Y[i];
	std:: sort(E + 1, E + m + 1);
	for(int i = 1; i <= n; i ++) fa[i] = i;
	int js = 0;
	for(int i = 1; i <= m && js < n - 1; i ++) {
		int fax = Get(E[i].u), fay = Get(E[i].v);
		if(fax == fay) continue;
		for(int j = 1; j <= k; j ++) {
			if(Answer[j]) continue;
			int fa1 = Get(X[j]), fa2 = Get(Y[j]);
			if((fa1 == fax && fa2 == fay) || (fa1 == fay && fa2 == fax)) {
				Answer[j] = E[i].w;
			}
		}
		fa[fax] = fay;
		js ++;
	}
	for(int i = 1; i <= k; i ++) {
		std:: cout << (Answer[i] == 0 ? -1 : Answer[i]) << "\n";
	}
	return 0; 
}
posted @ 2018-09-19 09:04  xayata  阅读(189)  评论(0编辑  收藏  举报