PAT甲题题解-1046. Shortest Distance (20)-做了一个假的最短路,水
一开始以为是最短路,结果是给你一个环,让你求环上两点之间的距离。。。那还做毛线
然而还是得做毛线
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
#include <iostream> #include <cstdio> #include <algorithm> #include <string.h> #define INF 0x3f3f3f3f using namespace std; const int maxn=100000+5; int clockwise[maxn]; int main() { int n; int dis[maxn]; scanf("%d",&n); for(int i=1;i<=n;i++){ scanf("%d",&dis[i]); } memset(clockwise,0,sizeof(clockwise)); clockwise[1]=0; for(int i=2;i<=n;i++){ clockwise[i]=dis[i-1]; clockwise[i]+=clockwise[i-1]; } clockwise[n+1]=clockwise[n]+dis[n]; int m; scanf("%d",&m); int a,b,u,v; int ans; for(int i=0;i<m;i++){ scanf("%d %d",&a,&b); u=min(a,b); v=max(a,b); ans=INF; ans=min(ans,clockwise[v]-clockwise[u]); //顺时针的距离 ans=min(ans,clockwise[n+1]-clockwise[v]+clockwise[u]);//逆时针的距离 printf("%d\n",ans); } return 0; }