VJ【并查集】

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

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


#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<math.h>
int n, m, f[1010];
struct point
{
	int s, e, l, r;
}p[3010];

int cmp(const void *a, const void *b)
{
	return (*(struct point *)a).l - (*(struct point *)b).l;
}
int find(int x)
{
	if(x != f[x])
	f[x] = find(f[x]);
	return f[x];
}

void merge(int x, int y)
{
	x = find(x);
	y = find(y);
	if(x != y)
	f[y] = x;
}

int main()
{
	int i, j, k;
	while(scanf("%d%d", &n, &m) != EOF)
	{
		for(i = 0; i < m; ++i)
		scanf("%d%d%d%d", &p[i].s, &p[i].e, &p[i].l, &p[i].r);
		qsort(p, m, sizeof(p[0]), cmp);
		int ans = 0;
		for(i = 0; i < m; ++i)//起始边
		{
			for(j = 1; j <= n; ++j)
			f[j] = j;
			for(j = 0; j < m; ++j)
			{
				if(p[j].l > p[i].r)
				break;
				if(p[j].r < p[i].r)
				continue;
				merge(p[j].s, p[j].e);
				if(find(1) == find(n))
				{
					if(p[i].r - p[j].l + 1> ans)
					ans = p[i].r - p[j].l + 1;
					break;
				}
			}
		}
		if(ans)
		printf("%d\n", ans);
		else
		printf("Nice work, Dima!\n");
	}
	return 0;
}

//题意:给出n个点和m条边,每一条边都有一条权值范围,现在求点1到点n所能使用的边权值的个数的最大值
//例如输入1,我们经过1-2可以使用的权值为1-10,接着是2-4我们能使用的权值为3-5或者是2-7,那么我们1-4所能使用的权值的最大范围就是2-7
//即我们从1-4路途上所经过的边的权值是一样的情况下,我们能使用的权值个数为6个
//思路:首先这是一个图,我们需要找出1-n的路径,但是这些边的权值是一个范围,所以我们在寻找路径时需要一些限定条件
//在这里我们将每一条边根据权值的范围下限从小到大进行排序,然后我们从第一条边开始遍历到最后一条边
//我们将上面的每一条边作为起始边,限定权值的范围上限为该起始边的上限,然后接着重新开始一个循环遍历所有边
//对于与上述起始边有交集且范围上限大于起始边的,我们将该边的2个端点进行合并,即表示相连接
//直至最后端点1和端点n相连接,那么我们目前得到的权值范围下限就是目前循环的边的下限而上限就是起始边的上限,这里所构成的1-n的权值范围既可以算出
//当所有边都当过起始边后我们就可以得到权值的最大范围,即权值的最大个数
//证明:首先我们对每一条边的上限都进行了遍历,那么对于权值的上限值是不会产生遗漏的情况
//其次对于权值的下限由于我们对下限进行了从小到大的排序,所以当我们发现1和n相连时则不必继续往下判断,因为越往下的下限必定越大,那么我们得到的权值个数则越少
//最后就是对于寻找的过程,当2个点相连时,由于已经定好了上限,那么我们知道该边能被选入的唯一条件就是下限小于起始边的上限并且上限大于起始边的上限,最后就是判断1和n是否连通了
//倘若所有情况都已经遍历而无连通的情况则ans = 0

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值