Codeforces 366D Dima and Trap Graph(搜索剪枝/二分)
来源:互联网 发布:php include没用 编辑:程序博客网 时间:2024/05/05 02:58
题目地址:点击打开链接
题意:给你一个图,每条边有一个[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;}
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!
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.
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.
4 41 2 1 102 4 3 51 3 1 52 4 2 7
6
5 61 2 1 102 5 11 201 4 2 51 3 10 113 4 12 100004 5 6 6
Nice work, Dima!
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 366D Dima and Trap Graph(搜索剪枝/二分)
- codeforces 366D Dima and Trap Graph 题解(搜索+剪枝or二分+枚举)
- Codeforces 366D Dima and Trap Graph【二分+Dfs】
- Codeforces Round #214 (Div. 2) D. Dima and Trap Graph (枚举+二分+搜索)
- CodeForces 366D Dima and Trap Graph
- Codeforces 366D Dima and Trap Graph (DFS剪枝水过...)
- Codeforces 366D Dima and Trap Graph 【并查集】
- CodeForces - 366D Dima and Trap Graph (并查集&技巧)好题
- Codeforces--366D--Dima and Trap Graph(并查集)
- codeforces Dima and Trap Graph
- Dima and Trap Graph CodeForces
- Dima and Trap Graph CodeForces
- cf-Round #214 (Div. 2)-D-Dima and Trap Graph-dfs+二分
- Codeforce336D.Dima and Trap Graph(二分右端点+dfs验证)
- Dima and Trap Graph ------codeforces #214 div.2
- 【二分图匹配】Codeforces 387D George and Interesting Graph
- Codeforces 387D George and Interesting Graph【思维+二分匹配】
- Codeforces 584D Dima and Lisa(数论)
- jQuery名称冲突解决方法
- 奖品的价值 Erasing and Winning
- 怎样写出漂亮整洁的代码?聊聊clean code的编码、重构技巧
- 正向表模板
- 记录一则线上bug
- Codeforces 366D Dima and Trap Graph(搜索剪枝/二分)
- Multi view adapter
- 去除list集合中重复项的几种方法
- 羽翼metasploit第一,二季学习笔记
- SoapUI使用方法-01发送http请求
- JPA——映射优化
- 第五届蓝桥杯【省赛试题7】六角填数
- 英文金曲大赛
- org.springframework.beans.factory.BeanDefinitionStoreException: Invalid bean definition with name 'd