Official

B - Traveling Takahashi Problem Editorial by en_translator


Implement it as instructed in the problem statement. Note that he has to go back to the origin at last.

Many languages provide sqrt function that returns the square root of a given value.
Some languages also offer hypot(x,y) function, which calculates \(\sqrt{x^2+y^2}\).

In some languages, be careful of the number of digits in the fractional part when printing the answer.

Sample code (Python)

import math

N = int(input())
points = [tuple(map(int,input().split())) for _ in range(N)]
points = [(0,0)] + points + [(0,0)]

ans = 0
for i in range(N+1):
  px, py = points[i]
  qx, qy = points[i+1]
  ans += math.hypot(px-qx, py-qy)

print(ans)

Sample code (C)

#include<stdio.h>
#include<math.h>

int main(){
	int n;
	scanf("%d", &n);

	double ans = 0;
	int crrx = 0;
	int crry = 0;
	for(int i=0; i<n; i++){
		int x, y;
		scanf("%d%d", &x, &y);
		ans += hypot(x-crrx, y-crry);
		crrx = x;
		crry = y;
	}
	ans += hypot(crrx, crry);
	printf("%.10f\n", ans);
}

posted:
last update: