UOJ003 - Magic Forest
This problem is from NOI 2014.
Description
You want to visit a hermit living in the Magic Forest. The forest can be seen as an undirected graph which has () nodes and () edges. Initially, you are at node while the hermit lives at node .
There are some monsters living in the Magic Forest, too. When someone passes an edge, the monsters will attack him. Luckily, you can take some guarding sprites with you when you depart. There are two types of sprites, type-A and type-B, and you can take as many as you want.
Every edge has two values and , which means that you need at least type-A sprites and type-B sprites to pass this edge. What is the minimal number of sprites you will need to take, if you choose the route optimally?
Input
- The first line contains two integers, the number of nodes , and the number of edges .
- The following lines each contain four integers , describing an undirected edge between and , which requires at least type-A sprites and at least type-B sprites.
Note that there might be multiple edges and self loops.
Output
One integer, the minimal number of sprites required to visit the hermit. If you can never reach the hermit, output instead.
Samples
Input 1
4 5
1 2 19 1
2 3 8 12
2 4 12 15
1 3 17 8
3 4 1 17
Output 1
32
Sample Explanation
- Choosing requires sprites.
- Choosing requires sprites.
- Choosing requires sprites.
- Choosing requires sprites.
So at least sprites are needed.