Codeforces 366D - Dima and Trap Graph(DFS)

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,當x滿足>=L && <= R的情況時, 才能走當前的邊.
問X的區間最大長度, 使得能從點1到達點n.

解題思路:
深搜區間, 但是要有兩個剪枝.
第一個是如果通過該點的區間長度已經小於當前的最大長度, 則回溯.
第二個是如果通過當前邊的區間是上一次到達該邊的區間的子集, 則回溯.

AC代碼:

#include <bits/stdc++.h>

using namespace std;

const int maxn = 1e3+5;

vector <int> mp[maxn];
vector <int> R[maxn];
vector <int> L[maxn];
int ans = 0;

bool vis[maxn];
int n, m;

struct node
{
    int last_L;
    int last_R;
}last[maxn];

void dfs(int now, int l, int r)
{
    if(now == n){
        ans = max(ans, r - l + 1);
        return ;
    }
    for(int i = 0; i < mp[now].size(); i++){
        if(vis[mp[now][i]])
            continue;
        int nl = max(l, L[now][i]);
        int nr = min(r, R[now][i]);
        vis[mp[now][i]] = 1;
        if(nl <= nr && nr - nl + 1 > ans && !(last[mp[now][i]].last_L <= nl && last[mp[now][i]].last_R >= nr))
           last[mp[now][i]].last_L = nl, last[mp[now][i]].last_R = nr, dfs(mp[now][i], nl, nr);
        vis[mp[now][i]] = 0;
    }
    return ;
}

int main()
{
    ios::sync_with_stdio(0);
    cin >> n >> m;
    for(int i = 1; i <= m; i++)
    {
        int a, b, l, r;
        cin >> a >> b >> l >> r;
        mp[a].push_back(b);
        mp[b].push_back(a);
        L[a].push_back(l);
        L[b].push_back(l);
        R[a].push_back(r);
        R[b].push_back(r);
    }
    vis[1] = 1;
    last[1].last_L = 1;
    last[1].last_R = INT_MAX;
    dfs(1, 1, INT_MAX);
    if(!ans)
        cout << "Nice work, Dima!" << endl;
    else
        cout << ans << endl;
    return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章