PAT甲级1046题(最短路径)


1.题目介绍

1046

2.考察点,难度

模拟现实类,问题转换题,最短路径,难度易

3.解题代码

#include<cstdio>
#include<algorithm>
using namespace std;
int main()
{
	int sum = 0, N, M, a, b, temp, d;
	scanf("%d",&N);
	int distance[N+1] = {0};
	for (int i = 1; i <= N; i++){
		scanf("%d", &temp);
		sum += temp;
		distance[i] = sum;
	}
	scanf("%d", &M);
	for (int i = 0; i < M; i++){
		scanf("%d%d", &a, &b);
		if (a > b) swap(a, b);
		d = distance[b-1] - distance[a-1];
		printf("%d\n", min(d, sum-d));
	}
	return 0;
}

文章作者: Peyton
版权声明: 本博客所有文章除特別声明外,均采用 CC BY 4.0 许可协议。转载请注明来源 Peyton !
  目录