B - Split Ticketing Editorial by en_translator
Original proposer: vwxyz
It is sufficient to inspect each triple \(a,b,c\) and check if it satisfies the condition in the problem statement.
Possible approaches include:
① Initialize a string-type variable with No. If a conforming triple is found, change it to Yes; print it at last.
② Initialize a boolean-type variable with false. If a conforming triple is found, change it to true; print it at last.
③ When a conforming triple is found, print Yes and terminate the program. Print No at last.
Different approaches have different advantages and disadvantages, so it is advised to learn a variety of implementation approaches so you can choose an appropriate one depending on problems or situations.
In Python, it is possible to combine break and else, but as in this problem, using them in a triply-nested loop may result in an unintuitive implementation, so it is discouraged. (We will present it as ④ though.)
Sample code in Python ①
N=int(input())
C=[[None]*(i+1)+list(map(int,input().split())) for i in range(N-1)]
ans="No"
for a in range(N):
for b in range(a+1,N):
for c in range(b+1,N):
if C[a][b]+C[b][c]<C[a][c]:
ans="Yes"
print(ans)
Sample code in Python ②
N=int(input())
C=[[None]*(i+1)+list(map(int,input().split())) for i in range(N-1)]
bl=False
for a in range(N):
for b in range(a+1,N):
for c in range(b+1,N):
if C[a][b]+C[b][c]<C[a][c]:
bl=True
if bl:
print("Yes")
else:
print("No")
Sample code in Python ③
N=int(input())
C=[[None]*(i+1)+list(map(int,input().split())) for i in range(N-1)]
for a in range(N):
for b in range(a+1,N):
for c in range(b+1,N):
if C[a][b]+C[b][c]<C[a][c]:
print("Yes")
exit()
print("No")
Sample code in Python ④
N=int(input())
C=[[None]*(i+1)+list(map(int,input().split())) for i in range(N-1)]
ans="No"
for a in range(N):
for b in range(a+1,N):
for c in range(b+1,N):
if C[a][b]+C[b][c]<C[a][c]:
print("Yes")
break
else:
continue
break
else:
continue
break
else:
print("No")
posted:
last update: