Codeforces Round 214 (Div. 2)
D
Dima and Trap Graph
Codeforces
2000
原题
← CE →
翻译中…

Dima and Inna love spending time together. The problem is, Seryozha isn't too enthusiastic to leave his room for some reason. But Dima and Inna love each other so much that they decided to get criminal...

翻译中…

Dima constructed a trap graph. He shouted: "Hey Seryozha, have a look at my cool graph!" to get his roommate interested and kicked him into the first node.

翻译中…

A trap graph is an undirected graph consisting of $*n*$ nodes and $*m*$ edges. For edge number $*k*$, Dima denoted a range of integers from $*l*_{*k*}$ to $*r*_{*k*}$ $(*l*_{*k*} ≤ *r*_{*k*})$. In order to get out of the trap graph, Seryozha initially (before starting his movements) should pick some integer (let's call it $*x*$), then Seryozha must go some way from the starting node with number $1$ to the final node with number $*n*$. At that, Seryozha can go along edge $*k*$ only if $*l*_{*k*} ≤ *x* ≤ *r*_{*k*}$.

翻译中…

Seryozha is a mathematician. He defined the loyalty of some path from the $1$-st node to the $*n*$-th one as the number of integers $*x*$, such that if he initially chooses one of them, he passes the whole path. Help Seryozha find the path of maximum loyalty and return to his room as quickly as possible!

翻译中…

Input

翻译中…

The first line of the input contains two integers $*n*$ and $*m*$ $(2 ≤ *n* ≤ 10^{3}, 0 ≤ *m* ≤ 3·10^{3})$. Then follow $*m*$ lines describing the edges. Each line contains four integers $*a*_{*k*}$, $*b*_{*k*}$, $*l*_{*k*}$ and $*r*_{*k*}$ $(1 ≤ *a*_{*k*}, *b*_{*k*} ≤ *n*, 1 ≤ *l*_{*k*} ≤ *r*_{*k*} ≤ 10^{6})$. The numbers mean that in the trap graph the $*k*$-th edge connects nodes $*a*_{*k*}$ and $*b*_{*k*}$, this edge corresponds to the range of integers from $*l*_{*k*}$ to $*r*_{*k*}$.

翻译中…

Note that the given graph can have loops and multiple edges.

翻译中…

Output

翻译中…

In a single line of the output print an integer — the maximum loyalty among all paths from the first node to the $*n*$-th one. If such paths do not exist or the maximum loyalty equals 0, print in a single line "Nice work, Dima!" without the quotes.

翻译中…

Note

翻译中…

Explanation of the first example.

翻译中…

Overall, we have 2 ways to get from node 1 to node 4: first you must go along the edge 1-2 with range [1-10], then along one of the two edges 2-4.

翻译中…

One of them contains range [3-5], that is, we can pass through with numbers 3, 4, 5. So the loyalty of such path is 3.

翻译中…

If we go along edge 2-4 with range [2-7], then we can pass through with numbers 2, 3, 4, 5, 6, 7. The loyalty is 6. That is the answer.

翻译中…

The edge 1-2 have no influence on the answer because its range includes both ranges of the following edges.

翻译中…

样例

Input #1
4 4
1 2 1 10
2 4 3 5
1 3 1 5
2 4 2 7
Output #1
6
InputCopy OutputCopy
Input #2
5 6
1 2 1 10
2 5 11 20
1 4 2 5
1 3 10 11
3 4 12 10000
4 5 6 6
Output #2
Nice work, Dima!
InputCopy OutputCopy
题目信息
题号
366D
时间限制
3 s
内存限制
256 MB
标签
二分
数据结构
DFS 及类似
并查集
最短路
双指针

题面与样例版权归 Codeforces 及出题人所有;中文翻译由 AI 生成(CI 管线),如有出入请以原文为准。