JOI '14 Open P1 - Factories

View as PDF

Submit solution

Points: 20 (partial)
Time limit: 4.0s
Memory limit: 512M

Problem types
Allowed languages
C, C++
Contest Day 1 - JOI Open Contest

In IOI Kingdom, there are N cities numbered from 0 to N1. These cities are connected by N1 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. Assume the company CA requires the components of the company CB (CACB). In this case, they need to transport components from CB to CA. They may transport components from any of the factories of the company CB to any of the factories of the company CA. They need to choose factories appropriately to minimize the distance between factories.

Task

First, the number of cities and the information of roads in IOI Kingdom are given. Then, Q queries are given. Each query is written in the following form: the company Uj having factories in cities Xj,0,,Xj,Sj1 requires components of the company Vj having factories in cities Yj,0,,Yj,Tj1. Write a program which, for each query, returns the minimum distance to transport the components.

Implementation Details

You are to write a program which implements procedures to answer queries.
Your program should include the header file factories.h by #include "factories.h"
Your program should implement the following procedures.

Copy
void Init(int N, int A[], int B[], int D[])
  • This procedure is called only once in the beginning. The parameter N is the number of cities in IOI Kingdom. The parameters A, B and D are arrays of length N1. The elements A[i], B[i] and D[i] are three integers Ai, Bi and Di (0iN2) respectively. This means, for each i (0iN2), there is a road of length Di connecting the city Ai and the city Bi.
Copy
long long Query(int S, int X[], int T, int Y[])
  • This procedure is called for each of Q queries. In the query j, the parameters S and T are two integers Sj and Tj respectively. These are the numbers of cities where the companies Uj, Vj have factories respectively. The parameter X is an array of length Sj. The company Uj has factories in cities X[0],X[1],,X[S1]. The parameter Y is an array of length Tj. The company Vj has factories in cities Y[0],Y[1],,Y[T1].
    This procedure should return the minimum distance to transport components from the company Vj to the company Uj.

Constraints

All input data satisfy the following conditions:

  • 2N500000.
  • 1Q100000.
  • 0AiN1 (0iN2).
  • 0BiN1 (0iN2).
  • 1Di100000000 (0iN2).
  • AiBi (1iN2).
  • You can travel from any city to any other city through some of these roads.
  • 1SjN1 (0jQ1).
  • 0Xj,kN1 (0jQ1,0kSj1).
  • 1TjN1 (0jQ1).
  • 0Yj,kN1 (0jQ1,0kTj1).
  • Xj,0,Xj,1,,Xj,Sj1,Yj,0,Yj,1,,Yj,Tj1 are different from each other (0jQ1).
  • S0+S1++SQ11000000.
  • T0+T1++TQ11000000.
Subtask 1 [15 points]

The following conditions are satisfied:

  • N5000.
  • Q5000.
Subtask 2 [18 points]

The following conditions are satisfied.

  • Si20 (0iQ1).
  • Ti20 (0iQ1).
Subtask 3 [67 points]

No additional constraints.

Sample Execution

Copy
Init(7, {0, 1, 2, 2, 4, 1}, {1, 2, 3, 4, 5, 6}, {4, 4, 5, 6, 5, 3});
Query(2, {0, 6}, 2, {3, 4}); //returns 12.
Query(3, {0, 1, 3}, 2, {4, 6}); //returns 3.
Query(1, {2}, 1, {5}); //returns 11.

Explanation for Sample

These are sample input and sample output of the sample grader.

  • In query 0, the company U0 has factories in the cities 0, 6, and the company V0 has factories in the cities 3, 4. The distance from the factory of the company V0 in the city 3 to the factory of the company U0 in the city 6 is minimum. The minimum distance is 12.
  • In the query 1, the company U1 has factories in the cities 0, 1, 3, and the company V1 has factories in the cities 4, 6. The distance from the factory of the company V1 in the city 6 to the factory of the company U1 in the city 1 is minimum. The minimum distance is 3.
  • In the query 2, the company U2 has factories in the city 2, and the company V2 has factories in the city 5. The distance from the factory of the company V2 in the city 5 to the factory of the company U2 in the city 2 is minimum. The minimum distance is 11.

Additional Sources

Since the official page does not seem to provide a grader, the following are made available for you to use.

factories.h

factories.cpp

grader.cpp

The code in factories.h includes the prototype functions that you need to implement, but do not write your code in this file. The code in factories.cpp includes a sample of the functions you should fill in to solve the problem. You should submit the contents of this file to the judge for grading. The code in grader.cpp includes the main function and should be used to run judge your program.

If you are using g++, then you can compile the program with g++ factories.cpp grader.cpp -std=c++11. If you are using an IDE, then consult Stack Overflow on how to add files into your project.


Comments

There are no comments at the moment.