Traveling SCCC President 2
Time limit3sMemory limit1024 MB
Find the path from building 1 to building N whose road lengths have the smallest bitwise OR, and print that OR value.
- Level
Medium6 of 10
- Topics
- Bit manipulation, Union-find, Greedy
- Solved
- No attempts yet
Problem
Sangwon, the president of SCCC, a problem-solving club in the Department of Computer Science and Engineering at Soongsil University, wants to walk from the Information Science Building to the Hyeongnam Engineering Building to put up contest promotion posters.
The Soongsil University campus has buildings numbered to and roads numbered to . Each road connects two different buildings. The -th road connects building and building , and its length is . At most one road connects any pair of buildings, and every building can be reached from every other building by roads. The Information Science Building is building , and the Hyeongnam Engineering Building is building .
An ordinary person spends time equal to the sum of the road lengths used to travel between buildings. Sangwon, however, spends time equal to the bitwise OR of all the road lengths used. Find the minimum time Sangwon needs to travel from building to building .
Input
The first line contains the number of buildings and the number of roads , separated by a space.
From the second line onward, each of the next lines contains the two buildings connected by the -th road and its length , separated by spaces.
Output
Print the minimum time Sangwon needs to travel from building to building .
Constraints
- ,
- If , then and . No two roads connect the same pair of buildings.
- Every building is connected to the others by roads.
- All input values are integers.
Hint
For two integers and , the bitwise OR is defined as follows. In binary, if at least one of the digits of and is 1, then the digit of is 1. If both are 0, the digit of is 0.
For example, and , so .
The bitwise OR of integers is defined as . You can prove that this result does not change when the order of is changed.
Road lengths and the answer can exceed the 32-bit integer range, so C/C++ users should use the long long type and Java users should use the long type.
Because the input and output are large, using fast I/O as described in the language guide on pages 2 to 4 of the problem booklet is recommended.