#abc286e. E - Souvenir
E - Souvenir
Score : points
问题描述
设有 座城市,同时存在连接不同城市的单向直达航班。
直达航班的可用性由 个长度为 的字符串 表示。如果 的第 个字符是 Y
,表示存在从城市 到城市 的直达航班;如果是 N
,则表示不存在该航班。
每个城市都出售一种纪念品,其中城市 出售的价值为 的纪念品。
考虑以下问题:
高桥当前在城市 ,他想通过一些直达航班到达另一个不同的城市 。 每当他访问一个城市(包括 和 ),他都会在那里购买一个纪念品。 如果从城市 到城市 存在多条路线时,高桥会按以下方式决定路线:
- 他尽量使从城市 到城市 的路线中的直达航班数量最小化。
- 然后尽量使购买的纪念品总价值最大化。
判断他是否能够利用直达航班从城市 到达城市 。如果可以,请找出满足上述条件的路线中的“直达航班数量”和“纪念品总价值”。
已给出 对不同的城市 。 对于每个 ,请计算当 且 时,上述问题的答案。
以上为通义千问 qwen-max 翻译,仅供参考。
Problem Statement
There are cities. There are also one-way direct flights that connect different cities.
The availability of direct flights is represented by strings of length each. If the -th character of is Y
, there is a direct flight from city to city ; if it is N
, there is not.
Each city sells a souvenir; city sells a souvenir of value .
Consider the following problem:
Takahashi is currently at city and wants to get to city (that is different from city ) using some direct flights.
Every time he visits a city (including and ), he buys a souvenir there.
If there are multiple routes from city to city , Takahashi decides the route as follows:
- He tries to minimize the number of direct flights in the route from city to city .
- Then he tries to maximize the total value of the souvenirs he buys.
Determine if he can travel from city to city 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 pairs of distinct cities.
For each , print the answer to the problem above when and .
Constraints
- is a string of length consisting of
Y
andN
. - The -th character of is
N
. - If , then .
- , and are all integers.
Input
The input is given from Standard Input in the following format:
Output
Print lines.
The -th line should contain Impossible
if he cannot travel from city to city ; 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 , there is a direct flight from city to city , so the minimum possible number of direct flights is , which is achieved when he uses that direct flight. In this case, the total value of the souvenirs is .
For , the minimum possible number of direct flights is . The following two routes achieve the minimum: cities , and cities . Since the total value of souvenirs in the two routes are and , respectively, so he chooses the latter route, and the total value of the souvenirs is .
For , the number of direct flights is minimum when he travels cities , where the total value of the souvenirs is .
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