1046 Shortest Distance
题意:给出一个环和结点之间的距离,求任意两结点之间的最近距离。如图:

思路:令数组dis[i]表示1号结点逆时针至i号结点的距离,初始化dis[1]=0,其他值在输入是确定,即
| dis[i] | 0 | 1 | 3 | 7 | 21 |
| i | 1 | 2 | 3 | 4 | 5 |
这样,起点start和终点end之间逆时针方向的距离即为 d=abs(dis[end]-dis[start]) ,注意要加绝对值,不然,比如起点4到终点2的距离是dis[2]-dis[4]=-6,就为负了。同时用total记录环的总距离,起点start和终点end之间逆时针方向的距离即为 total-d 。对于任意两结点u,v之间的最短距离,就是其顺时针和逆时针距离最较小值。
代码:
#include <cstdio>
#include <cstdlib>
#define MIN(x,y) (x)<(y)?(x):(y)
;
};
int main()
{
;//total表示总的距离
scanf("%d",&n);
;i<=n;i++){
scanf("%d",&d);
total+=d;
dis[i+]=d+dis[i];
}
int m,s,e;
scanf("%d",&m);
;i<m;i++){
scanf("%d%d",&s,&e);
d=abs(dis[e]-dis[s]);
printf("%d\n",MIN(d,total-d));
} return 0;
}
1046 Shortest Distance的更多相关文章
- PAT 1046 Shortest Distance
1046 Shortest Distance (20 分) The task is really simple: given N exits on a highway which forms a ...
- 1046 Shortest Distance (20 分)
1046 Shortest Distance (20 分) The task is really simple: given N exits on a highway which forms a si ...
- PAT甲 1046. Shortest Distance (20) 2016-09-09 23:17 22人阅读 评论(0) 收藏
1046. Shortest Distance (20) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue The ...
- PAT 1046 Shortest Distance[环形][比较]
1046 Shortest Distance(20 分) The task is really simple: given N exits on a highway which forms a sim ...
- 1046 Shortest Distance (20 分)
1046 Shortest Distance (20 分) The task is really simple: given N exits on a highway which forms a si ...
- pat 1046 Shortest Distance(20 分) (线段树)
1046 Shortest Distance(20 分) The task is really simple: given N exits on a highway which forms a sim ...
- PAT 甲级 1046 Shortest Distance (20 分)(前缀和,想了一会儿)
1046 Shortest Distance (20 分) The task is really simple: given N exits on a highway which forms a ...
- PAT (Advanced Level) Practice 1046 Shortest Distance (20 分) 凌宸1642
PAT (Advanced Level) Practice 1046 Shortest Distance (20 分) 凌宸1642 题目描述: The task is really simple: ...
- [A] 1046 Shortest Distance
The task is really simple: given N exits on a highway which forms a simple cycle, you are supposed t ...
- PAT 甲级 1046 Shortest Distance
https://pintia.cn/problem-sets/994805342720868352/problems/994805435700199424 The task is really sim ...
随机推荐
- shell 数组操作
1. 定义数组: var_array=(one two three four five) 2.常用操作 获取数组长度: ${#var_array[@]} 获取所有数组元素: ${var_array[ ...
- CSS自定义字体(@font-face选择符)
@font-face是CSS中的一个模块,他主要是把自己定义的Web字体嵌入到你的网页中,随着@font-face模块的出现,我们在Web的开发中使用字体不怕只能使用Web安全字体. 语法规则: @f ...
- ultraedit使用记录
ultraedit使用记录 10:57:33 在日常的工作中,我经常用keil进行程序的编写等工作,不过在编写过程中Keil对中文的支持不是很好,容易发生问题:同事推荐我用ultraedit进行程序的 ...
- 51nod 1270 dp
http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1270 简单的线性dp,最近狂刷水题真的是...药丸 差值最大得话要么是峰 ...
- js中关于json常用的内容、js将数字保留两位小数
没什么好说的 保存起来 以后有个地方找 var json=eval("[]") //json定义 var s={"id":"xxx",& ...
- a, b交换与比较问题
1. 求a, b中较大的数,不使用if.?.switch等判断语句. 答案: 另一种思路是求两者的差,然后通过位运算判断差值的正负,不过个人觉得还是第一种各位简洁优雅. 2. 交换a, b的值,要求不 ...
- Struts08---全局结果和全局异常的配置
01.创建测试页面 <%-- 验证全局结果 和 局部结果 --%> <a href="user/UserAction_add">新增用户</a> ...
- 条款20:在传递对象的时候尽量用reference-to-constent来代替,pass-by-value
注意一下,c++的底层编译器而言,引用类型往往都是由指针来实现出来的,所以pass-by-reference实际意义上往往是传递了一个指针,这对于c++的内置类型来说往往传递一个reference更加 ...
- 【MFC】MFC DLEdit 设计属于自己的编辑框_鼠标悬停
MFC DLEdit 设计属于自己的编辑框 2012-02-04 13:00 by 捣乱小子, 3543 阅读, 5 评论, 收藏, 编辑 起因 无意间看到了大牛们写的自定义编辑框控件,于是找了个时间 ...
- tab显示不同数据
效果 核心代码 [js] [#escape x as (x)!?html]<!doctype html><html lang="zh-CN"><hea ...