Codeforces 366D Dima and Trap Graph (DFS剪枝水过...)

题目大意:

就是现在给你一个有n个点m条边的图(1 <= n <= 1000, 1 <= m <= 3000)

然后每条边上都有一个区间限制[L, R], 现在从点1出发选择一个能够到达n的路径, 对于这条路径初始的时候选择一个x如果x对于这个路径上的每一条边都满足L <= x <= R则x为可选择的正整数, 现在对于所有从1到n的路径输出这些路径对应的最大的x的可行数量, 如果是0输出"Nice work, Dima!"


大致思路:

看了题解之后才发现正解是先确定左边界然后二分右边界, 不过我写的时候dfs剪枝了一下就水过了...

就是dfs的时候如果当前可行区间长度已经比找到的最大长度小就直接不往下找了, 还有就是对于每一个点记录上一次搜索时的L和R, 如果接下来的L', R'是其子区间就不往下搜了

结果就这么水过去了....


代码如下:

Result  :  Accepted     Memory  :  24036 KB     Time  :  31 ms

/*
 * Author: Gatevin
 * Created Time:  2015/3/22 15:04:13
 * File Name: Chitoge_Kirisaki.cpp
 */
#include<iostream>
#include<sstream>
#include<fstream>
#include<vector>
#include<list>
#include<deque>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<bitset>
#include<algorithm>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cctype>
#include<cmath>
#include<ctime>
#include<iomanip>
using namespace std;
const double eps(1e-8);
typedef long long lint;

int n, m;
vector <int> G[1010];
int L[1010][3030];
int R[1010][3030];
bool vis[1010];
int visx[1010], visy[1010];
int ans;

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

int main()
{
    scanf("%d %d", &n, &m);
    int a, b, l, r;
    for(int i = 1; i <= m; i++)
    {
        scanf("%d %d %d %d", &a, &b, &l, &r);
        if(a == b) continue;
        G[a].push_back(b);
        G[b].push_back(a);
        R[a][G[a].size() - 1] = r;
        R[b][G[b].size() - 1] = r;
        L[a][G[a].size() - 1] = l;
        L[b][G[b].size() - 1] = l;
    }
    ans = 0;
    vis[1] = 1;
    visx[1] = 1, visy[1] = 1e6;
    dfs(1, 1, 1e6);
    if(ans == 0)
        printf("Nice work, Dima!\n");
    else
        printf("%d\n", ans);
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值