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);
}

二分做法,确定下界,二分求上界

http://blog.csdn.net/cillyb/article/details/60779187 斌巨代码

codeforces Dima and Trap Graph相关推荐

  1. CF366D Dima and Trap Graph 题解

    题意 给定一张有 nnn 个节点,mmm 条边的无向图(可能有环或重边),对于每个节点 iii,有 lil_ili​ 和 rir_iri​,定义在经过该节点后,只能携带于 lil_ili​ 和 rir ...

  2. 【CodeForces - 246D】Colorful Graph (暴力,图,存边,STL)

    题干: You've got an undirected graph, consisting of n vertices and m edges. We will consider the graph ...

  3. Codeforces 716D - Complete The Graph(最短路)

    题意:给定n个点,m条边,以及起点s,终点t,问你图中是否存在s->t的最短路为L,其中权值为0的可以任意修改. 思路:对给定的边分为2类,权重不为0的直接扔进去建图,权重为0的边先存起来.接着 ...

  4. Codeforces 题目合集+分类+代码 【Updating...】【361 in total】

    961A - Tetris                                                模拟                                      ...

  5. czl蒻蒟的OI之路10、11、12

    好久没有发微博了表示自己也很绝望啊今天来个三连击 XJOI奋斗群蒻蒟群群赛11 RANK排名11 T1The Wall WA一次后AC 题意 分析过程 给出题解 T2Maximal Area Quad ...

  6. c语言二维vector大小,vector作为二维数组

    vector本来就是可以用来代替一维数组的,vector提供了operator[]函数,可以像数组一样的操作,而且还有边界检查,动态改变大小. 这里只介绍用它来代替二维的数组,二维以上的可以依此类推. ...

  7. Codeforces 724 G Xor-matic Number of the Graph 线性基+DFS

    G. Xor-matic Number of the Graph http://codeforces.com/problemset/problem/724/G 题意:给你一张无向图.定义一个无序三元组 ...

  8. 构造图 Codeforces Round #236 (Div. 2) C. Searching for Graph

    题目地址 1 /* 2 题意:要你构造一个有2n+p条边的图,使得,每一个含k个结点子图中,最多有2*k+p条边 3 水得可以啊,每个点向另外的点连通,只要不和自己连,不重边就可以,正好2*n+p就结 ...

  9. Codeforces Round #167 (Div. 1) C. Dima and Horses(BFS+贪心)

    题目大意 有 n(1≤n≤3*105) 匹马,每条马都有几个敌人(不超过 3 个),现在要求把这些马分成两部分(允许一部分中没有一条马),使得对于每条马,和它在同一部分中的敌人的数量不超过1个 给出了 ...

  10. CodeForces - 1494E A-Z Graph(构造+思维)

    题目链接:https://vjudge.net/problem/CodeForces-1494E 题目大意:给出一个初始时只有 nnn 个点的有向带权图,需要执行 mmm 次操作,每次操作分为下列三种 ...

最新文章

  1. Solr索引数据同步ReplicationHandler
  2. 自动完成--autoComplete插件(2)
  3. 仅靠一杯奶茶钱8.8元,你就能转到人工智能专业?
  4. eclipse xml文件中按没有提示
  5. leetcode 第 216 场周赛 整理
  6. established关键字
  7. 重新想象 Windows 8.1 Store Apps (75) - 新增控件: Hub, Hyperlink
  8. Word 参考文献的自动修改
  9. MySQL 优化技巧
  10. python飞机大战类_python微信飞机大战
  11. python怎么关闭浏览器_python selenium 对浏览器标签页进行关闭和切换的方法
  12. 安卓原生镜像(中国网站)
  13. ibm常用分析工具ha.jar,jca.jar
  14. Outline for Mac(Mac记事本软件)
  15. 怎么装python的keras库_Keras 教程: Python 深度学习终极入门指南
  16. 玩安卓从 0 到 1 之列表一键置顶
  17. 文件目录自动生成工具--Dir Tree Noter
  18. latex输入单双引号
  19. 汇编语言(二)之将十进制数的ASCⅡ码转换为BCD码
  20. 神经网络输入图片大小,神经网络 图像相似度

热门文章

  1. Histromap of World History: The rise and fall of peoples and notions for 4000 years
  2. macbook系统占用硬盘大_苹果电脑系统占用硬盘过大,怎么解决
  3. 计算机学情问卷调查报告,学情调查报告及调查问卷(共9篇).docx
  4. Mac在已安装Python3.9的情况下利用miniconda配置【Python3.7+TensorFlow1.14环境】+ Sublime Text如何通过conda切换不同Python环境
  5. 6款免费网络延迟测试工具
  6. Simon‘s writting 全网最全笔记
  7. 香港理工大学计算机专业课程,香港理工大学计算机系包括哪些专业
  8. Linux reboot全过程
  9. zoomit64_终极缩放工具? Sysinternal的ZoomIt
  10. 阿里云 数据库mysql卸载安装,基本上所有坑全趟了