#include<stdio.h> int main(){ int n,m,i,j,d[100000]; scanf("%d", &n); for(i=1;i<=n;i++){ scanf("%d", &d[i]); } scanf("%d", &m); int start,end,temp,sum1,sum2; for(j=0;j<m;j++){ scanf("%d %d", &start, &end); sum1 = sum2 = 0; temp = start; while(temp != end){ sum1 += d[temp]; temp++; if(temp>n) temp = 1; } temp = start; while(temp != end){ if(temp==1) temp = n+1; sum2 += d[--temp]; } if(sum1 <= sum2){ printf("%d\n", sum1); }else { printf("%d\n", sum2); } } return 0; }
运行超时。
#include<stdio.h> int main(){ int n,m,i,j,d[100001],total=0,temp; scanf("%d", &n); //每个点存储的都是D1到该点的距离总和,D1存储环的总和 for(i=1;i<=n;i++){ scanf("%d", &temp); total += temp; d[i+1] = total; } d[1] = total; scanf("%d", &m); int start,end,t,sum1,sum2; for(j=0;j<m;j++){ scanf("%d %d", &start, &end); if(start > end){ t = start; start = end; end = t; } if(start == 1){ sum1 = d[end]; sum2 = d[1] - sum1; }else { sum1 = d[end] - d[start]; sum2 = d[1] - sum1; } if(sum1 <= sum2){ printf("%d\n", sum1); } else { printf("%d\n", sum2); } } return 0; }
正常。
相关推荐
为了在大图中找到两点之间的最短路径,我们先通过宽度优先搜索为每个点建立距离标签索引。关键是在宽度优先搜索是进行剪枝。
SecGDB: Graph Encryption for Exact Shortest Distance Queries with Efficient Updates
Status ShortestPath_Floyd(Graph &G,Distance &D,Path &P,Number &N) {int i,j,k; int s,t; if(G.kind==DG||G.kind==UDG) return ERROR; for(j=0;j;j++) for(k=0;k;k++) {D[j][k]=G.arcs[j][k]; P[j][k][0]=j...
matlab k shortest path code 供相关研究的学生使用
最短路问题The Shortest Path ProblemThe Shortest Path ProblemThe Shortest Path ProblemThe Shortest Path ProblemThe Shortest Path Problem
本程序是利用神经网络来计算五个城市的最短距离
最短路径算法实现 k-shortest-paths
基于D算法实现K shortest Path,封装得比较好。也很方便修改类的实现。
查找关键路径(shortest path)
computes shortest distance between two words
Dijkstra%DIJKSTRA Calculates the shortest distance and path between points on a map % using Dijkstra's Shortest Path Algorithm % % [DIST, PATH] = DIJKSTRA(NODES, SEGMENTS, SID, FID) % Calculates the ...
求解K短路问题的一种创新算子的单亲遗传算法源程序,该程序需要一个distance.txt文本文件,从中读取任意两点间距离数据。本程序所解决的为有26个节点的任意两点间K短路问题。
K-Shortest Paths 算法相关论文
严蔚敏数据结构与算法▲课本算法实现
OSPF(Open Shortest Path First开放式最短路径优先)是一个内部网关协议(Interior Gateway Protocol,简称IGP),用于在单一自治系统(autonomous system,AS)内决策路由。是对链路状态路由协议的一种实现,隶属内部...
sqrt((x2-x0)^2+(y2-y0)^2)-sqrt((x1-x0)^2+(y1-y0)^2)=t
典型的解决单源最短路径的贪心算法Dijkstra算法 可以运行
严蔚敏数据结构与算法▲课本算法实现
基于java的开发源码-最短路径算法实现 k-shortest-paths.zip 基于java的开发源码-最短路径算法实现 k-shortest-paths.zip 基于java的开发源码-最短路径算法实现 k-shortest-paths.zip 基于java的开发源码-最短路径...
It finds the shortest path between any two nodes.