P9735 题解

· · 题解

定义一个数组,存储第一个车站到第 i 个车站所需的时间,然后循环计算两个车站之间的所需的时间,并计算最小值和更新序号。

时间计算:
字符串 s=\tt{Patrik} 时,时间等于 t_i

将时间存入数组并计算最小值和序号即可。 ```cpp #include <bits/stdc++.h> using namespace std; const int N = 1145141; int n, t, y, p[N], minn = INT_MAX, ans; string s; int main() { cin >> n; for (int i = 2; i <= n; ++i) { cin >> s; if (s[0] == 'P') { cin >> t; p[i] = t; } if (s[0] == 'J') { cin >> y >> t; p[i] = p[y] + t; } } for (int i = 2; i <= n; ++i) { if (p[i] - p[i - 1] < minn) { minn = p[i] - p[i - 1]; ans = i; } } cout << minn << ' ' << ans - 1 << ' ' << ans; return 0; } ```