1046 Shortest Distance
题意:
给出n个点,再给出当前点到下一点的距离,计算两点之间的最短距离
分析:
此题就是给你送分的,即使不会图论的也会写这个!!!!!
因为此题只有两条路径可以到达目的地,所以只要用一个数组就行
Sum[i]用来记录上一点到这点的距离之和
接下来请看代码
#include<stdio.h>
#include<cstring>
#include<math.h>
#include<iostream>
#include<algorithm>
#include<cstdlib>
using namespace std;
int sum[100000] = { 0 };//用来记录上一点到当前点的距离之和
int dist[1000000] = { 0 };
int main()
{
int total = 0;
int n;
cin >> n;
for (int i = 1; i <= n; i++)
{
cin >> dist[i];
total += dist[i];
sum[i] = sum[i - 1] + dist[i];
}
int k;
int a, b;
cin >> k;
while (k--)
{
cin >> a >> b;
if (a > b)swap(a, b);
int temp = sum[b - 1] - sum[a - 1];
cout << min(temp, total - temp) << endl;
}
return 0;
}
最后,就是这么简单哈。。。。