In IOI Kingdom, there are cities numbered from 0 to . These cities are connected by roads through which you can pass in both directions. You can travel from any city to any other city by passing through some of these roads.
In IOI Kingdom, there are many companies producing special components. Each company produces only one kind of components. No two companies produce the same kind of components. Each company has at least one factory. Each factory is built in one of the cities. More than one company may have factories in the same city.
Sometimes, a company requires components of another company (). In this case, they need to transport components from to . They may transport components from any of the factories of the company to any of the factories of the company . They need to choose factories appropriately to minimize the distance between factories.
First, the number of cities and the information of roads in IOI Kingdom are given. Then, queries are given. Each query is written in the following form: the company having factories in cities requires components of the company having factories in cities . Write a program which, for each query, returns the minimum distance to transport the components.
You are to write a program which implements procedures to answer the queries. Your program should include the header file factories.h by #include "factories.h".
Your program should implement the following procedures.
void Init(int N, int A[], int B[], int D[])
This procedure is called only once in the beginning. The parameter is the number of cities in IOI Kingdom. The parameters , and are arrays of length . The elements , and are three integers and () respectively. This means, for each (), there is a road of length connecting the city and the city .
long long Query(int S, int X[], int T, int Y[])
This procedure is called for each of queries. In the query , the parameters and are two integers and respectively. These are the numbers of cities where the companies have factories respectively. The parameter is an array of length . The company has factories in cities . The parameter is an array of length . The company has factories in cities . This procedure should return the minimum distance to transport components from the company to the company .
You can download an archive file from the contest webpage which contains a sample grader to test your program. The archive file also contains a sample source file of your program.
A sample grader consists of one source file which is either grader.c or grader.cpp. For example, if your program is factories.c or factories.cpp, you run the following commands to compile your program.
C
gcc -O2 -std=c11 -o grader grader.c factories.c -lm
C++
g++ -O2 -std=c++11 -o grader grader.cpp factories.cpp
When the compilation succeeds, the executable file grader is generated.
Note that the actual grader is different from the sample grader. The sample grader will be executed as a single process, which will read input data from the standard input and write the results to the standard output.
The sample grader reads the following data from the standard input.
When the program terminates successfully, the sample grader writes to the standard output the values returned by Query one per line.
7 3
0 1 4
1 2 4
2 3 5
2 4 6
4 5 5
1 6 3
2 2
0 6
3 4
3 2
0 1 3
4 6
1 1
2
5
12
3
11
These are sample input and sample output of the sample grader.
All input data satisfy the following conditions.
The following conditions are satisfied.
The following conditions are satisfied.
There are no additional constraints.