CodeForces - 366D Dima and Trap Graph(병집 & 테크닉) 좋은 문제

4408 단어
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 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을 먼저 입력하고 그 다음에 m줄을 입력하면 줄마다 4개의 수 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[N];
struct zz
{
	int s;
	int e;
	int l;
	int r;
}p[N];
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!
"); else printf("%d
",ans); } return 0; }

,

좋은 웹페이지 즐겨찾기