Codeforces 366D Dima and Trap Graph 【并查集】

题目链接:Codeforces 366D Dima and Trap Graph
D. Dima and Trap Graph
time limit per test3 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard 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 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.

Examples
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条无向边。初始你需要选择一个整数x,走第i条边的限制为Li <= x <= Ri,假设1-n的一条路径上可以选择的整数x有ans,问你最大的ans。

思路:首先考虑在已知左端点时,能否维护出最大的右端点。显然是可以的,但是要注意,这里枚举左端点不能二分,具体原因自己尝试。。。不能二分的话,那么直接枚举好了,显然最优左端点一定存在于这m条边。我们需要sort下,让右端点大的靠前,这样才可以保证在连通1和n时才是最优的右端点。

AC代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <vector>
#include <queue>
#include <map>
#include <stack>
#define PI acos(-1.0)
#define CLR(a, b) memset(a, (b), sizeof(a))
#define fi first
#define se second
#define ll o<<1
#define rr o<<1|1
using namespace std;
typedef long long LL;
typedef pair<int, int> pii;
const int MAXN = 1e3 + 10;
const int pN = 1e6;// <= 10^7
const int INF = 0x3f3f3f3f;
const int MOD = 1e9 + 7;
void add(LL &x, LL y) { x += y; x %= MOD; }
int father[MAXN];
int n, m;
void init() {for(int i = 1; i <= n; i++) father[i] = i; }
int Find(int p) {
    int t, child = p;
    while(p != father[p]) { p = father[p]; }
    while(child != p) { t = father[child]; father[child] = p; child = t; }
    return p;
}
void Merge(int x, int y) {
    int fx = Find(x);
    int fy = Find(y);
    father[fx] = fy;
}
struct Edge {
    int from, to, L, R;
};
Edge edge[MAXN*3+10];
bool cmp(Edge a, Edge b) {
    return a.R != b.R ? a.R > b.R : a.L < b.L;
}
int Min, ans;
void judge(int l) {
    Min = INF; init();
    for(int i = 0; i < m; i++) {
        int u = edge[i].from;
        int v = edge[i].to;
        if(edge[i].L > l) continue;
        if(edge[i].R < l) continue;
        if(Find(u) != Find(v)) {
            Merge(u, v);
            Min = min(Min, edge[i].R);
        }
        if(Find(n) == Find(1)) {
            ans = max(ans, Min - l + 1);
            return ;
        }
    }
}
int main()
{
    scanf("%d%d", &n, &m);
    for(int i = 0; i < m; i++) {
        scanf("%d%d%d%d", &edge[i].from, &edge[i].to, &edge[i].L, &edge[i].R);
    }
    sort(edge, edge+m, cmp);
    ans = 0;
    for(int i = 0; i < m; i++) {
        judge(edge[i].L);
    }
    if(ans == 0) {
        printf("Nice work, Dima!\n");
    }
    else {
        printf("%d\n", ans);
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值