Codeforces Yaroslav and Time(最短路)
2013-10-06 23:51
357 查看
Codeforces Yaroslav and Time(最短路)
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 1000
1000
0 0
0 1
0 3
Output
2000
Input
3 1000
1000
1 0
1 1
1 2
Output
1000
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 1000
1000
0 0
0 1
0 3
Output
2000
Input
3 1000
1000
1 0
1 1
1 2
Output
1000
分析:相当于点上带权的最短路,什么写法都可以过吧。。(前提是不要写挫。。) 下面是我的Dijkstra写法。。 代码: #include<iostream> #include<cmath> #include<cstdio> #include<cstdlib> #include<cstring> using namespace std; const int maxn=110; const int INF=0x7fffffff; int n,map[maxn][maxn],a[maxn],vis[maxn],dis[maxn]; struct Node { int x,y; }node[maxn]; int Manhaton_Dis(Node a,Node b) { return abs(a.x-b.x)+abs(a.y-b.y); } int Dijkstra(int s,int t) { int i,j,k,temp; memset(vis,0,sizeof(vis)); for(i=1;i<=n;i++) dis[i]=map[s][i]; dis[s]=0;vis[s]=1; for(i=1;i<=n;i++) { temp=INF; for(j=1;j<=n;j++) if(!vis[j]&&temp>dis[j]) { temp=dis[j]; k=j; } if(temp==INF) break; vis[k]=1; for(j=1;j<=n;j++) if(!vis[j]&&dis[j]>dis[k]+map[k][j]-a[k]) dis[j]=dis[k]+map[k][j]-a[k]; } return dis[t]; } int main() { int d,i,j,k; scanf("%d%d",&n,&d);//输入数据; for(i=2;i<n;i++) scanf("%d",&a[i]); for(i=1;i<=n;i++) scanf("%d%d",&node[i].x,&node[i].y); for(i=1;i<=n;i++) for(j=1;j<=n;j++) map[i][j]=INF;//初始化; for(i=1;i<=n;i++) for(j=1;j<=n;j++) map[i][j]=d*Manhaton_Dis(node[i],node[j]);//建图; cout<<Dijkstra(1,n)<<endl;//输出结果; return 0; }
相关文章推荐
- Codeforces Round #182 (Div. 1) B. Yaroslav and Time(二分+SPFA变形)
- 08/13 D -> codeforces Round #Pi div 2 E. President and Roads 最短路变形
- Codeforces Round #182 (Div. 1) D. Yaroslav and Divisors
- Codeforces Round #Pi (Div. 2) E. President and Roads(无向图缩点(有重边)+最短路)
- D. Okabe and City codeforces 最短路
- Codeforces Round #182 (Div. 1) B. Yaroslav and Time 最短路
- Codeforces Round #179 (Div. 2) B. Yaroslav and Two Strings
- Codeforces Round #179 (Div. 2) B (codeforces 296b) Yaroslav and Two Strings
- Codeforces Round #348 E. Little Artem and Time Machine(给你一个n,有三种选择,1表示增加一个值,2表示删除这个数的一个值,3表示查询)
- Danil and a Part-time Job codeforces dfs序,线段树
- Jzzhu and Cities CodeForces - 449B (最短路 + 最短路的条数)
- Codeforces Round #Pi (Div. 2) E. President and Roads 最短路,桥
- Codeforces Round #Pi (Div. 2) E. President and Roads (最短路+强连通求割边)
- Dima and Bacteria CodeForces - 400D [最短路+缩点] 好题~
- Codeforces Round #420 (Div. 2) D. Okabe and City (最短路)
- Codeforces Round #Pi (Div. 2) E. President and Roads 最短路+桥
- Codeforces Round #Pi (Div. 2) E. President and Roads tarjan+最短路
- Yaroslav and Time (简单最短路)
- Codeforces Round #442 (Div. 2) E. Danil and a Part-time Job DFS序+树链剖分+线段树区间^
- Fox and Card Game CodeForces - 388C