AtCoder Beginner Contest 286 题目讲解

A题 B题 C题 D题 E题


蒟蒻来讲题,还望大家喜。若哪有问题,大家尽可提!

Hello, 大家好哇!本初中生蒟蒻今天讲解一下AtCoder Beginner Contest 286的E题——Souvenir

===========================================================================================

原题

Problem Statement

There are N N N cities. There are also one-way direct flights that connect different cities.
The availability of direct flights is represented by N N N strings S 1 ​ , S 2 ​ , … , S N S_{1}​,S_{2}​,…,S_{N} S1​​,S2​​,…,SN​​ of length N N N each. If the j j j-th character of S i S_{i} Si​​ is Y Y Y, there is a direct flight from city i i i to city j j j; if it is N N N, there is not.
Each city sells a souvenir; city i sells a souvenir of value A i A_{i} Ai​​.

Consider the following problem:

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

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

Determine if he can travel from city S S S to city T T T 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 Q Q Q pairs ( U i ​ , V i ​ ) (U_{i}​,V_{i}​) (Ui​​,Vi​​) of distinct cities.
For each 1 ≤ i ≤ Q 1\leq i\leq Q 1≤i≤Q, print the answer to the problem above when S = U i S=U_{i} S=Ui​​ and T = V i T=V_{i} T=Vi​​.

Constraints

  • 2 ≤ N ≤ 300 2\leq N\leq 300 2≤N≤300
  • 1 ≤ A i ​ ≤ 1 0 9 1\leq A_{i}​\leq10^9 1≤Ai​​≤109
  • S i S_{i} Si​​ is a string of length
  • N N N consisting of Y Y Y and N N N.
  • The i i i-th character of S i S_{i} Si​​ is N N N.
  • 1 ≤ Q ≤ N ( N − 1 ) 1\leq Q\leq N(N−1) 1≤Q≤N(N−1)
  • 1 ≤ U i ​ , V i ​ ≤ N 1\leq U_{i​},V_{i}​\leq N 1≤Ui​​,Vi​​≤N
  • U i ​ ≠ V i Ui​\neq Vi Ui​=Vi​
  • If i ≠ j i\neq j i=j, then ( U i ​ , V i ​ ) ≠ ( U j ​ , V J ​ ) (U_{i}​,V_{i}​)\neq (U_{j}​,V_{J}​) (Ui​​,Vi​​)=(Uj​​,VJ​​).
  • N , A i ​ , Q , U i ​ , N,A_{i}​,Q,U_{i}​, N,Ai​​,Q,Ui​​, and V i V_{i} Vi​​ are all integers.

Input

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

N
A1​  A2​  …  AN​
S1​
S2​
⋮
SN​
Q
U1​  V1​
U2​  V2​
⋮
UQ​  VQ​

Output

Print Q Q Q lines.
The i i i-th ( 1 ≤ i ≤ Q 1\leq i\leq Q 1≤i≤Q) line should contain Impossible if he cannot travel from city U i U_{i} Ui​​ to city V i V_{i} Vi​​; 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 ) = ( U 1 ​ , V 1 ​ ) = ( 1 , 3 ) (S,T)=(U_{1}​,V_{1}​)=(1,3) (S,T)=(U1​​,V1​​)=(1,3), there is a direct flight from city 1 1 1 to city 3 3 3, so the minimum possible number of direct flights is 1 1 1, which is achieved when he uses that direct flight. In this case, the total value of the souvenirs is A 1 ​ + A 3 ​ = 30 + 70 = 100 A_{1}​+A_{3}​=30+70=100 A1​​+A3​​=30+70=100.
For ( S , T ) = ( U 2 ​ , V 2 ​ ) = ( 3 , 1 ) (S,T)=(U2​,V2​)=(3,1) (S,T)=(U2​,V2​)=(3,1), the minimum possible number of direct flights is 2 2 2. The following two routes achieve the minimum: cities 3 → 4 → 1 3→4→1 3→4→1, and cities 3 → 5 → 1 3→5→1 3→5→1. Since the total value of souvenirs in the two routes are 70 + 20 + 30 = 120 70+20+30=120 70+20+30=120 and 70 + 60 + 30 = 160 70+60+30=160 70+60+30=160, respectively, so he chooses the latter route, and the total value of the souvenirs is 160 160 160.
For ( S , T ) = ( U 3 ​ , V 3 ​ ) = ( 4 , 5 ) (S,T)=(U_{3}​,V_{3}​)=(4,5) (S,T)=(U3​​,V3​​)=(4,5), the number of direct flights is minimum when he travels cities 4 → 1 → 3 → 5 4→1→3→5 4→1→3→5, where the total value of the souvenirs is 20 + 30 + 70 + 60 = 180 20+30+70+60=180 20+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.


思路

这道题一看就可以用我们的 D i j k s t r a Dijkstra Dijkstra了,但是, D i j k s t r a Dijkstra Dijkstra只能求出最短路径,不能求出最短路径的最大价值。不过,我们可以再开一个数组 r e s res res,记录价值。

  • 如果 d i s t [ i ] [ j ] > d i s t [ i ] [ t ] + g [ t ] [ j ] dist[i][j] > dist[i][t]+g[t][j] dist[i][j]>dist[i][t]+g[t][j]就说明有一条最短路径,因为短优先,所以直接让 r e s [ i ] [ j ] = r e s [ i ] [ t ] + a [ j ] res[i][j] = res[i][t] + a[j] res[i][j]=res[i][t]+a[j]
  • 如果 d i s t [ i ] [ j ] = d i s t [ i ] [ t ] + g [ t ] [ j ] dist[i][j] = dist[i][t]+g[t][j] dist[i][j]=dist[i][t]+g[t][j]就说明最短路径相等,那我们就要求最大值,即为 r e s [ i ] [ j ] = m a x ( r e s [ i ] [ j ] , r e s [ i ] [ t ] + a [ j ] ) res[i][j] = max(res[i][j], res[i][t]+a[j]) res[i][j]=max(res[i][j],res[i][t]+a[j])

不过每次调用 D i j k s t r a Dijkstra Dijkstra会超时,所以我们要先预处理一下,把任一点到任一点枚举一遍因为点的数量很少,这样就是 O ( N 3 ) O(N^3) O(N3)的时间复杂度,就可以过了!


代码

#include <iostream>
#include <cstring>
#define int long long //本题要开long long!using namespace std;const int N = 5e2 + 10;int n, m;
int g[N][N], dist[N][N]; //定义邻接矩阵和最短距离数组
int cost[N];
int res[N][N];//最短距离下的最大价值
bool st[N];void dijkstra()
{for (int i = 0; i <= n; i ++)for (int j = 0; j <= n; j ++)dist[i][j] = 1e18;for (int i = 1; i <= n; i ++){dist[i][i] = 0;memset(st, 0, sizeof st);for (int k = 0; k < n - 1; k ++){int t = -1;for (int j = 1; j <= n; j ++)if (!st[j] && (t == -1 || dist[i][t] > dist[i][j]))t = j;st[t] = 1;for (int j = 1; j <= n; j ++){if (dist[i][j] > dist[i][t] + g[t][j]) //说明有最短路径{dist[i][j] = dist[i][t] + g[t][j];res[i][j] = res[i][t] + cost[j]; //必须选}else if (dist[i][j] == dist[i][t] + g[t][j]) //长度相等res[i][j] = max(res[i][j], res[i][t] + cost[j]); //选择价值大的}}}
}signed main()
{cin >> n;for (int i = 1; i <= n; i ++)for (int j = 1; j <= n; j ++)g[i][j] = 1e18;for (int i = 1; i <= n; i ++)cin >> cost[i];for (int i = 1; i <= n; i ++){string s;cin >> s;for (int j = 0; j < n; j ++)if (s[j] == 'Y')g[i][j + 1] = 1; //边权赋值为1}dijkstra();cin >> m;int a, b;while (m --){cin >> a >> b;if (dist[a][b] >= 1e18) cout << "Impossible" << endl;else cout << dist[a][b] << " " << res[a][b] + cost[a] << endl; //要加起点!}return 0;
}

今天就到这里了!

大家有什么问题尽管提,我都会尽力回答的!最后,除夕夜祝大家新年快乐!

吾欲您伸手,点的小赞赞。吾欲您喜欢,点得小关注!

AtCoder Beginner Contest 286——E - Souvenir相关推荐

  1. AtCoder Beginner Contest 202 D - aab aba baa(组合计数,字典序)

    整理的算法模板合集: ACM模板 点我看算法全家桶系列!!! 实际上是一个全新的精炼模板整合计划 Problem 有 AAA 和 aaa,BBB 个 bbb ,可以使用这 A+BA+BA+B 个字符任 ...

  2. AtCoder Beginner Contest 197 题解(A ~ F)

    整理的算法模板合集: ACM模板 点我看算法全家桶系列!!! 实际上是一个全新的精炼模板整合计划 目录 A - Rotate B - Visibility C - ORXOR D - Opposite ...

  3. AtCoder Beginner Contest 198 (A ~ F)题解

    目录 A. Div B. Palindrome with leading zeros C. Compass Walking D. Send More Money E. Unique Color F. ...

  4. AtCoder Beginner Contest 215 G - Colorful Candies 2

    AtCoder Beginner Contest 215 G - Colorful Candies 2 有n个糖果,每个糖果有着一个颜色a[i],每次拿k个糖果期望拿到E(x)个不同颜色的糖果,求出k ...

  5. AtCoder Beginner Contest 215 F - Dist Max 2

    AtCoder Beginner Contest 215 F - Dist Max 2 平面上有一系列的点(xi,yi)(x_i,y_i)(xi​,yi​),定义两点(xi,yi),(xj,yj)(x ...

  6. AtCoder Beginner Contest 215 E - Chain Contestant

    AtCoder Beginner Contest 215 E - Chain Contestant 给出一个只包括A~J的字符串,定义一种子序列为:在这个子序列中,相同的字符必定连续出现,求出这样的子 ...

  7. AtCoder Beginner Contest 204 F Hanjo 2

    AtCoder Beginner Contest 204 F Hanjo 2 H宽,W长的二维平面上,用1 * 1或者2 * 1的地砖来铺,要求铺满,求出方案数. 数据范围H <= 6, W & ...

  8. Caddi Programming Contest 2021(AtCoder Beginner Contest 193) 题解

    Caddi Programming Contest 2021(AtCoder Beginner Contest 193) A - Discount 打折浮点数除即可 B - Play Snuke 枚举 ...

  9. Mynavi Programming Contest 2021(AtCoder Beginner Contest 201)题解

    文章目录 A - Tiny Arithmetic Sequence B - Do you know the second highest mountain? C - Secret Number D - ...

最新文章

  1. 为什么阿里巴巴禁止使用BigDecimal的equals方法做等值比较?
  2. 请不要将抛出异常作为业务逻辑使用!!!
  3. 动手写一个简单版的谷歌TPU
  4. linux ftp登录命令_Linux使用pinky命令查询登录用户信息
  5. 测试CPU品牌和当前工作频率
  6. Python IDLE无法显示行号、Python IDLE shell里运行py文件
  7. 面试官:请手写一个带取消功能的延迟函数,axios 取消功能的原理是什么
  8. mycat和应用程序集成_企业应用程序集成简介
  9. outlook阅读html,Outlook HTML邮件中英文混排字体设置
  10. java 执行 awk_3.1 biostar lesson3 linux学习日记;java版本;awk
  11. Linux替代Windows系统软件比拼
  12. 5.1(统计正数和负数的个数然后计算这些数的平均值)
  13. HDU 5643 约瑟夫环的应用
  14. ASP.NET Core 基础教程总结 - ASP.NET Core 基础教程 - 简单教程,简单编程
  15. 超级详细的iptables介绍
  16. C语言实现方程组LU分解法和列主元LU分解法
  17. 钉钉 消息防撤回 分析
  18. 【JS】网页自动连点器,选取网页元素连续点击
  19. 六西格玛dfss_什么是六西格玛设计(DFSS)
  20. CPA二十一--划出和追加保证金(转载)

热门文章

  1. 网易云课堂html笔记
  2. 计算机应用基础试题学测,计算机应用基础测试题含答案
  3. 分享一些iOS实用Demo源码
  4. ubuntu或者树莓派截图工具flameshot下载与快捷键配置
  5. 杭州计算机学校排名,杭州市十大考研学校排名
  6. ubuntu16.04安装虚拟摄像头用于webrtc测试
  7. ubuntu9.10稳定运行QQ2008和迅雷
  8. halcon 底帽运算
  9. Python数据可视化第六节(坐标轴的定制)
  10. 今天把之前开发的捕鱼游戏加上摇杆手柄的支持,在家拿摇杆玩捕鱼,一样开心!