题目大意:
就是现在给你一个有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;
}