您的位置:首页 > 其它

Codeforces 366D Dima and Trap Graph(搜索剪枝/二分)

2017-03-07 20:47 423 查看
题目地址:点击打开链接

题意:给你一个图,每条边有一个[L, R],只有在这个区间内的点才能通过,问满足能从1

到n的最大连续区间的长度为多少

思路:

二分思路:枚举下界,二分上界,dfs是否能到。

搜索思路:

剪枝1.如果到过该点,并且此时的左右边界比原先的小,剪枝;

剪枝2:如果当前左右边界范围的值比之前搜到的答案还小,剪枝;

二分代码:

#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e4+5;
struct node
{
int v, l, r;
node(){}
node(int vv, int ll, int rr): v(vv), l(ll), r(rr) {}
};
int L[maxn], R[maxn], n, m;
bool vis[maxn];
vector<node> g[maxn];

bool dfs(int u, int l, int r)
{
if(l > r) return 0;
if(u == n) return 1;
for(int i = 0; i < g[u].size(); i++)
{
int v = g[u][i].v;
int vl = g[u][i].l;
int vr = g[u][i].r;
if(vl <= l && vr >= r && !vis[v])
{
vis[v] = 1;
if(dfs(v, l, r)) return 1;
}
}
return 0;
}

int main(void)
{
while(cin >> n >> m)
{
for(int i = 1; i <= n; i++)
g[i].clear();
for(int i = 1; i <= m; i++)
{
int u, v, l, r;
scanf("%d%d%d%d", &u, &v, &l, &r);
g[u].push_back(node(v, l, r));
g[v].push_back(node(u, l, r));
L[i] = l;
R[i] = r;
}
sort(L+1, L+1+m);
sort(R+1, R+1+m);
int ans = 0;
for(int i = 1; i <= m; i++)
{
int l = 1, r = m;
while(l <= r)
{
memset(vis, 0, sizeof(vis));
int mid = (l+r)/2;
if(dfs(1, L[i], R[mid]))
{
ans = max(ans, R[mid]-L[i]+1);
l = mid+1;
}
else r = mid-1;
}
}
if(!ans) puts("Nice work, Dima!");
else printf("%d\n", ans);
}
return 0;
}


搜索代码:

#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e4+5;
const int INF = 0x3f3f3f3f;
struct node
{
int v, l, r;
node(int vv, int ll, int rr): v(vv), l(ll), r(rr) {}
};
int n, m, ans, recl[maxn], recr[maxn];
bool vis[maxn], book[maxn];
vector<node> g[maxn];

void dfs(int u, int l, int r)
{
if(u == n)
{
ans = max(ans, r-l+1);
return ;
}
for(int i = 0; i < g[u].size(); i++)
{
int v = g[u][i].v;
int L = g[u][i].l;
int R = g[u][i].r;
if(!vis[v])
{
int tl = max(L, l);
int tr = min(R, r);
if(tl <= tr)
{
if(ans != -1 && tr-tl+1 <= ans) continue;//
if(book[v] && tl >= recl[v] && tr <= recr[v]) continue ;//
book[v] = 1;
recl[v] = tl, recr[v] = tr;
vis[v] = 1;
dfs(v, tl, tr);
vis[v] = 0;
}
}
}
}

int main(void)
{
while(cin >> n >> m)
{
memset(vis, 0, sizeof(vis));
memset(book, 0, sizeof(book));
for(int i = 1; i <= n; i++)
g[i].clear();
for(int i = 1; i <= m; i++)
{
int u, v, l, r;
scanf("%d%d%d%d", &u, &v, &l, &r);
g[u].push_back(node(v, l, r));
g[v].push_back(node(u, l, r));
}
ans = -1;
dfs(1, -INF, INF);
if(ans == -1) puts("Nice work, Dima!");
else printf("%d\n", ans);
}
return 0;
}


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 of n nodes and m edges.
For edge number k, Dima denoted a range of integers from lkto 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 61 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.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  codeforces 二分 dfs