Codeforces 182 Div.2 D题

来源:互联网 发布:雅思口语网络课程 编辑:程序博客网 时间:2024/06/16 02:56

类型:dp,图论 难度:2

D. 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 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 test(s)
input
3 100010000 00 10 3
output
2000
input
3 100010001 01 11 2
output
1000

题意分析:图上有n个点,起点为1,终点为n,第2到n-1每个点上都有钱,走到这里可以获取,但是不能重复获取,从(xi,yi)走到(xj,yj)要消耗dist*d的金钱,dist为|xi-xj|+|yi-yj|,d为一个给定常数,求从1走到n最多需要起始时有多少钱。

可以转化为求图中任意两点最短距离,用Floyd算法求解,只不过公式变成:dp[i][j] = min(dp[i][k]+dp[k][j]-a[k]) a[k]即为第k个点的钱

#include<cstdio>#include<cstring>#define MIN(x,y) (x)<(y)?(x):(y)#define MAX(x,y) (x)>(y)?(x):(y)const int N=110;const int maxn=999999999;int n,d,a[N],pt[N][2],dp[N][N];int abs1(int x){if(x<0) return -x;return x;}int main(){while(~scanf("%d%d",&n,&d)){//memset(dp,0,sizeof(dp));int i,j,k;for(i=0;i<=n;i++)for(j=0;j<=n;j++)dp[i][j]=maxn;a[1]=a[n]=0;for(i=2;i<=n-1;i++)scanf("%d",a+i);for(i=1;i<=n;i++)scanf("%d%d",&pt[i][0],&pt[i][1]);for(i=1;i<n;i++){for(j=2;j<=n;j++){if(i==j) continue;int dis = abs1(pt[i][0]-pt[j][0])+abs1(pt[i][1]-pt[j][1]);dp[i][j]=dis*d;}}for(k=2;k<n;k++){for(i=1;i<n;i++)for(j=2;j<=n;j++){if(i==j || i==k || j==k) continue;int tmp = dp[i][k]+dp[k][j]-a[k];if(tmp<dp[i][j])dp[i][j]=tmp;}}printf("%d\n",dp[1][n]);}}


 

 

原创粉丝点击