This page is still under construction.

Parts of this page are still being built. What you see may change.

Making test data 3

Time limit1sMemory limit128 MB

Summary
Construct the lexicographically smallest SSSP test file (at most T integers) on which optimized Bellman-Ford finishes under C iterations but Floyd-Warshall exceeds C, or report that none exists.
Level

Hard8 of 10

Topics
Graph, Shortest path, Greedy, Implementation
Solved
No attempts yet

Problem

Programming contests are common. Writing a good contest problem is hard, and making the test data is the hardest part. Good tests separate a program written to the intended algorithm from one that is not. They should also catch a program that is right on most inputs and wrong on a special case.

This task does not ask you to submit a shortest-path solver. It asks you to print one test file in the single-source shortest path (SSSP) format.

Sungkeun must build a file XX that separates code A (OptimizedBellmanFord) from code B (FloydWarshall). Both programs keep an iteration count in counter. If counter exceeds CC, the run is a time limit exceeded (TLE). In the sources in the hint, the literal 1000000 is replaced by CC in this problem.

File XX must satisfy all of the following.

  1. Code A must not TLE on XX.
  2. Code B must TLE on XX.
  3. XX consists of at most TT integers.

The SSSP input format is as follows.

The first line contains the number of vertices VV. Vertices are numbered 00 through V−1V-1. Then VV lines follow. The line for vertex ii (0≤i<V0 \le i < V) starts with nin_i, the number of outgoing edges, then nin_i pairs jj ww. jj is the head and ww is the weight. The next line contains the number of queries QQ. Each of the next QQ lines contains sks_k and tkt_k.

The file must also obey these constraints.

  • 1≤V≤3001 \le V \le 300
  • nin_i is a non-negative integer
  • 0≤j<V0 \le j < V
  • ∣w∣<106|w| < 10^6
  • 0≤∑ni≤50000 \le \sum n_i \le 5000
  • 1≤Q≤101 \le Q \le 10
  • 0≤sk,tk<V0 \le s_k, t_k < V
  • The graph has no cycle whose weights sum to a negative value

If several files work, choose the lexicographically smallest sequence of integers. Compare from the left. At the first position where they differ, the sequence with the smaller integer comes first. If one sequence is a prefix of the other, the shorter one comes first.

If no such file exists, print -1.

Input

The first line contains two integers CC and TT.

  • 0≤C≤1090 \le C \le 10^9
  • 1≤T≤1051 \le T \le 10^5

Output

If no valid file exists, print -1 on one line.

Otherwise print an SSSP input file. Separate integers on the same line with a single space. Do not put a trailing space at the end of a line.

Print VV on the first line. Then print VV lines. The line for vertex ii (with ii starting at 00) starts with nin_i, the number of outgoing edges, followed by nin_i pairs jj ww. If there are no outgoing edges, print only 0. Then print QQ. Then print QQ lines, each with ss and tt for one query.

Hint

Code A is optimized Bellman-Ford. Code B is Floyd-Warshall. In the sources below, the literal 1000000 is replaced by the input CC. If counter exceeds CC, the program stops as TLE.

OptimizedBellmanFord:

#define INF 1000000000

int i, j, u, vv, w_u_vv, V, n, w, Q, counter, s, t;
int dist[1000];
int AdjList[1000][1000];
int weight[1000][1000];
int sz[1000];
int change;

int main() {
  scanf("%d", &V);
  for (i = 0; i < V; i++) {
    scanf("%d", &n);
    sz[i] = 0;
    while (n--) {
      scanf("%d %d", &j, &w);
      AdjList[i][sz[i]] = j;
      weight[i][sz[i]] = w;
      sz[i]++;
    }
  }
  counter = 0;
  scanf("%d", &Q);
  while (Q--) {
    scanf("%d %d", &s, &t);
    for (i = 0; i < V; i++) dist[i] = INF;
    dist[s] = 0;
    for (i = 0; i < V-1; i++) {
      change = 0;
      for (u = 0; u < V; u++)
        for (j = 0; j < sz[u]; j++) {
          counter++;
          if (counter > 1000000) {
            printf("TLE because iteration counter > 1000000\n");
            return 1;
          }
          vv = AdjList[u][j];
          w_u_vv = weight[u][j];
          if (dist[u] + w_u_vv < dist[vv]) {
            dist[vv] = dist[u] + w_u_vv;
            change = 1;
          }
        }
      if (!change)
        break;
    }
    printf("%d\n", dist[t]);
  }
  printf("The value of counter is: %d\n", counter);
  return 0;
}

FloydWarshall:

int i, j, k, V, n, w, M[300][300], counter, Q, s, t;

int main() {
  scanf("%d", &V);
  for (i = 0; i < V; i++)
    for (j = i+1; j < V; j++)
      M[i][j] = M[j][i] = 1000000000;
  for (i = 0; i < V; i++)
    M[i][i] = 0;
  for (i = 0; i < V; i++) {
    scanf("%d", &n);
    while (n--) {
      scanf("%d %d", &j, &w);
      if (w < M[i][j]) M[i][j] = w;
    }
  }
  counter = 0;
  for (k = 0; k < V; k++)
    for (i = 0; i < V; i++)
      for (j = 0; j < V; j++) {
        counter++;
        if (counter > 1000000) {
          printf("TLE because iteration counter > 1000000\n");
          return 1;
        }
        if (M[i][k] + M[k][j] < M[i][j]) M[i][j] = M[i][k] + M[k][j];
      }
  scanf("%d", &Q);
  while (Q--) {
    scanf("%d %d", &s, &t);
    printf("%d\n", M[s][t]);
  }
  printf("The value of counter is: %d\n", counter);
  return 0;
}

Floyd-Warshall increments counter exactly V3V^3 times, regardless of the number of edges or queries. Optimized Bellman-Ford stops a query early when a round makes no update.

Examples3

  1. Example 1

    Input
    1000000 100000
    
    Expected output
    101
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    1
    0 0
  2. Example 2

    Input
    0 5
    
    Expected output
    1
    0
    1
    0 0
  3. Example 3

    Input
    8 6
    
    Expected output
    -1