fork download
#include <bits/stdc++.h>
using namespace std;
const int maxN=510;
const int maxQ=2e5+1;
const int INF=1e9;

int def[maxN],dp[maxN][maxN],idx[maxN];//機場防禦力高到低
int qry[maxQ][3],ans[maxQ],ord[maxQ];//查詢暴動指數高到低
bool comp(int lhs, int rhs){
	return qry[lhs][0]>qry[rhs][0];
}
int main(){
	int N, M, Q, u, v, c;
	cin>>N>>M>>Q;
	for(int i=1; i<=N; i++)
		cin>>def[i];
	for(int i=1; i<=N; i++)
		for(int j=1; j<=N; j++)
			dp[i][j]=INF;
	for(int i=1; i<=M; i++){
		cin>>u>>v>>c;
		dp[u][v]=dp[v][u]=min(dp[u][v],c);
	}
	for(int q=1; q<=Q; q++)
		cin>>qry[q][0]>>qry[q][1]>>qry[q][2];
		
	iota(ord+1,ord+1+Q,1);
	sort(ord+1,ord+1+Q,comp);
	iota(idx+1,idx+1+N,1);//防禦力
	sort(idx+1,idx+1+N,[](int lhs, int rhs){
		return def[lhs]>def[rhs];
	});
	//for(int i=1; i<=N; i++)
	//	cout<<idx[i]<<' '; 
	
	for(int n=1, q=1; q<=Q; q++){
		int qID=ord[q];
		for(; n<=N and def[idx[n]]>=qry[qID][0]; n++){
			int m=idx[n];
			for(int s=1; s<=N; s++)
				for(int e=1; e<=N; e++)
					dp[s][e]=min(dp[s][e],dp[s][m]+dp[m][e]);
		}
		u=qry[qID][1], v=qry[qID][2];
		ans[qID]=(def[u]>=qry[qID][0] and def[v]>=qry[qID][0] and dp[u][v]<INF)?dp[u][v]:-1;
	}
	for(int q=1; q<=Q; q++)
		cout<<ans[q]<<'\n';
	
	/*for(int i=1; i<=N; i++){
		for(int j=1; j<=N; j++)
			cout<<(dp[i][j]==INF? 0:dp[i][j])<<' ';
		cout<<'\n';
	}*/
}
Success #stdin #stdout 0.01s 5576KB
stdin
6 5 5
5 4 1 3 2 6
1 3 3
1 4 1
2 4 6
2 5 1
3 5 2
1 1 2
2 1 2
3 1 2
4 1 2
1 5 3
stdout
6
7
7
-1
2