Codeforces--366D--Dima and Trap Graph(并查集)

 Dima and Trap Graph
Time Limit:3000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u

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 tork(lk ≤ rk). In order to get out of the trap graph, Seryozha initially (before starting his movements) should pick some integer (let's call itx), 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 akbklk 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 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!

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条无向边,每条边都有自己的权值范围,从1走到n可以有多种方法,现在求一个x使得从1--n的路上

x在所有边的权值范围,给出这个x有多少种

思路:我们找到的答案肯定是ri-lj+1这种情况,在从1通往n的路上,取最大的lj还有最小的ri然后相减得到答案,开始用的邻接表加上DFS来解,但是敲了一半发现不对劲儿,因为会出现重边的情况,如果对边进行判重的话也不知道要取哪个,因为我们找的是r最大还有l最小的,每一条边可能都会是组成答案的一部分,所以不能随意舍弃某一条边。

可能也可以用DFS写吧,说不定是我存储数据的方式不对,最后还是选择了并查集

首先是从1通向n,我们可以进行每一条边的添加实现1还有n在在一起,然后记录这些边的最小r还有最大l,

开始时先对边排序,排序时l小的在前边,每次我们枚举每一条边的r是最小的r,然后开始添加每一条边,直到1还有n全部进入集合,但是添加每一条边的时候我们应该维护r的最小地位,添加的边的r不可以大于我们选取的r,还有就是添加的边l不可以大于选的r


#include<cstdio>
#include<queue>
#include<cstring>
#include<algorithm>
using namespace std;
struct node
{
	int u,v;
	int l,r;
}edge[30000+10];
int pre[2000],m,n;
int find(int x)
{
	return x==pre[x]?pre[x]:(pre[x]=find(pre[x]));
}
void join(int x,int y)
{
	int fx=find(x);
	int fy=find(y);
	if(fx!=fy)
	pre[fx]=fy;
}
bool cmp(node s1,node s2)
{
	return s1.l<s2.l;
}
int main()
{
	while(scanf("%d%d",&n,&m)!=EOF)
	{
		for(int i=0;i<m;i++)
		scanf("%d%d%d%d",&edge[i].u,&edge[i].v,&edge[i].l,&edge[i].r);
		sort(edge,edge+m,cmp);
		int ans=0;
		for(int i=0;i<m;i++)
		{
			for(int k=1;k<=n;k++)
			pre[k]=k;//每次初始化 
			for(int j=0;j<m;j++)
			{
				if(edge[j].l>edge[i].r)
				continue;
				if(edge[j].r<edge[i].r)
				continue;//保证枚举的r一定是最小的r 
				join(edge[j].u,edge[j].v);
				if(find(1)==find(n))
				{//因为l从小到大排序,所以边j的l一定是最大的 
					ans=max(ans,edge[i].r-edge[j].l+1);
//					printf("%d %d\n",edge[i].r,edge[j].l);
					break;
				}
			}
		}
		if(!ans)
			printf("Nice work, Dima!\n");
		else
			printf("%d\n",ans);
	}
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值