Time Limit: 2000MS | Memory Limit: 262144KB | 64bit IO Format: %I64d & %I64u |
Description
Yaroslav is playing a game called "Time". The game has a timer showing the lifespan he's got left. As soon as the timer shows 0, Yaroslav's character dies and the game ends. Also, the game has n clock stations, station number i is at point (xi, yi) of the plane. As the player visits station number i, he increases the current time on his timer by ai. The stations are for one-time use only, so if the player visits some station another time, the time on his timer won't grow.
A player spends d·dist time units to move between stations, where dist is the distance the player has covered and d is some constant. The distance between stations i and j is determined as|xi - xj| + |yi - yj|.
Initially, the player is at station number 1, and the player has strictly more than zero and strictly less than one units of time. At station number 1 one unit of money can increase the time on the timer by one time unit (you can buy only integer number of time units).
Now Yaroslav is wondering, how much money he needs to get to station n. Help Yaroslav. Consider the time to buy and to increase the timer value negligibly small.
Input
The first line contains integers n and d(3 ≤ n ≤ 100, 103 ≤ d ≤ 105) — the number of stations and the constant from the statement.
The second line contains n - 2 integers: a2, a3, ..., an - 1(1 ≤ ai ≤ 103). The next n lines contain the coordinates of the stations. The i-th of them contains two integers xi, yi(-100 ≤ xi, yi ≤ 100).
It is guaranteed that no two stations are located at the same point.
Output
In a single line print an integer — the answer to the problem.
Sample Input
3 1000 1000 0 0 0 1 0 3
2000
3 1000 1000 1 0 1 1 1 2
1000
Source
Codeforces Round #182 (Div. 1)
Yaroslav 在玩一个游戏,还是走过来走过去的那种,有n个点,人物在1的位置上,终点在n,每走一步都会花费时间d,但是每到一个点,都可以释放一次技能,让时间回复a[ j ],但是每个点的技能只能用一次,走的时候只能上下左右的走,也就是说距离就是abs(x[i]-x[j])+abs(y[i]-y[j]),两个站点之间分花费就是dis*d,到一个站点的时候恢复时间就行,其实就是一个最短路,点的数量很少,所以弗洛伊德就好,不会超时
#include<cstdio>
#include<cstring>
#include<queue>
#include<algorithm>
using namespace std;
#define INF 0x3f3f3f
int map[200][200],a[200],n,d;
struct node
{
int x,y;
}p[200];
int dis(node s1,node s2)
{
return abs(s1.x-s2.x)+abs(s1.y-s2.y);
}
int main()
{
while(scanf("%d%d",&n,&d)!=EOF)
{
memset(a,0,sizeof(a));
for(int i=2;i<n;i++)
scanf("%d",&a[i]);
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
{
if(i==j) map[i][j]=0;
else
map[i][j]=map[j][i]=INF;
}
for(int i=1;i<=n;i++)
{
scanf("%d%d",&p[i].x,&p[i].y);
for(int j=1;j<i;j++)
{
map[i][j]=map[j][i]=dis(p[i],p[j])*d-a[j];
}
}
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
{
if(i==j) continue;
map[i][j]=dis(p[i],p[j])*d-a[j];
}
}
for(int k=1;k<=n;k++)
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
{
if(i==j) continue;
map[i][j]=min(map[i][j],map[i][k]+map[k][j]);
}
}
printf("%d\n",map[1][n]);
}
return 0;
}