[CF301B] Yaroslav and Time - 最短路
作者:互联网
[CF301B] Yaroslav and Time - 最短路
Description
坐标上有n个点,走一个单位距离要花d时间(只能横走纵走) \((3\le n\le 100,{10}^3\le d\le {10}^5)\)
除了起点和终点,其他的点都有个宝箱,每个宝箱可以加时间 \(a_i(1\le a_{i}\le10^{3})\)
输出从起点到终点,刚开始最少要带多少时间
Solution
关键是要注意到这个奇怪的数据范围,这意味着就算我们允许重复过点也不会重复,因此转化为简单的最短路
#include <bits/stdc++.h>
using namespace std;
#define int long long
const int N = 1e2 + 5;
int g[N][N], a[N], x[N], y[N], n, d;
signed main()
{
ios::sync_with_stdio(false);
cin >> n >> d;
for (int i = 2; i < n; i++)
cin >> a[i];
for (int i = 1; i <= n; i++)
cin >> x[i] >> y[i];
memset(g, 0x3f, sizeof g);
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= n; j++)
{
if (i == j)
continue;
g[i][j] = abs(x[i] - x[j]) * d + abs(y[i] - y[j]) * d - a[i];
}
}
for (int k = 1; k <= n; k++)
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++)
g[i][j] = min(g[i][j], g[i][k] + g[k][j]);
cout << g[1][n] << endl;
}
标签:le,宝箱,int,CF301B,短路,long,Yaroslav,Time 来源: https://www.cnblogs.com/mollnn/p/14648057.html