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 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.

Examples
input
3 1000
1000
0 0
0 1
0 3
output
2000
input
3 1000
1000
1 0
1 1
1 2
output
1000

一开始当作搜索来做,发现有问题,读题要全面分析,最后看题解,发现是一个最短路问题。
思路:知道是最短路后,就是一个简单题,用floyd过掉。
#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<stdlib.h>
#include<cmath>
using namespace std;
#define INF 99999999
int n,d;
int map[][]; void init()
{
for(int i=; i<=n; i++)
for(int j=; j<=n; j++)
{
if(i==j)
map[i][j]=;
else
map[i][j]=INF;
}
} int add[];
int loca[][];
int main()
{
scanf("%d%d",&n,&d);
init();
for(int i=; i<n; i++)
scanf("%d",&add[i]);
for(int i=; i<=n; i++)
scanf("%d%d",&loca[i][],&loca[i][]);
for(int i=; i<=n; i++)
for(int j=; j<=n; j++)
if(i!=j)
map[i][j]=(abs(loca[i][]-loca[j][])+abs(loca[i][]-loca[j][]))*d-add[j];
for(int k=; k<=n; k++)
for(int i=; i<=n; i++)
for(int j=; j<=n; j++)
map[i][j]=min(map[i][j],map[i][k]+map[k][j]);
printf("%d\n",map[][n]);
return ;
}

codeforces_302D的更多相关文章

随机推荐

  1. linux netlink套接字实现相似ss命令 ,统计套接字以及TCP信息

    參考了 ss的源码 以及 netlink相关资料:http://blog.csdn.net/scdxmoe/article/details/27711205 实现结果为: gcc netlink_di ...

  2. [转] Ubuntu/Linux Mint/Debian 安装 Java 8

    本PPA由webupd8制作,支持Ubuntu .04以及对应的Linux Mint版本,Oracle Java 8包提供JDK8 和 JRE8. sudo add-apt-repository pp ...

  3. mybatis之if else语句

    最近项目中遇到一个相同表设计,但是表名不同的sql语句操作. 在遇到这样的情况时候可以用一下方式: <choose> <when test=""> //.. ...

  4. PHP 给图片加边框

    /** * 给图片加边框 by liangjian 2014-06-19 * @param $ImgUrl 图片地址 * @param $SavePath 新图片保存路径 * @param $px 边 ...

  5. iOS开发——常见BUG——window决定程序的状态栏管理问题

    Xcode7升级之后遇到的问题   问题一: 老项目在Xcode6上运行没有任何问题,但在Xcode7上运行直接崩了! 经过一波分析: 发现是因为我顶部状态栏处添加了topWindow,用于处理Tab ...

  6. html鼠标事件

    jsp鼠标事件汇总 onclick 单击时触发的事件,这个比较常用 ondblclick 双击时触发的事件 onmoucedown 鼠标按下时触发的事件(个人觉得与onclick异曲同工) onmou ...

  7. 【BZOJ 5165】 树上倍增

    [题目链接] 点击打开链接 [算法] 树上倍增,时间复杂度 : O(qklog(n)) [代码] #include<bits/stdc++.h> using namespace std; ...

  8. DTV_SI 汇总 & 兼谈LCN

    前言 本章主要对数字广播DVB做一个系统的概况的描述,以及一些spc的相关的内容,虽然流程分析的不多,但是做为后续 章节资料的源泉,也是不可或缺的. 一. ATSC和DVB数字电视系统的比较 本文的主 ...

  9. hdu1213 并查集不压缩

    题意:题意:一个人请人吃饭,相互认识的朋友在一张桌子,相互认识的朋友的意思是如果A认识B,B认识C,那么A.B.C是朋友,对于每组输入输出桌子的张数. Sample Input 2 5 3 1 2 2 ...

  10. poj 2154 Color【polya定理+欧拉函数】

    根据polya定理,答案应该是 \[ \frac{1}{n}\sum_{i=1}^{n}n^{gcd(i,n)} \] 但是这个显然不能直接求,因为n是1e9级别的,所以推一波式子: \[ \frac ...