I'm fairly familiar with input handling in Competitive Programming settings, but I can't make it work in Python for this problem (https://www.spoj.com/problems/MINDIST/) (my solution works in C++ so the problem in itself is not broken). What is the standard way to read input in the SPOJ platform?
Here, this is what will be fed to the stdin:
The first line of the input contains a single integer T, the number of test cases. T blocks follow.
For each test case, the first line contains two space-separated integer N (1<=N<=100000) and S (0<=S<=100000000). N-1 lines follow, each contains three integers X (1<=X<=N), Y (1<=Y<=N) and Z (1<=Z<=1000), denotes that there is an (undirected) edge weighted Z between node X and Y. The input is correct.
And an example of fed input would be:
2
5 2
1 2 5
2 3 2
2 4 4
2 5 3
8 6
1 3 2
2 3 2
3 4 6
4 5 3
4 6 4
4 7 2
7 8 3
MRE:
This (just the input handling) gets "runtime error, Non-Zero Exit Code" (but works locally and on online compilers such as ideone):
def get_int():
return int(input())
def get_ints():
return map(int, input().split())
T = get_int()
for _ in range(T):
N, S = get_ints()
for _ in range(N-1):
X, Y, Z = get_ints()
It seems the get_ints()
call is the problem because this:
def get_int():
return int(input())
def get_ints():
return map(int, input().split())
T = get_int()
for _ in range(T):
N, S = 2, 3
for _ in range(N-1):
X, Y, Z = 1, 2, 3
gets (as expected) "wrong answer".
I'm at loss as to why this happens.
The input data actually contains blank lines. If I just filter them out by adding this at the top, the verdict changes from "error" to "wrong answer":
def input(input=input):
while True:
line = input()
if line.strip():
return line
I consider their input invalid and your input handling correct. The blank lines shouldn't be there, or their input description should mention them. The way it's written, they're not ok.
I figured this out by submitting quite a few times with various tests, for example storing the split()
results in a temporary variable and doing while len(temp) != 2: pass
. That gave me "time limit exceeded", so I knew the number of elements on the line was wrong.