AtCoder Beginner Contest 286——E - Souvenir
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相关推荐
- AtCoder Beginner Contest 202 D - aab aba baa(组合计数,字典序)
整理的算法模板合集: ACM模板 点我看算法全家桶系列!!! 实际上是一个全新的精炼模板整合计划 Problem 有 AAA 和 aaa,BBB 个 bbb ,可以使用这 A+BA+BA+B 个字符任 ...
- AtCoder Beginner Contest 197 题解(A ~ F)
整理的算法模板合集: ACM模板 点我看算法全家桶系列!!! 实际上是一个全新的精炼模板整合计划 目录 A - Rotate B - Visibility C - ORXOR D - Opposite ...
- AtCoder Beginner Contest 198 (A ~ F)题解
目录 A. Div B. Palindrome with leading zeros C. Compass Walking D. Send More Money E. Unique Color F. ...
- AtCoder Beginner Contest 215 G - Colorful Candies 2
AtCoder Beginner Contest 215 G - Colorful Candies 2 有n个糖果,每个糖果有着一个颜色a[i],每次拿k个糖果期望拿到E(x)个不同颜色的糖果,求出k ...
- 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 ...
- AtCoder Beginner Contest 215 E - Chain Contestant
AtCoder Beginner Contest 215 E - Chain Contestant 给出一个只包括A~J的字符串,定义一种子序列为:在这个子序列中,相同的字符必定连续出现,求出这样的子 ...
- AtCoder Beginner Contest 204 F Hanjo 2
AtCoder Beginner Contest 204 F Hanjo 2 H宽,W长的二维平面上,用1 * 1或者2 * 1的地砖来铺,要求铺满,求出方案数. 数据范围H <= 6, W & ...
- Caddi Programming Contest 2021(AtCoder Beginner Contest 193) 题解
Caddi Programming Contest 2021(AtCoder Beginner Contest 193) A - Discount 打折浮点数除即可 B - Play Snuke 枚举 ...
- Mynavi Programming Contest 2021(AtCoder Beginner Contest 201)题解
文章目录 A - Tiny Arithmetic Sequence B - Do you know the second highest mountain? C - Secret Number D - ...
最新文章
- 【stanford C++】容器III——Vector类
- wgan 不理解 损失函数_AI初识:深度学习中常用的损失函数有哪些?
- 我犯的错误--struts标签s:radio
- Ext 入门 (05) 打印+gridpanel()方法
- 一维信号双边滤波器_定义图上的各向异性、动态、频谱和多尺度滤波器
- 五行代码终极完美解决从IE6到Chrome所有浏览器的position:fixed;以及闪动问题
- 配置 --- vscode中react格式化解决方案
- Go中error类型的nil值和nil
- 在线文本并集计算工具
- python读取大文件太慢_强悍的Python读取大文件的解决方案
- 创建私服maven服务
- 【C#】eventlog类的使用
- 《异度神剑2》与犹太教卡巴拉略考
- python文件或文本加密(4种方法)
- java模拟手机号码发短信_java实现发送手机短信
- 推荐一个练习英语听力的网站
- 3dmax:3dmax三维VR渲染设置(VR发光贴图、VR灯光缓存、V-Ray焦散,渲染图中出现黑斑点的原因、插值类型)之详细攻略
- Linux学习-Boot Loader: Grub2
- c语言地震子波6,地震子波显示及合成地震记录
- sun.misc.BASE64Encoder详解