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.
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.
In a single line print an integer — the answer to the problem.
3 1000
1000
0 0
0 1
0 3
2000
3 1000
1000
1 0
1 1
1 2
1000
思路:最短路,map[i][j] = d*(|x[i]-x[j]| + |y[i]-y[j]|) - add[i]
1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<cmath> 5 #include<cstdlib> 6 using namespace std; 7 long long int map[105][105],vis[105],dist[105],add[105]; 8 int n,d; 9 void init() 10 { 11 memset(vis,0,sizeof(vis)); 12 for(int i = 0; i < 105; i ++) 13 { 14 for(int j = 0; j < 105; j ++) 15 { 16 map[i][j] = 1 << 30; 17 } 18 } 19 return ; 20 } 21 22 int main() 23 { 24 int x[105],y[105]; 25 int ans,k; 26 //freopen("in.c","r",stdin); 27 while(~scanf("%d%d",&n,&d)) 28 { 29 init(); 30 for(int i = 2; i <= n-1; i ++) 31 scanf("%d",&add[i]); 32 for(int i = 1; i <= n; i ++) 33 scanf("%d%d",&x[i],&y[i]); 34 for(int i = 1; i <= n; i ++) 35 { 36 for(int j = 1; j <= n; j ++) 37 map[i][j] = (abs(x[i] - x[j]) +abs(y[i] - y[j]))*d - add[i]; 38 } 39 // for(int i = 1;i <= n;i ++) 40 // { 41 // for(int j = 1;j <= n;j ++) 42 // {if(i != j) printf("%d<-->%d == %d ",i,j,map[i][j]),printf(" ");} 43 // } 44 for(int i = 1;i <= n;i ++) 45 dist[i] = map[1][i]; 46 ans = 0; 47 vis[1] = 1; 48 for(int i = 1;i <= n;i ++) 49 { 50 long long int min = 1 << 30; 51 for(int j = 1;j <= n;j ++) 52 { 53 if(!vis[j] && min > dist[j]) 54 { 55 k = j; 56 min = dist[j]; 57 } 58 } 59 vis[k] = 1; 60 for(int j = 1;j <= n;j ++) 61 { 62 if(!vis[j] && dist[j] > map[k][j] + dist[k]) 63 dist[j] = map[k][j] +dist[k]; 64 } 65 } 66 printf("%d ",dist[n]); 67 } 68 return 0; 69 }