C ENTRAL E UROPEAN O LYMPIAD IN I NFORMATICS
Munster,
Germany
July 5-12, 2003
Page 1 of 2
English
Day 1: square
Input File:
Output File: Source Code: square.pas/.c/.cpp
100 Points
Time Limit: 1 s
Memory Limit: 16 MB
Square
You are given a graph. All vertices are positioned on a N N grid of points (1 N 2003). Each
point is occupied by exactly one vertex. Each vertex has directed edges to its neighbor vertices to the
right and to the bottom, if they exist. These edges are weighted with an integer w (1 w 500 000).
Every path starting at the upper left vertex (at v1,1 ) to a vertex vx,y has the same weight. The weight
of a path is the sum of the weight of the edges along the path.
In the following graph with N = 3, every path from vertex v1,1 to v2,2 has weight 4. The only path
from v1,1 to v1,2 has weight 2.
x
1
2
2
3
y
5
4
3
4
1
2
1
You are given an integer L (1 L 2 000 000 000). Your task is to search for a vertex vx,y so that
a path from v1,1 to vx,y have exactly weight L. The weights are not directly given to your program,
and so it has to ask for a special weight by using a library. Your program is only allowed to ask for at
most 6667 weights.
The library offers the following functions:
getN() and getL() return the values for N and L. getWeight(x, y, direction) returns
the weight of the edge starting at vx,y to the right (direction=0) or to the bottom (direction=1).
If you have found a vertex vx,y so that a path from v1,1 to vx,y has exactly weight L, you have to
call solution(x, y). If no such vertex exists, call solution(-1, -1). Your program will
be terminated automatically after calling solution(x, y). If you perform more then 6667 calls
to getWeight() or your solution is incorrect, you will get no points for that testcase.
This task has neither input nor output files.
C ENTRAL E UROPEAN O LYMPIAD IN I NFORMATICS
Munster,
Germany
July 5-12, 2003
Page 2 of 2
English
Day 1: square
Library functions
C/C++
int getN(void)
int getL(void)
int getWeight(int x, int y, int direction)
void solution(int x, int y)
Pascal
function getN: Longint
function getL: Longint
function getWeight(x, y, direction: Longint):
procedure solution(x, y: Longint)
Longint
You can find a sample implementation of the library in /ceoi or c:\ceoi, but your program
will be evaluated with another implementation.
For testing and debugging you can create the file square.in, which will be read by the sample
library. The first line of square.in must contain the integers N and L. Each of the following
N lines contains exactly N 1 integers, the weights of the horizontal edges. After that, the following
N 1 lines contain N integers, the weights of the vertical edges.
The library is to be included via #include "square lib.h" or uses square lib.
Please note that the sample library you can use for testing is rather slow and memory consuming.
This is because it has to read your input file into memory. Nevertheless you can assume that the
evaluation library doesnt need any memory or running time at all.
Sample input for library
square.in
3 4
1 2
2 3
1 1
2 3 4
5 4 2
Protocol
getN() -> 3
getL() -> 4
getWeight(1,1,0) -> 1
getWeight(2,1,1) -> 3
solution(2,2)