Codeforces 366D Dima and Trap Graph【二分+Dfs】

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 of n nodes and m edges. For edge number k, Dima denoted a range of integers from lk to 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!

Input

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.

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.

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


题目大意:

给你N个点,M条无向边,每条边都有一个限制,[L,R],表示值x只有保证L<=x<=R的时候,才允许通过,我们最初选择一个数x.目的是为了从1走到n.问一共可以找到多少个这样的数。


思路:


首先我们明确一点,这些数一定是连续的.

那么我们不妨考虑枚举一个起点,然后二分终点判定一个区间是否合法,如果合法,过程维护最大值(R-L+1).

又因为数据比较大,L,R都是在1e6之内的,我们Dfs判定一个区间是否合法时间复杂度要O(n);

明显我们肯定不能O(1e6*log1e6*n)去做,那么我们再观察到m并不大,m在1e3之内,而且显然最终解的L是某条边的L.那么我们1e3去枚举左端点即可。

时间复杂度O(1e3*log1e3*n);

Dfs判定一个区间是否合法,如果合法,对应增大右端点,否则减小右端点。

二分和枚举过程中维护最终解的最大值。

注意无解时的输出。


Ac代码:

#include
#include
#include
using namespace std;
struct node
{
    int from;
    int to;
    int l;
    int r;
    int next;
}e[100000];
int cont;
int ok;
int vis[4000];
int head[100000];
int L[1000000];
int n,m;
void add(int from,int to,int l,int r)
{
    e[cont].to=to;
    e[cont].l=l;
    e[cont].r=r;
    e[cont].next=head[from];
    head[from]=cont++;
}
void Dfs(int u,int LL,int RR)
{
    vis[u]=1;
    if(u==n)
    {
        ok=1;
        return ;
    }
    for(int i=head[u];i!=-1;i=e[i].next)
    {
        int v=e[i].to;
        if(LL>=e[i].l&&RR<=e[i].r)
        {
            if(vis[v]==0)
            Dfs(v,LL,RR);
        }
    }
}
int main()
{
    while(~scanf("%d%d",&n,&m))
    {
        cont=0;
        memset(head,-1,sizeof(head));
        for(int i=0;i=0)
            {
                ok=0;
                int mid=(l+r)/2;
                memset(vis,0,sizeof(vis));
                Dfs(1,L[i],mid);
                if(ok==1)
                {
                    pos=mid;
                    l=mid+1;
                }
                else r=mid-1;
            }
            if(pos==-1)continue;
            ans=max(ans,pos-L[i]+1);
        }
        if(ans==-1)printf("Nice work, Dima!\n");
        else
        printf("%d\n",ans);
    }
}











你可能感兴趣的:(二分查找,搜索)