poj 1201 Intervals(差分约束)
题目: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; const int INF = <<;
struct node
{
int u;
int v;
int w;
int next;
}edge[];
int dis[];
int mi,ma,n; void bellman_ford()
{
int i,f=;
while(f)
{
f=;
for(i=; i<=n; i++)
if(dis[edge[i].u]>dis[edge[i].v]-edge[i].w)
{
dis[edge[i].u]=dis[edge[i].v]-edge[i].w;
f=;
} for(i=mi; i<=ma; i++)
if(dis[i]>dis[i-]+)
{
dis[i]=dis[i-]+;
f=;
} for(i=ma; i>=mi; i--)
if(dis[i-]>dis[i])
{
dis[i-]=dis[i];
f=;
}
}
} int main()
{
int i;
while(~scanf("%d",&n))
{
mi=INF; ma=-;
for(i=; i<=n; i++)
{
scanf("%d%d%d",&edge[i].u, &edge[i].v, &edge[i].w);
if(mi>edge[i].u)
mi=edge[i].u;
if(ma<edge[i].v)
ma=edge[i].v; edge[i].u--;
dis[i]=;
}
bellman_ford();
printf("%d\n",dis[ma]-dis[mi-]);
}
return ;
}
大神的题解:
差分约束的思想:可以肯定的是s[bi]-s[ai-1]>=ci; 为什么要ai-1,是因为ai也要选进来
在一个是s[i]-s[i-1]<=1;
s[i]-s[i-1]>=0
所以整理上面三个式子可以得到约束条件:
①s[ai-1]-s[bi] <= -ci
②s[i]-s[i-1] <= 1
③s[i-1]-s[i] <= 0
1、约束条件必须同为<= 或者同为>=
2、若形如:x1 - x2<=k1 则 x2点 向x1点连边,权值为k1,求最短路
若形如:x1-x2>=k1 则 x2点 向x1点连边,权值为k1,求最长路
3、如果多组约束存在矛盾,则图中存在负环,判断负环的方法是用spfa判断是否有点进队次数>=n
poj 1201 Intervals(差分约束)的更多相关文章
- poj 1201 Intervals——差分约束裸题
题目:http://poj.org/problem?id=1201 差分约束裸套路:前缀和 本题可以不把源点向每个点连一条0的边,可以直接把0点作为源点.这样会快许多! 可能是因为 i-1 向 i 都 ...
- POJ 1201 Intervals (差分约束系统)
题意 在区间[0,50000]上有一些整点,并且满足n个约束条件:在区间[ui, vi]上至少有ci个整点,问区间[0, 50000]上至少要有几个整点. 思路 差分约束求最小值.把不等式都转换为&g ...
- POJ 2101 Intervals 差分约束
Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 27746 Accepted: 10687 Description You ...
- POJ 1201 Intervals【差分约束】
传送门:http://poj.org/problem?id=1201 题意: 有n个如下形式的条件:,表示在区间[, ]内至少要选择个整数点.问你满足以上所有条件,最少需要选多少个点? 思路:第一道差 ...
- poj 1201 Intervals(差分约束)
做的第一道差分约束的题目,思考了一天,终于把差分约束弄懂了O(∩_∩)O哈哈~ 题意(略坑):三元组{ai,bi,ci},表示区间[ai,bi]上至少要有ci个数字相同,其实就是说,在区间[0,500 ...
- POJ 1201 Intervals (经典) (差分约束)
<题目链接> 题目大意:给你$n$段区间,$a_i,b_i,c_i$ 表示在 $[a_i,b_i]$ 区间内至少要选择$c_i$个点.现在问你在满足这n个条件的情况下,最少要选多少个点? ...
- 【题解】 POJ 1201 Intervals(差分约束)
懒得复制,戳我戳我 Solution: 这道题就是一个板子题 抽象成第\(a\)至第\(b\)间选择数的个数为\(c\),我们就可以用前缀和来表示,这样就可以得到不等式\(s[b]-s[a-1]> ...
- POJ 1201 Intervals(差分约束 区间约束模版)
关于差分约束详情可阅读:http://www.cppblog.com/menjitianya/archive/2015/11/19/212292.html 题意: 给定n个区间[L,R], 每个区间至 ...
- poj 1201 Intervals【差分约束+spfa】
设s为前缀和,首先显然的条件是\[ s_{bi}-s_{ai-1}>=c \],然后隐含的是\[ s_i-s_{i-1}>=0 s_i-s_{i-1}<=1 \] 然后根据差分约束, ...
随机推荐
- discuz 注册用户用到的几个表
通过开启记录:mysql记录日志,筛出如下信息: INSERT INTO `userclub`.pre_ucenter_members SET secques='', username='pthlp1 ...
- .NET安装和配置Oracle数据访问组件(ODAC)
Many ASP.NET applications access Oracle database for the data source. Oracle supports the .NET with ...
- [译] ASP.NET 生命周期 – ASP.NET 上下文对象(五)
ASP.NET 上下文对象 ASP.NET 提供了一系列对象用来给当前请求,将要返回到客户端的响应,以及 Web 应用本身提供上下文信息.间接的,这些上下文对象也可以用来回去核心 ASP.NET 框架 ...
- HTML 菜单 a 标签设置样式
html: "<div style='font-weight:800;color:red'> <a href='javascript:void(0)'style='colo ...
- Understanding Responsive Web Design: Cross-browser Compatibility
http://www.sitepoint.com/understanding-responsive-web-design-cross-browser-compatibility/ In the las ...
- uva 10534
一开始WA了 参考了一下 求正反两个方向的最长上升子序列 并分别记录在两个数组中 最后求最大值 #include <iostream> #include <cstdio&g ...
- 深入理解计算机各种类型大小(sizeof)
深入理解计算机各种类型大小(sizeof) // Example of the sizeof keyword size_t i = sizeof( int ); struct align_dep ...
- 咦,为DJANGO的ORM的QUERYSET增加数据列的样码,很好用哟
这个我真的没有查资料,是通过直觉和经验弄出来的,哈哈,感觉用深一点好. 这样在模板输出时,就更好控制啦.. if self.kwargs: if self.kwargs.has_key('search ...
- hbase集群在启动的时候找不到JAVA_HOME的问题
hbase集群在启动的时候找不到JAVA_HOME的问题,启动集群的时候报错信息如下: root@master:/usr/local/hbase-/bin# ./start-hbase.sh star ...
- the service mysql56 was not found in the Windows services的解决办法
mysql无法启动,无法改变状态-CSDN论坛-CSDN.NET-中国最大的IT技术社区 http://bbs.csdn.net/topics/390943788 具体描述: 关闭,重启mysql ...