#abc286e. E - Souvenir

E - Souvenir

Score : 500500 points

问题描述

设有 NN 座城市,同时存在连接不同城市的单向直达航班。 直达航班的可用性由 NN 个长度为 NN 的字符串 S1,S2,,SNS_1,S_2,\ldots,S_N 表示。如果 SiS_i 的第 jj 个字符是 Y,表示存在从城市 ii 到城市 jj 的直达航班;如果是 N,则表示不存在该航班。 每个城市都出售一种纪念品,其中城市 ii 出售的价值为 AiA_i 的纪念品。

考虑以下问题:

高桥当前在城市 SS,他想通过一些直达航班到达另一个不同的城市 TT。 每当他访问一个城市(包括 SSTT),他都会在那里购买一个纪念品。 如果从城市 SS 到城市 TT 存在多条路线时,高桥会按以下方式决定路线:

  • 他尽量使从城市 SS 到城市 TT 的路线中的直达航班数量最小化。
  • 然后尽量使购买的纪念品总价值最大化。

判断他是否能够利用直达航班从城市 SS 到达城市 TT。如果可以,请找出满足上述条件的路线中的“直达航班数量”和“纪念品总价值”。

已给出 QQ 对不同的城市 (Ui,Vi)(U_i,V_i)。 对于每个 1iQ1\leq i\leq Q,请计算当 S=UiS=U_iT=ViT=V_i 时,上述问题的答案。

以上为通义千问 qwen-max 翻译,仅供参考。

Problem Statement

There are NN cities. There are also one-way direct flights that connect different cities.
The availability of direct flights is represented by NN strings S1,S2,,SNS_1,S_2,\ldots,S_N of length NN each. If the jj-th character of SiS_i is Y, there is a direct flight from city ii to city jj; if it is N, there is not.
Each city sells a souvenir; city ii sells a souvenir of value AiA_i.

Consider the following problem:

Takahashi is currently at city SS and wants to get to city TT (that is different from city SS) using some direct flights.
Every time he visits a city (including SS and TT), he buys a souvenir there.
If there are multiple routes from city SS to city TT, Takahashi decides the route as follows:

  • He tries to minimize the number of direct flights in the route from city SS to city TT.
  • Then he tries to maximize the total value of the souvenirs he buys.

Determine if he can travel from city SS to city TT using the direct flights. If he can, find the "number of direct flights" and "total value of souvenirs" in the route that satisfies the conditions above.

You are given QQ pairs (Ui,Vi)(U_i,V_i) of distinct cities.
For each 1iQ1\leq i\leq Q, print the answer to the problem above when S=UiS=U_i and T=ViT=V_i.

Constraints

  • 2N3002 \leq N \leq 300
  • 1Ai1091\leq A_i\leq 10^9
  • SiS_i is a string of length NN consisting of Y and N.
  • The ii-th character of SiS_i is N.
  • 1QN(N1)1\leq Q\leq N(N-1)
  • 1Ui,ViN1\leq U_i,V_i\leq N
  • UiViU_i\neq V_i
  • If iji \neq j, then (Ui,Vi)(Uj,VJ)(U_i,V_i)\neq (U_j,V_J).
  • N,Ai,Q,UiN,A_i,Q,U_i, and ViV_i are all integers.

Input

The input is given from Standard Input in the following format:

NN

A1A_1 A2A_2 \ldots ANA_N

S1S_1

S2S_2

\vdots

SNS_N

QQ

U1U_1 V1V_1

U2U_2 V2V_2

\vdots

UQU_Q VQV_Q

Output

Print QQ lines.
The ii-th (1iQ)(1\leq i\leq Q) line should contain Impossible if he cannot travel from city UiU_i to city ViV_i; if he can, the line should contain "the number of direct flights" and "the total value of souvenirs" in the route chosen as described above, in this order, separated by a space.

Sample Input 1

5
30 50 70 20 60
NYYNN
NNYNN
NNNYY
YNNNN
YNNNN
3
1 3
3 1
4 5

Sample Output 1

1 100
2 160
3 180

For (S,T)=(U1,V1)=(1,3)(S,T)=(U_1,V_1)=(1,3), there is a direct flight from city 11 to city 33, so the minimum possible number of direct flights is 11, which is achieved when he uses that direct flight. In this case, the total value of the souvenirs is A1+A3=30+70=100A_1+A_3=30+70=100.

For (S,T)=(U2,V2)=(3,1)(S,T)=(U_2,V_2)=(3,1), the minimum possible number of direct flights is 22. The following two routes achieve the minimum: cities 3413\to 4\to 1, and cities 3513\to 5\to 1. Since the total value of souvenirs in the two routes are 70+20+30=12070+20+30=120 and 70+60+30=16070+60+30=160, respectively, so he chooses the latter route, and the total value of the souvenirs is 160160.

For (S,T)=(U3,V3)=(4,5)(S,T)=(U_3,V_3)=(4,5), the number of direct flights is minimum when he travels cities 41354\to 1\to 3\to 5, where the total value of the souvenirs is 20+30+70+60=18020+30+70+60=180.

Sample Input 2

2
100 100
NN
NN
1
1 2

Sample Output 2

Impossible

There may be no direct flight at all.

update @ 2024/3/10 11:57:58