http://codeforces.com/contest/366/problem/D

10 篇文章 0 订阅
6 篇文章 0 订阅
D. Dima and Trap Graph
time limit per test
3 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

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 lkto 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 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 test(s)
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.


题意:有n个点m条边,对于便a-b关联一个区间[l,r],现在从点1开始要走到点n,但是在走之前需要选定一个数x,然后只有当l<=x<=r时才能通过相应的边a-b,求一个最大的区间使区间内的数作为x都能满足要求,输出区间的大小,如果没有满足条件的区间则输出 Nice work, Dima!

第一种做法:
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <string>
#include <queue>
#include <algorithm>
#include <map>
#include <iomanip>
#define INF 99999999
using namespace std;

const int MAX=1000+10;
int head[MAX],size,n,m;
bool mark[MAX];

struct Node{
    int l,r;
    bool operator<(const Node &a)const{
        if(l == a.l)return r>a.r;
        return l<a.l;
    }
}s[3*MAX];

struct Edge{
    int v,next,l,r;
    Edge(){}
    Edge(int V,int NEXT,int L,int R):v(V),next(NEXT),l(L),r(R){}
}edge[6*MAX];

void Init(int num){
    for(int i=1;i<=num;++i)head[i]=-1;
    size=0;
}

void InsertEdge(int u,int v,int l,int r){
    edge[size]=Edge(v,head[u],l,r);
    head[u]=size++;
}

bool dfs(int u,int l,int r){
    if(u == n)return true;
    mark[u]=true;
    for(int i=head[u];i != -1;i=edge[i].next){
        if(mark[edge[i].v])continue;
        if(edge[i].l>l || edge[i].r<r)continue;
        if(dfs(edge[i].v,l,r))return true;
    }
    return false;
}

int main(){
    int u,v,l,r;
    while(cin>>n>>m){
        Init(n);
        for(int i=1;i<=m;++i){
            scanf("%d%d%d%d",&u,&v,&l,&r);
            s[i].l=l,s[i].r=r;
            InsertEdge(u,v,l,r);
            InsertEdge(v,u,l,r);
        }
        sort(s+1,s+1+m);
        int sum=0,mid=0;
        for(int i=1;i<=m;++i){
            l=s[i].l+sum,r=s[i].r;
            if(r<l || s[i].l == s[i-1].l)continue;
            while(l<=r){
                mid=l+r>>1;
                memset(mark,false,sizeof mark);
                if(dfs(1,s[i].l,mid))l=mid+1;
                else r=mid-1;
            }
            if(r-s[i].l+1>sum)sum=r-s[i].l+1;
        } 
        if(sum)cout<<sum<<endl;
        else cout<<"Nice work, Dima!"<<endl;
    }
    return 0;
}

第二种做法:
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <string>
#include <queue>
#include <algorithm>
#include <map>
#include <iomanip>
#define INF 99999999
using namespace std;

const int MAX=1000+10;
int father[MAX],rank[MAX];

struct Edge{
	int u,v,l,r;
	bool operator<(const Edge &a)const{
		return l<a.l;
	}
}edge[3*MAX];

void makeset(int num){
	for(int i=1;i<=num;++i)father[i]=i,rank[i]=1;
}

int findset(int v){
	if(v != father[v])father[v]=findset(father[v]);
	return father[v];
}

void Union(int x,int y){
	int a=findset(x);
	int b=findset(y);
	if(a == b)return;
	if(rank[a]>rank[b]){
		father[b]=a;
		rank[a]+=rank[b];
	}else{
		father[a]=b;
		rank[b]+=rank[a];
	}
}

int main(){
	int n,m;
	while(cin>>n>>m){
		for(int i=0;i<m;++i){
			cin>>edge[i].u>>edge[i].v>>edge[i].l>>edge[i].r;
		}
		sort(edge,edge+m);
		int sum=0;
		for(int i=0;i<m;++i){
			makeset(n);
			for(int j=0;j<m;++j)if(edge[j].r>=edge[i].r){
				Union(edge[j].u,edge[j].v);
				if(findset(1) == findset(n)){sum=max(sum,edge[i].r-edge[j].l+1);break;}
			}
		}
		if(sum)cout<<sum<<endl;
		else cout<<"Nice work, Dima!"<<endl;
	}
	return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值