poj 1201 Intervals【差分约束+spfa】】的更多相关文章

题目:http://poj.org/problem?id=1201 题意:给定n组数据,每组有ai,bi,ci,要求在区间[ai,bi]内至少找ci个数, 并使得找的数字组成的数组Z的长度最小. #include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <cstdlib> using namespace std; <<;…
题目:http://poj.org/problem?id=1201 差分约束裸套路:前缀和 本题可以不把源点向每个点连一条0的边,可以直接把0点作为源点.这样会快许多! 可能是因为 i-1 向 i 都连着一条0的边. 别忘了约束条件不仅有s[ i ] - s[ i-1 ] >= 0,还有s[ i ] - s[ i - 1] <= 1! 别忘了s的范围不是n而是mx! #include<iostream> #include<cstdio> #include<cstr…
题意 在区间[0,50000]上有一些整点,并且满足n个约束条件:在区间[ui, vi]上至少有ci个整点,问区间[0, 50000]上至少要有几个整点. 思路 差分约束求最小值.把不等式都转换为>=形式,那么显然有xvi >= xui-1 + ci,那么就在约束图中连一条(ui-1, vi, ci)的边:当然不要忘记隐含的不等式:xi >= xi-1 + 0;   xi-1 >= xi -1. 建完图后SPFA求最长路径即可 代码 [cpp] #include <iostr…
King Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 11946   Accepted: 4365 Description Once, in one kingdom, there was a queen and that queen was expecting a baby. The queen prayed: ``If my child was a son and if only he was a sound kin…
Candies Time Limit: 1500MS   Memory Limit: 131072K Total Submissions: 28071   Accepted: 7751 Description During the kindergarten days, flymouse was the monitor of his class. Occasionally the head-teacher brought the kids of flymouse’s class a large b…
Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 27746   Accepted: 10687 Description You are given n closed, integer intervals [ai, bi] and n integers c1, ..., cn. Write a program that: reads the number of intervals, their end points and…
题目大意:原题链接 构造一个集合,这个集合内的数字满足所给的n个条件,每个条件都是指在区间[a,b]内至少有c个数在集合内.问集合最少包含多少个点.即求至少有多少个元素在区间[a,b]内. 解题思路: 首先假设s[i]表示从0到i中有s[i]个数属于这个序列. 1. 开始我用每个整数(1,2,...)当做图的结点,添加边就是Add(u,v,w),写出来之后发现连题目的样例数据都输出错误,输出结果为8.2. 后来发现这样是错误的,比如有两个约束条件分别为:1 3 2, 3 6 2;那么按照上面的思…
O - Layout(差分约束 + spfa) Like everyone else, cows like to stand close to their friends when queuing for feed. FJ has N (2 <= N <= 1,000) cows numbered 1-N standing along a straight line waiting for feed. The cows are standing in the same order as the…
题目链接http://poj.org/problem?id=3169 题目大意: 一些牛按序号排成一条直线. 有两种要求,A和B距离不得超过X,还有一种是C和D距离不得少于Y,问可能的最大距离.如果没有输出-1,如果可以随便排输出-2,否则输出最大的距离. 首先关于差分约束:https://blog.csdn.net/consciousman/article/details/53812818 了解了差分约束之后就知道该题典型的差分约束+spfa即可. #include<iostream> #i…
设s为前缀和,首先显然的条件是\[ s_{bi}-s_{ai-1}>=c \],然后隐含的是\[ s_i-s_{i-1}>=0 s_i-s_{i-1}<=1 \] 然后根据差分约束,就是连边(bi,ai-1,-li),(i-1,i,1),(i,i-1,0) spfa跑最长路最后输出相反数即可,注意n是起点,min是终点,跑最短路(不会有负环) #include<iostream> #include<cstdio> #include<queue> usi…