CodeForces 366D Dima and Trap Graph

迪玛的陷阱图算法

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 ofnnodes andmedges. For edge numberk, Dima denoted a range of integers fromlktork(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 number1to the final node with numbern. At that, Seryozha can go along edgekonly iflk ≤ x ≤ rk.

Seryozha is a mathematician. He defined theloyaltyof some path from the1-st node to then-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 integersnandm(2 ≤ n ≤ 103, 0 ≤ m ≤ 3·103). Then followmlines describing the edges. Each line contains four integersak,bk,lkandrk(1 ≤ ak, bk ≤ n, 1 ≤ lk ≤ rk ≤ 106). The numbers mean that in the trap graph thek-th edge connects nodesakandbk, this edge corresponds to the range of integers fromlktork.

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



并查集。。。。。
按左端点排序后,枚举每一段,更新如果以这一段的右端点为区间结束点的长度。。。


#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;

struct node
{
    int x,y,l,r;
}c[3300];

bool cmp(node a,node b)
{
    return a.l<b.l;
}

int f[1100];

void init()
{
    for(int i=0;i<=1010;i++) f[i]=i;
}

int find(int x)
{
    if(x==f[x]) return x;
    return find(f[x]);
}

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

int n,m;

int main()
{
    scanf("%d%d",&n,&m);
    init();
    for(int i=0;i<m;i++)
    {
        int a,b,cc,d;
        scanf("%d%d%d%d",&a,&b,&cc,&d);
        c[i].x=a;c[i].y=b;c[i].l=cc;c[i].r=d;
    }
    sort(c,c+m,cmp);
    int ans=0;
    for(int i=0;i<m;i++)
    {
        init();
        for(int j=0;j<m;j++)
        {
            if(c[j].l>c[i].r) break;
            if(c[j].r<c[i].r) continue;
            merge(c[j].x,c[j].y);
            if(find(1)==find(n))
            {
                ans=max(ans,c[i].r-c[j].l+1);
                break;
            }
        }
    }
    if(!ans) puts("Nice work, Dima!");
    else printf("%d\n",ans);
    return 0;
}





### Codeforces Problem 1014D 解答与解释 当前问题并未提供关于 **Codeforces Problem 1014D** 的具体描述或相关背景信息。然而,基于常见的竞赛编程问题模式以及可能涉及的主题领域(如数据结构、算法优化等),可以推测该问题可能属于以下类别之一: #### 可能的解法方向 如果假设此问题是典型的计算几何或者图论类题目,则通常会涉及到如下知识点: - 图遍历(DFS 或 BFS) - 贪心策略的应用 - 动态规划的状态转移方程设计 由于未给出具体的输入输出样例和约束条件,这里无法直接针对Problem 1014D 提供精确解答。但是可以根据一般性的解决思路来探讨潜在的方法。 对于类似的复杂度较高的题目,在实现过程中需要注意边界情况处理得当,并且要充分考虑时间效率的要求[^5]。 以下是伪代码框架的一个简单例子用于说明如何构建解决方案逻辑流程: ```python def solve_problem(input_data): n, m = map(int, input().split()) # 初始化必要的变量或数组 graph = [[] for _ in range(n)] # 构建邻接表或其他形式的数据表示方法 for i in range(m): u, v = map(int, input().split()) graph[u].append(v) result = [] # 执行核心算法部分 (比如 DFS/BFS 遍历) visited = [False]*n def dfs(node): if not visited[node]: visited[node] = True for neighbor in graph[node]: dfs(neighbor) result.append(node) for node in range(n): dfs(node) return reversed(result) ``` 上述代码仅为示意用途,实际应用需依据具体题目调整细节参数设置及其功能模块定义[^6]。 #### 关键点总结 - 明确理解题意至关重要,尤其是关注特殊测试用例的设计意图。 - 对于大规模数据集操作时应优先选用高效的时间空间性能表现良好的技术手段。 - 结合实例验证理论推导过程中的每一步骤是否合理有效。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值