您的位置:首页 > 其它

CodeForces - 366D Dima and Trap Graph (并查集&技巧)好题

2016-03-30 22:20 453 查看
CodeForces - 366D

Dima and Trap Graph

Time Limit: 3000MS Memory Limit: 262144KB 64bit IO Format: %I64d & %I64u
Submit Status

Description

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

Sample Input

Input
4 4
1 2 1 10
2 4 3 5
1 3 1 5
2 4 2 7


Output
6


Input
5 61 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!


Hint

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 ,接下来m行输入,每行四个数  s,e,l,r,分别表示:

有n个节点,m条边,第i条边的起点为s,终点为e,这条边的取值范围是[l,r],现在要求从节点1走到节点n,经过的每条边的权值x都要相等,问慢走条件的x的取值的个数有多少?

//思路:

先将这些边的左端点按从小到大的顺序排序,然后再用并查集判断那些可以从节点1到节点n的路的x的取值。

#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<iostream>
#define INF 0x3f3f3f3f
#define ull unsigned long long
#define ll long long
#define IN __int64
#define N 3010
#define M 1000000007
using namespace std;
int n;
int a
;
struct zz
{
int s;
int e;
int l;
int r;
}p
;
int cmp(zz a,zz b)
{
return a.l<b.l;
}
int init()
{
for(int i=1;i<=n;i++)
a[i]=i;
}
int find(int x)
{
return x==a[x]?x:a[x]=find(a[x]);
}
int judge(int x,int y)
{
int fx=find(x);
int fy=find(y);
if(fx!=fy)
a[fy]=fx;
}
int main()
{
int t,i,j,k,m;
while(scanf("%d%d",&n,&m)!=EOF)
{
init();
for(i=0;i<m;i++)
scanf("%d%d%d%d",&p[i].s,&p[i].e,&p[i].l,&p[i].r);
sort(p,p+m,cmp);
int ans=0;
for(i=0;i<m;i++)
{
init();
for(j=0;j<m;j++)
{
if(p[j].l>p[i].r)
break;
if(p[j].r<p[i].r)
continue;
judge(p[j].s,p[j].e);
if(find(1)==find(n))
{
ans=max(ans,p[i].r-p[j].l+1);
break;
}
}
}
if(!ans)
printf("Nice work, Dima!\n");
else
printf("%d\n",ans);
}
return 0;
}

,
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: