Yaroslav and Time - CodeForces 301B Dijkstra

B. Yaroslav and Time
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

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 xiyi (-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 test(s)
input
3 1000
1000
0 0
0 1
0 3
output
2000
input
3 1000
1000
1 0
1 1
1 2
output
1000


思路:因为所有的ai都小于d,所以可以转换成简单的Dijkstra,另外Floyd其实也可以,因为数据量才100.

AC代码如下:

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<cmath>
#include<cstdlib>
using namespace std;
#define INF 1000000000
#define MAX 110
typedef long long ll;
int dis[MAX][MAX],f[MAX],d,x[MAX],y[MAX],a[MAX];
bool vis[MAX];
int main()
{ int T,t,n,m,i,j,k,ret,pos;
  scanf("%d%d",&n,&d);
  for(i=2;i<n;i++)
   scanf("%d",&a[i]);
  for(i=1;i<=n;i++)
   scanf("%d%d",&x[i],&y[i]);
  for(i=1;i<=n;i++)
   for(j=1;j<=n;j++)
   { dis[i][j]=(abs(x[i]-x[j])+abs(y[i]-y[j]))*d;
     if(i!=j && j!=1 && j!=n)
      dis[i][j]-=a[j];
   }
  f[1]=0;
  for(i=2;i<=n;i++)
   f[i]=INF;
  for(i=1;i<=n;i++)
  { ret=INF;
    for(j=1;j<=n;j++)
     if(!vis[j] && f[j]<ret)
     { pos=j;
       ret=f[j];
     }
    vis[pos]=1;
    for(j=1;j<=n;j++)
     f[j]=min(f[j],f[pos]+dis[pos][j]);
  }
  printf("%d\n",f[n]);
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值