Codeforces Round #182 (Div. 1) B. Yaroslav and Time 最短路
题目链接:
http://codeforces.com/problemset/problem/301/B
B. Yaroslav and Time
time limit per test2 secondsmemory limit per test256 megabytes
#### 问题描述
> 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
题意
给你二维上的n个点,两个点的时间消耗为哈密顿距离*d,且到第i个点能补充a[i]的时间,问你在起始点需要买多少的时间才能保证你能够到终点。
题解
对于每条边(u,v),边权定为d*(u到v的哈密顿距离)-a[v],然后由点n到点1跑spfa最短路,这里注意:由于题目给的d和a的范围,导致都是正权图。
代码
#include<map>
#include<set>
#include<cmath>
#include<queue>
#include<stack>
#include<ctime>
#include<vector>
#include<cstdio>
#include<string>
#include<bitset>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<functional>
using namespace std;
#define X first
#define Y second
#define mkp make_pair
#define lson (o<<1)
#define rson ((o<<1)|1)
#define mid (l+(r-l)/2)
#define sz() size()
#define pb(v) push_back(v)
#define all(o) (o).begin(),(o).end()
#define clr(a,v) memset(a,v,sizeof(a))
#define bug(a) cout<<#a<<" = "<<a<<endl
#define rep(i,a,b) for(int i=a;i<(b);i++)
#define scf scanf
#define prf printf
typedef __int64 LL;
typedef vector<int> VI;
typedef pair<int,int> PII;
typedef vector<pair<int,int> > VPII;
const int INF=0x3f3f3f3f;
const LL INFL=0x3f3f3f3f3f3f3f3fLL;
const double eps=1e-8;
const double PI = acos(-1.0);
//start----------------------------------------------------------------------
const int maxn=111;
struct Edge {
int u,v,d;
Edge(int u,int v,int d):u(u),v(v),d(d) {}
};
int n,D;
int arr[maxn];
int mat[maxn][maxn];
PII pt[maxn];
int dis(int i,int j) {
return abs(pt[i].X-pt[j].X)+abs(pt[i].Y-pt[j].Y);
}
bool inq[maxn];
LL d[maxn];
LL spfa(int s) {
queue<int> Q;
clr(inq,0);
for(int i=1; i<=n; i++) d[i]=INFL;
d[s]=0,inq[s]=true,Q.push(s);
while(!Q.empty()) {
int u=Q.front();
Q.pop();
inq[u]=false;
for(int v=1;v<=n;v++){
if(v==u) continue;
if(d[v]>d[u]+mat[u][v]){
d[v]=d[u]+mat[u][v];
if(!inq[v]){
Q.push(v); inq[v]=1;
}
}
}
}
return d[1];
}
int main() {
scf("%d%d",&n,&D);
arr[1]=arr[n]=0;
for(int i=2; i<n;i++) scf("%d",&arr[i]);
for(int i=1; i<=n; i++) {
scf("%d%d",&pt[i].X,&pt[i].Y);
}
for(int i=1; i<=n; i++) {
for(int j=1; j<=n; j++) {
if(i==j) mat[i][j]=0;
else mat[i][j]=dis(i,j)*D-arr[j];
}
}
LL ans=spfa(n);
prf("%I64d\n",ans);
return 0;
}
//end-----------------------------------------------------------------------
Codeforces Round #182 (Div. 1) B. Yaroslav and Time 最短路的更多相关文章
- Codeforces Round #182 (Div. 1)题解【ABCD】
Codeforces Round #182 (Div. 1)题解 A题:Yaroslav and Sequence1 题意: 给你\(2*n+1\)个元素,你每次可以进行无数种操作,每次操作必须选择其 ...
- Codeforces Round #179 (Div. 2) B. Yaroslav and Two Strings (容斥原理)
题目链接 Description Yaroslav thinks that two strings s and w, consisting of digits and having length n ...
- Codeforces Round #182 (Div. 1 + Div. 2)
A. Eugeny and Array \(r-l+1\)是奇数时,和显然无法为0. 奇数的情况需要判断-1和1的个数是否大于等于长度的一半. B. Eugeny and Play List 模拟. ...
- Codeforces Round #Pi (Div. 2) E. President and Roads 最短路+桥
题目链接: http://codeforces.com/contest/567/problem/E 题意: 给你一个带重边的图,求三类边: 在最短路构成的DAG图中,哪些边是必须经过的: 其他的(包括 ...
- Codeforces Round #349 (Div. 1) B. World Tour 暴力最短路
B. World Tour 题目连接: http://www.codeforces.com/contest/666/problem/B Description A famous sculptor Ci ...
- Codeforces Round #303 (Div. 2) E. Paths and Trees 最短路+贪心
题目链接: 题目 E. Paths and Trees time limit per test 3 seconds memory limit per test 256 megabytes inputs ...
- Codeforces Round #303 (Div. 2)E. Paths and Trees 最短路
E. Paths and Trees time limit per test 3 seconds memory limit per test 256 megabytes input standard ...
- Codeforces Round #287 (Div. 2) E. Breaking Good [Dijkstra 最短路 优先队列]
传送门 E. Breaking Good time limit per test 2 seconds memory limit per test 256 megabytes input standar ...
- Codeforces Round #349 (Div. 2) D. World Tour 暴力最短路
D. World Tour A famous sculptor Cicasso goes to a world tour! Well, it is not actually a world-wid ...
随机推荐
- STM32的AFIO时钟什么时候需要开启
相比于普通单片机,STM32 拥有复杂的时钟系统,相应的控制器称为 RCC(Reset Clock Controller,复位与时钟控制器).每个外设都配备了外设时钟的开关,当我们不使用某个外设时,可 ...
- 根据URL地址获取域名
#coding:utf-8 import urllib #根据URL获取域名 def getdomain(url): proto, rest = urllib.splittype(url) host, ...
- 2018下学期C语言学习总结
怎么说呢,2018年下学期已经过去了,2019年的上学期又来了,在2018年没有达到自己想要的结果,希望2019年可以达到,加油!我希望我以后可以又快又准确的完成C语言的编程,学习好所以关于电脑的基础 ...
- PostgreSQL的psql客户端各种连接错误总结
磨砺技术珠矶,践行数据之道,追求卓越价值回到上一级页面:PostgreSQL基础知识与基本操作索引页 回到顶级页面:PostgreSQL索引页[作者 高健@博客园 luckyjackgao@g ...
- mfc 形式参数初始化
初始化函数形参 int add(int a,int b) { return a+b; } add(,); ,,char*s=NULL) { printf("a=%d,b=%d,c=%d\n& ...
- #6435. 「PKUSC2018」星际穿越
考场上写出了70分,现在填个坑 比较好写的70分是这样的:(我考场上写的贼复杂) 设\(L(i)=\min_{j=i}^nl(j)\) 那么从i开始向左走第一步能到达的就是\([l(i),i-1]\) ...
- [webpack]-webpack超级详细搭建实用前端环境
前言: webpack 超级实用前端环境搭建 一.我们日常使用的前端开发环境应该是怎样的? 构建我们需要发布的html,css ,js 文件 使用css 预处理器来编写样式 处理压缩图片 使用Babl ...
- JetBrains全家桶使用攻略
JetBrains全家桶使用攻略 今天狠狠心某宝买了一个key,可以使用15款开发软件,在此进行记录. 全家桶链接:https://www.jetbrains.com/products.html?fr ...
- 【转】新装的CentOS 7安装python3
https://blog.csdn.net/lovefengruoqing/article/details/79284573 centos7 自带有 python,但是却是 python2 版本的 p ...
- python的eval和json.loads(),json.dumps()
eval() 将字符串当成一个表达式去执行,可以想象成一个去字符串然后执行的操作. In [1]: s = '3*8' In [2]: eval(s) Out[2]: 24 eval()和json.l ...