Making test data 3
Time limit1sMemory limit128 MB
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 that separates code A (OptimizedBellmanFord) from code B (FloydWarshall). Both programs keep an iteration count in counter. If counter exceeds , the run is a time limit exceeded (TLE). In the sources in the hint, the literal 1000000 is replaced by in this problem.
File must satisfy all of the following.
- Code A must not TLE on .
- Code B must TLE on .
- consists of at most integers.
The SSSP input format is as follows.
The first line contains the number of vertices . Vertices are numbered through . Then lines follow. The line for vertex () starts with , the number of outgoing edges, then pairs . is the head and is the weight. The next line contains the number of queries . Each of the next lines contains and .
The file must also obey these constraints.
- is a non-negative integer
- 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 and .
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 on the first line.
Then print lines. The line for vertex (with starting at ) starts with , the number of outgoing edges, followed by pairs . If there are no outgoing edges, print only 0.
Then print .
Then print lines, each with and 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 . If counter exceeds , 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 times, regardless of the number of edges or queries. Optimized Bellman-Ford stops a query early when a round makes no update.