Dijkstra's Nightmare (Easy)
Time limit1sMemory limit1024 MB
Construct a graph with negative edges on which the reference Dijkstra implementation terminates while processing at least 10000 vertices.
- Level
Medium6 of 10
- Topics
- Shortest path, Graph, Greedy
- Solved
- No attempts yet
Problem
Time limit: 1000 ms. Memory limit: 1024 MB.
Every computer scientist should be familiar with Dijkstra's algorithm, the basic algorithm for computing single-source shortest paths in a given graph. The algorithm maintains a set of active vertices. In each iteration, it selects and processes the active vertex with the smallest current distance. When processing a vertex, it examines the outgoing edges. If an edge can improve the distance to an adjacent vertex, the algorithm updates that distance and marks the adjacent vertex as active.
The algorithm is efficient when all edge lengths are non-negative. The reference implementation in this problem runs in time on such graphs, where is the number of edges and is the number of vertices.
Things get hairy once negative edge lengths are allowed. Finding the shortest simple path (a path with no repeated vertices) in such a graph is NP-hard. Some versions of Dijkstra's algorithm terminate quickly on such graphs but sometimes give incorrect results. The reference implementation does not, but it solves a slightly different problem: for each vertex , it finds the length of the shortest walk from the starting vertex to . A walk is a path that may repeat vertices and edges. If all edge lengths are positive, the shortest walk is always a simple path.
Sometimes no shortest walk from the starting vertex to some vertex exists, because for every walk there is an even shorter one. For such inputs the reference implementation never terminates.
In this problem, "graph" always means a directed weighted graph with no duplicate edges and no self-loops. and denote its numbers of vertices and edges. Vertices are numbered 0 through . The starting vertex is 0.
We want you to show that there are graphs on which Dijkstra's algorithm terminates but its time complexity is exponential in the number of vertices. The reference implementation contains a variable named PROCESSED_VERTICES. Construct any valid graph (defined below) such that the implementation terminates after finitely many steps and the final value of PROCESSED_VERTICES is at least 10 000.
Input
You are given the files d.cc and d.py. They contain equivalent reference implementations in C++11 and in Python 2.
Output
In a valid graph, and the length of each edge fits into a signed 32-bit integer variable.
Each graph should be output as a sequence of lines. The first line contains , the second line contains , and each of the following lines describes an edge with three integers: the numbers of its two endpoints (between 0 and ) followed by its length.
The output should contain exactly one such graph.