CodeForces - 366D Dima and Trap Graph (并查集&技巧)好题
2016-03-30 22:20
453 查看
CodeForces - 366D Dima and Trap Graph
Description 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 integersx, 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. Sample Input 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 Output Nice work, Dima! Hint 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 ,接下来m行输入,每行四个数 s,e,l,r,分别表示: 有n个节点,m条边,第i条边的起点为s,终点为e,这条边的取值范围是[l,r],现在要求从节点1走到节点n,经过的每条边的权值x都要相等,问慢走条件的x的取值的个数有多少? //思路: 先将这些边的左端点按从小到大的顺序排序,然后再用并查集判断那些可以从节点1到节点n的路的x的取值。 #include<stdio.h> #include<string.h> #include<algorithm> #include<iostream> #define INF 0x3f3f3f3f #define ull unsigned long long #define ll long long #define IN __int64 #define N 3010 #define M 1000000007 using namespace std; int n; int a ; struct zz { int s; int e; int l; int r; }p ; int cmp(zz a,zz b) { return a.l<b.l; } int init() { for(int i=1;i<=n;i++) a[i]=i; } int find(int x) { return x==a[x]?x:a[x]=find(a[x]); } int judge(int x,int y) { int fx=find(x); int fy=find(y); if(fx!=fy) a[fy]=fx; } int main() { int t,i,j,k,m; while(scanf("%d%d",&n,&m)!=EOF) { init(); for(i=0;i<m;i++) scanf("%d%d%d%d",&p[i].s,&p[i].e,&p[i].l,&p[i].r); sort(p,p+m,cmp); int ans=0; for(i=0;i<m;i++) { init(); for(j=0;j<m;j++) { if(p[j].l>p[i].r) break; if(p[j].r<p[i].r) continue; judge(p[j].s,p[j].e); if(find(1)==find(n)) { ans=max(ans,p[i].r-p[j].l+1); break; } } } if(!ans) printf("Nice work, Dima!\n"); else printf("%d\n",ans); } return 0; } |
相关文章推荐
- Android_R文件丢失
- android studio 的AVD Manager点击无响应解决办法【新浪云搬运】
- win7的oracle 10gdbca无法启动的问题
- 11款好用的编程语言你会几种?
- Java_chapter14_抽象类和接口
- HDU 5281 Senior's Gun 贪心
- (Array) 一个 N*N 的矩阵,每一行从左到右有序,每一列从上到下有序,都是递增,写个程序,判断一个数是否在矩阵中。
- 专题一 Problem R
- 【那年那人那linux】“正则表达式”初窥之二则问题
- ***网站时用到的十种小方法
- 笔记 - 2、HTML - 表格
- 高效查找树
- JDBC在springMvc等框架中使用的方式
- scala入门1
- Poj 1065 Wooden Sticks【贪心+LIS?】
- LeetCode 82. Remove Duplicates from Sorted List II
- HDU 4548 美素数(打表呀,有个小技巧)
- android studio 修改程序包名
- 二叉树中和为某一值的路径
- 单片机调试——《浅谈工程师的调试法宝(四)——RTT的应用》