problem description
The task is really simple: given N exits on a highway which forms a simple cycle, you are supposed to tell the shortest distance between any pair of exits.
题目描述
任务非常简单:给定N个出口在高速公路上形成一个简单的循环,你应该告诉每个出口之间最短的距离。
Input Specification:
Each input file contains one test case. For each case, the first line contains an integer N (in [3,105]), followed by N integer distances D1D2 ⋯ DN , where Di is the distance between the i-th and the (i+1)-st exits, and DN is between the N-th and the 1st exits. All the numbers in a line are separated by a space. The second line gives a positive integer M (≤104), with M lines follow, each contains a pair of exit numbers, provided that the exits are numbered from 1 to N. It is guaranteed that the total round trip distance is no more than 107.
输入格式:
每个输入文件包含一个测试用例。对于每种情况,第一行包含一个整数 N(在 [3,105] ),后面跟着 N 个整数距离 D1D2DN ,其中 Di 是第 i 出口和 (i+1) 出口之间的距离,DN在第N出口和第1出口之间。一行中的所有数字都被一个空间分隔开。第二行给出一个正整数 M(≤104) ,后面跟着 M 行,每行包含一对出口号码,只要出口从 1 到 N 编号,保证往返总距离不超过 107 。
Output Specification:
For each test case, print your results in M lines, each contains the shortest distance between the corresponding given pair of exits.
输出格式:
对于每个测试用例,用M行打印结果,每一行包含对应的给定出口之间的最短距离。
Sample Input:
5 1 2 4 14 9
3
1 3
2 5
4 1
输入样例:
5 1 2 4 14 9
3
1 3
2 5
4 1
Sample Output:
3
10
7
输出样例:
3
10
7
样例解释
如图所示,共有 5 个结点,分别标号为 1、2、3、4、5,相邻两点的距离在图上给出。总共三个询问:
1 3:从 1 号点到 3 号点的最短距离为 3,路径为 1 → 2 → 3;
2 5:从 2 号点到 5 号点的最短距离为 10,路径为 2 → 1 → 5;
4 1:从 4 号点到 1 号点的最短距离为 7,路径为 4 → 3 → 2 → 1;
代码:
#include <stdio.h>
int main(){
int n,num[100000]={
0},sum=0,lenth[10000]={
0};
scanf("%d",&n);
for(int i=1;i<=n;i++){
scanf("%d",&num[i]);
sum += num[i];