One Pass Short
Time limit2sMemory limit512 MB
Construct a directed graph with edge weights 1 or -1, no negative cycle, yet a Bellman-Ford variant that runs N-2 rounds then checks would falsely report a negative cycle; minimize the edge count and lexicographic order.
- Level
Hard9 of 10
- Topics
- Graph, Shortest path, Implementation, Brute force
- Solved
- No attempts yet
Problem
Jigui implemented the Bellman-Ford algorithm to decide whether a directed graph whose edge weights are all or has a negative cycle.
- Set for every vertex .
- One round scans every edge in the order given in the input and assigns to . Each edge changes right away, so the next edge of the same round reads the changed values.
- Repeat the round times. It should have been times.
- Run one more round. If any value of changes during that round, report that a negative cycle exists.
Because step 3 runs one round too few, the code reports a negative cycle on a graph that has none. Build such a graph yourself and show that Jigui's code is wrong.
You are given the number of vertices . Print a directed graph on vertices whose edge weights are all or , that has no negative cycle, and that the code above declares to have one. The order in which you print the edges is the order in which the code scans them, so the order is part of the answer.
Many graphs satisfy the conditions. Print the one with the smallest number of edges . If several graphs remain, lay the printed edges out as the sequence , compare the sequences number by number, and print the one that comes first.
Input
The first line contains the number of vertices . ()
Output
On the first line print the number of vertices and the number of edges . ()
On each of the next lines print the start vertex , the end vertex , and the weight of one edge. (, , or )
Do not print the same pair twice.