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 ...
随机推荐
- LeetCode第[19]题(Java):Remove Nth Node From End of List(删除链表的倒数第N个节点)
题目:删除链表的倒数第N个节点 难度:Medium 题目内容: Given a linked list, remove the n-th node from the end of list and r ...
- IE兼容性测试工具IETester
IE兼容性测试工具:IETester 1.这种做法,不能做到100%的覆盖: 2.实际的业务场景会比IEtester更符合.
- Selenium学习笔记(1) - 自动化测试体系与原理
技术体系 基于代码的测试(Code-Based Testing) 基于协议的测试(Protocol-Based Testing) 基于界面的测试(GUI-Based Testing) 工作原理 基于代 ...
- springboot打Jar包和War包
一:打JAR包 在工程的pom.xml中添加以下依赖 <build> <plugins> <plugin> <groupId>org.springfra ...
- 一 web爬虫,requests请求
requests请求,就是用python的requests模块模拟浏览器请求,返回html源码 模拟浏览器请求有两种,一种是不需要用户登录或者验证的请求,一种是需要用户登录或者验证的请求 一.不需要用 ...
- null与""的区别
两者的区别与 “数字0和没有不是同一种概念”是一个道理.null是空对象,""是空字符串null可以赋值给任何对象,而""就不行了,只能赋值给字符串对象如:St ...
- [eShopOnContainers 学习系列] - 03 - 在远程 Ubuntu 16.04 上配置开发环境
直接把 md 粘出来了,博客园的富文本编辑器换成 markdown,没啥效果呀 ,先凑合吧.实在不行换地方 # 在远程 Ubuntu 16.04 上配置开发环境 ## 零.因 为什么要用这么麻烦的 ...
- LeetCode OJ:Plus One (加1)
Given a non-negative number represented as an array of digits, plus one to the number. The digits ar ...
- windows下memcached安装以及php_memcache.dll扩展
http://kimi.it/258.html http://kimi.it/259.html https://www.cnblogs.com/elenaPeng/p/6877530.html htt ...
- .net 学习路线感想(转)
从上到大学到现在工作,已经有六年多了,发现学习编程到以开发为工作也是一个挺长的过程的. 大学中,从c语言到java.C#到其他各种语言的学习,还有其他知识的学习如:数据库(oracle.sql Ser ...