比赛 CSP2023-S模拟赛 评测结果 EEEEEEEEEEEEEEEEEEEE
题目名称 坡伊踹 最终得分 0
用户昵称 hnzzlxs01 运行时间 6.926 s
代码语言 C++ 内存使用 14.99 MiB
提交时间 2023-10-17 20:50:01
显示代码纯文本
#include <cstdio>
#include <queue>
#include <cstring>

using namespace std;

#define int long long

int read() {
	int x = 0, w = 1;
	char ch = 0;
	while (ch < '0' || ch > '9') {
		if (ch == '-') w = -1;
		ch = getchar();
	} 
	while (ch >= '0' && ch <= '9') {
		x = x * 10 + (ch - '0');
		ch = getchar();
	}
	return x * w;
}

const int maxn = 2e5 + 5;
const int mod = 998244353;

int n, Q;
int a[maxn];

struct edge {
	int to;
	int next;
	int dist;
};
vector<edge> e;
int head[maxn];
int edgenum;

void add(int u, int v, int dist) {
	e.push_back((edge){v, head[u], dist});
	head[u] = edgenum++;
}

int u, v, w;

int dist[maxn];
bool f[maxn];
int from[maxn];
int pn;

struct node {
	int p;
	int dist;
	bool operator <(const node &x) const {
		return x.dist < dist;
	}
};
priority_queue<node> q;

void dijkstra(int u) {
	dist[u] = 0;
	q.push((node){u, 0});
	from[u] = u;
	while (!q.empty()) {
		node sum = q.top();
		q.pop();
		int x = sum.p;
		if (f[x] == 1) continue;
		f[x] = 1;
		for (int i = head[x]; i != -1; i = e[i].next) {
			int v = e[i].to;
			if (dist[v] > dist[x] + e[i].dist) {
				dist[v] = dist[x] + e[i].dist;
				from[v] = x;
				if (f[v] == 0) {
					//printf("v:%lld\n", v);
					q.push((node){v, dist[v]});
				}
			}
		}
	}
}

queue<int> path;

void findpath(int s, int t) {
	if (s == t) {
		path.push(t);
		return ;
	}
	path.push(t);
	findpath(s, from[t]);
}

signed main() {
	freopen("poitry.in", "r", stdin);
	freopen("poitry.out", "w", stdout);
	memset(head, -1, sizeof(head));
	
	n = read(), Q = read();
	for (int i = 1; i <= n; i++) {
		a[i] = read();
	}
	for (int i = 1; i < n; i++) {
		u = read(), v = read(), w = read();
		add(u, v, w);
		add(v, u, w);
	}
	
	while (Q--) {
		u = read(), v = read();
		memset(dist, 0x3f3f3f3f, sizeof(dist));
		memset(from, 0, sizeof(from));
		dijkstra(u);
		int minn = 0x3f3f3f3f;
		while (!path.empty()) path.pop();
		findpath(u, v);
		while (!path.empty()) {
			minn = min(minn, max(a[path.front()], dist[path.front()]));
			path.pop();
		}
		printf("%lld\n", minn);
	}
	return 0;
}