codeforces Dima and Trap Graph

    xiaoxiao2021-03-26  10

    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.

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

    dfs剪枝 先收缩区间 区间大的剪掉,区间比maxx小的剪掉。搜索的区间是搜索过的子区间剪掉

    #include <bits/stdc++.h> using namespace std; vector<pair<int,pair<int,int> > > v[10101]; typedef pair<int,pair<int,int > > dap; typedef pair<int,int> xiaop; int maxx=0; int n,m; int vis[101010]; int visx[101010]; int visy[101010]; void dfs(int x,int l,int r) { if(x==n) { maxx=max(maxx,r-l+1); return ; } for(int i=0;i<v[x].size();i++) { int xx=v[x][i].first; if(vis[xx]==1) continue; int ll=v[x][i].second.first; int rr=v[x][i].second.second; int lll=max(l,ll); int rrr=min(r,rr); if(vis[xx]==1) continue; else { vis[xx]=1; if(visx[xx]<=lll&&visy[xx]>=rrr) { vis[xx]=0; continue; } if(lll<=rrr&&rrr-lll+1>maxx) { visx[xx]=lll; visy[xx]=rrr; dfs(xx,lll,rrr); } vis[xx]=0; } } } int main() { cin>>n>>m; for(int i=1;i<=m;i++) { int a,b,c,d; scanf("%d %d %d %d",&a,&b,&c,&d); v[a].push_back(dap(b,xiaop(c,d))); v[b].push_back(dap(a,xiaop(c,d))); } dfs(1,1,1e9); if(maxx==0) printf("Nice work, Dima!"); else printf("%d\n",maxx); }

    二分做法,确定下界,二分求上界 斌巨代码