Codeforce336D.Dima and Trap Graph(二分右端点+dfs验证)
来源:互联网 发布:linux tar命令出错 编辑:程序博客网 时间:2024/05/24 02:37
题目传送门:http://codeforces.com/problemset/problem/366/D
题意:有n个点m条无向边。初始你需要选择一个整数x,走第i条边的限制为Li <= x <= Ri,假设1-n的一条路径上可以选择的整数x有ans个,问你最大的ans。
分析:因为答案肯定是这些边中最优左端点到某个右端点的,所以就成了在已知左端点的情况下,维护最大的右端点。二分右端点,dfs验证是否可行即可。
#include<bits/stdc++.h>#define PI acos(-1.0)#define eps 1e-8#define ll long long#define MEM(a, b) memset(a, b, sizeof(a))#define pb push_back#define mp make_pair#define MII map<int,int>::iterator#define MLL map<LL,LL>::iterator#define pii pair<int,int>#define si set<int>::iterator#define sl set<LL>::iterator#define bug printf("bug-------bug-------bug\n")using namespace std;const int maxn = 1005;int Time, head[maxn], vis[maxn], eid, ans, n, m;bool success;set<int> st;struct Edge{ int to, x, y, nxt;}edge[6010];void addedge(int u, int v, int l, int r){ edge[eid].to = v; edge[eid].x = l; edge[eid].y = r; edge[eid].nxt = head[u]; head[u] = eid++;}void dfs(int u, int l, int r){ if(success) return; if(u == n) { success = true; return; } for(int i = head[u]; i != -1; i = edge[i].nxt) { int v = edge[i].to; if(vis[v] == Time || edge[i].x > l || edge[i].y < r) continue; vis[v] = Time; dfs(v, l, r); }}void solve(){ ans = 0; for(si i = st.begin(); i != st.end(); i++) { int now = *i; int left = now, right = 1e6+5, mid; while(left < right) { Time++; success = false; vis[1] = Time; mid = (left + right) / 2; dfs(1, now, mid); if(success) left = mid + 1; else right = mid; } ans = max(ans, left - now); }}int main(){ Time = 1; memset(vis, 0, sizeof(vis)); while(cin >> n >> m) { eid = 0; memset(head, -1, sizeof(head)); st.clear(); while(m--) { int u, v, l, r; cin >> u >> v >> l >> r; addedge(u, v, l, r); addedge(v, u, l, r); st.insert(l); st.insert(r); } solve(); if(ans == 0) printf("Nice work, Dima!\n"); else printf("%d\n",ans); } return 0;}
0 0
- Codeforce336D.Dima and Trap Graph(二分右端点+dfs验证)
- Codeforces 366D Dima and Trap Graph【二分+Dfs】
- cf-Round #214 (Div. 2)-D-Dima and Trap Graph-dfs+二分
- codeforces Dima and Trap Graph
- Dima and Trap Graph CodeForces
- Dima and Trap Graph CodeForces
- CodeForces 366D Dima and Trap Graph
- 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 题解(搜索+剪枝or二分+枚举)
- Dima and Trap Graph ------codeforces #214 div.2
- Codeforces 366D Dima and Trap Graph 【并查集】
- CodeForces - 366D Dima and Trap Graph (并查集&技巧)好题
- Codeforces--366D--Dima and Trap Graph(并查集)
- C. Inna and Dima----dfs
- codeforces 366D 求1-n点最大区间长度 枚举区间左端+二分区间右端dfs判可行
- codeforces 374C C. Inna and Dima(DFS)
- git初识
- 软件测试(一)app测试过程总结
- Codeforces 580D 状压DP
- 面试题集锦
- 【Java】知乎:学习 JAVA,有什么书籍推荐?学习的方法和过程是怎样的?
- Codeforce336D.Dima and Trap Graph(二分右端点+dfs验证)
- Merge Two Sorted Lists问题及解法
- 括号配对问题
- PAT-A1125
- springMVC 不扫描 controller 中的方法
- QtQuick 中的 qml 与 Qt 的 C++
- 软件测试定义
- 测试的知识
- javascript download excel file from JSON data