CodeForces

来源:互联网 发布:sql server要钱吗 编辑:程序博客网 时间:2024/05/17 22:18

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 hasn clock stations, station number i is at point (xi, yi) of the plane. As the player visits station numberi, he increases the current time on his timer byai. 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, wheredist is the distance the player has covered andd is some constant. The distance between stationsi 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 number1 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 nextn lines contain the coordinates of the stations. Thei-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.

Example
Input
3 100010000 00 10 3
Output
2000
Input
3 100010001 01 11 2
Output

1000

题意:给你n个点,给你d的值,然后给你2~n-1的补给值,再后面是每个点的坐标,问你从1到n的最时间是多少。两点的时间是d* |xi - xj| + |yi - yj|..

思路:将n个点的转变成地图,Map[i][j]存的是i到j的时间 。

#include<iostream>#include<cstdio>#include<functional>#include<queue>#include<cmath>#include<cstring>using namespace std;#define N 99999999int n,d;int Map[105][105];int v[105];struct node {    int x;    int y;} a[105];int dis(node a,node b) {    return abs(a.x-b.x)+abs(a.y-b.y);}int main() {    int i,j,k;    while(scanf("%d%d",&n,&d)!=EOF) {        memset(v,0,sizeof(v));        for(i=2; i<n; i++)            scanf("%d",&v[i]);//每个点的补给值               for(i=0;i<=n;i++){            for(j=0;j<=n;j++){                if(i==j) Map[i][j]=0;                else Map[i][j]=N;//对地图初始化            }        }         for(i=1; i<=n; i++) {            scanf("%d%d",&a[i].x,&a[i].y);//存一下每个点的坐标        }        for(i=1; i<=n; i++) {            for(j=1; j<=n; j++) {                if(i==j) continue;                Map[i][j]=d*dis(a[i],a[j])-v[j];//将坐标转化地图             //   printf("d=%d dis=%lf\n",d,dis(a[i].x,a[i].y,a[j].x,a[j].y));            //    printf("i=%d j=%d Map=%lf\n",i,j,Map[i][j]);            }        }        /*        for(i=1;i<=n;i++){            for(j=1;j<=n;j++){                printf("%d ",Map[i][j]);            }            printf("\n");        }        */            for(k=1; k<=n; k++) {                for(i=1; i<=n; i++) {                    for(j=1; j<=n; j++) {                        // printf("i=%d j=%d k=%d\n",i,j,k);                        // printf("M[i][j]=%d  M[i][k]=%d  M[k][j]=%d v[k]=%d\n",Map[i][j],Map[i][k],Map[k][j],v[k]);                        if(i==j)                        continue;                        if(Map[i][j]>(Map[i][k]+Map[k][j]))                            Map[i][j]=Map[i][k]+Map[k][j];//弗洛伊德                        //   printf("Map[i][j]=%d\n",Map[i][j]);                    }                }            }            printf("%ld\n",Map[1][n]);//输出1到n的值    }}


0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 思想晚熟的人怎么办 孩子学不好数学怎么办 做作业速度慢怎么办 孩子碎头发太多怎么办 孩子碎发太多怎么办 孩子的作业太多怎么办 爸爸是乙肝孩子怎么办 孩子出生没有奶怎么办 顺产后没有奶水怎么办 还在经常逃课怎么办 老板不重视自己怎么办 孩子不听话爱撒谎怎么办 老师针对我孩子怎么办 小孩动不动就哭怎么办 孩子太拧应该怎么办 孩子贪玩不爱学习怎么办 高二了还不学怎么办 小朋友不喜欢上幼儿园怎么办 一岁半的宝宝不听话怎么办 老公是个窝囊废怎么办 幼儿园老师不喜欢家长怎么办 老师讨厌学生了怎么办 一个舍友很讨厌怎么办 家长对老师不满怎么办 教师被家长辱骂怎么办 如果老师喜欢自己怎么办 孩子不思进取逃避学习怎么办 老师拿孩子泄愤怎么办 老师对小孩不好怎么办 孩子被老师骂怎么办 孩子幼儿园被打怎么办 两个家长吵架老师怎么办 家长和老师矛盾怎么办 孩子特别害怕老师怎么办? 老师排挤孤立孩子怎么办 很害怕一件事怎么办 高中老师误会我怎么办 家长讹老师老师怎么办 小孩子老说头疼怎么办 孩子总是否定自己怎么办 孩子总是否定别人怎么办