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 lkto rk (lk ≤ rk). 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 lk ≤ x ≤ rk.
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!
The first line of the input contains two integers n and m (2 ≤ n ≤ 103, 0 ≤ m ≤ 3·103). Then follow m lines describing the edges. Each line contains four integers ak, bk, lk and rk (1 ≤ ak, bk ≤ n, 1 ≤ lk ≤ rk ≤ 106). The numbers mean that in the trap graph the k-th edge connects nodes ak and bk, this edge corresponds to the range of integers from lk to rk.
Note that the given graph can have loops and multiple edges.
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.
4 4 1 2 1 10 2 4 3 5 1 3 1 5 2 4 2 7
6
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
Nice work, Dima!
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.
题意:
给你一个图,每条边有一个[a,b],只有在这个区间内的点才能通过,问满足能从1到n的最大连续区间的长度为多少。
思路:
枚举左区间,二分右区间,dfs验证连通性。
感想:
这题开始想到的是二分+dfs,但是想的是二分答案,不满足单调性,所以这个思路就放弃了,思维总是差那么一点点。
代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <string>
#include <map>
#include <stack>
#include <vector>
#include <set>
#include <queue>
//#pragma comment (linker,"/STACK:102400000,102400000")
#define maxn 1005
#define MAXN 100005
#define mod 1000000007
#define INF 0x3f3f3f3f
#define pi acos(-1.0)
#define eps 0.000001
typedef long long ll;
using namespace std;
int n,m,ans,cnt,ii,cxx,flag;
int pp[MAXN];
bool vis[MAXN];
int x[MAXN];
struct Node
{
int v,next;
int a,b;
} edge[MAXN];
void addedge(int u,int v,int a,int b)
{
cnt++;
edge[cnt].v=v;
edge[cnt].a=a;
edge[cnt].b=b;
edge[cnt].next=pp[u];
pp[u]=cnt;
}
void dfs(int u,int le,int ri)
{
if(flag) return ;
if(u==n)
{
flag=1;
return ;
}
int i,j,v;
for(i=pp[u]; i; i=edge[i].next)
{
v=edge[i].v;
if(!vis[v]&&le>=edge[i].a&&ri<=edge[i].b)
{
vis[v]=1;
dfs(v,le,ri);
}
}
}
void solve()
{
int i,j,u,v,t;
int le,ri,mid;
ans=0;
for(i=1;i<=cxx;i++)
{
le=x[i],ri=1000001;
while(le<ri)
{
mid=(le+ri)>>1;
flag=0;
memset(vis,0,sizeof(vis));
vis[1]=1;
dfs(1,x[i],mid);
if(flag) le=mid+1;
else ri=mid;
}
ans=max(ans,le-x[i]);
}
}
int main()
{
int i,j;
int u,v,a,b;
while(~scanf("%d%d",&n,&m))
{
cnt=cxx=0;
memset(pp,0,sizeof(pp));
for(i=1; i<=m; i++)
{
scanf("%d%d%d%d",&u,&v,&a,&b);
x[++cxx]=a;
x[++cxx]=b;
addedge(u,v,a,b);
addedge(v,u,a,b);
}
solve();
if(ans) printf("%d\n",ans);
else printf("Nice work, Dima!\n");
}
return 0;
}