Codeforces 366D
来源:互联网 发布:纯牛奶 知乎 编辑:程序博客网 时间:2024/06/10 11:28
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;}
- codeforces 366D
- Codeforces 366D
- CodeForces 366D Dima and Trap Graph
- http://codeforces.com/contest/366/problem/D
- Codeforces 366D 贪心+并查集
- codeforces 366D 并查集
- CodeForces 617D CodeForces 617D
- CodeForces 101D
- CodeForces 103D
- CodeForces 222D Olympiad
- codeforces 242d
- CodeForces 111D
- 【codeforces】3D
- CodeForces 127D Password
- codeforces 257D. Sum
- Codeforces 292D
- Codeforces 275D
- Codeforces 126D
- FFmpeg 介绍
- 为什么不建议客户端bind
- 微信支付v3发布到iis时的证书问题
- NO.10 selinux的管理
- [BZOJ3489]A simple rmq problem(可持久化树套树||K-D tree)
- Codeforces 366D
- C#中Socket关闭 Close、Dispose、Shutdown、Disconnect
- HTML5新标签<canvas>
- Android 6.0 usb解析(二)UsbHostManager
- php开发环境及工具
- 我的软件工程课目标
- Java 获取随机验证码数字
- linux几种快速清空文件内容的方法
- 登录日志帮助文件