CodeForces 366D Dima and Trap Graph

D. Dima and Trap Graph
time limit per test
3 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

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 ofnnodes andmedges. For edge numberk, Dima denoted a range of integers fromlktork(lk ≤ rk). In order to get out of the trap graph, Seryozha initially (before starting his movements) should pick some integer (let's call itx), then Seryozha must go some way from the starting node with number1to the final node with numbern. At that, Seryozha can go along edgekonly iflk ≤ x ≤ rk.

Seryozha is a mathematician. He defined theloyaltyof some path from the1-st node to then-th one as the number of integersx, 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 integersnandm(2 ≤ n ≤ 103, 0 ≤ m ≤ 3·103). Then followmlines describing the edges. Each line contains four integersak,bk,lkandrk(1 ≤ ak, bk ≤ n, 1 ≤ lk ≤ rk ≤ 106). The numbers mean that in the trap graph thek-th edge connects nodesakandbk, this edge corresponds to the range of integers fromlktork.

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 then-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.

Sample test(s)
input
4 4
1 2 1 10
2 4 3 5
1 3 1 5
2 4 2 7
output
6
input
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
Nice work, Dima!
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.



并查集。。。。。
按左端点排序后,枚举每一段,更新如果以这一段的右端点为区间结束点的长度。。。


#include 
#include 
#include 
#include 

using namespace std;

struct node
{
    int x,y,l,r;
}c[3300];

bool cmp(node a,node b)
{
    return a.lc[i].r) break;
            if(c[j].r





你可能感兴趣的:(CodeForces 366D Dima and Trap Graph)