差分约束经典题。设s[i]为前缀和,则有

  s[i]-s[i-1]<=1 (i往i-1连-1的边)

  s[i]>=s[i-1] (i-1往i连0的边)

  s[b]-s[a-1]>=c (a-1往b连c的边)

  最小值就改>=然后跑最长路

#include<iostream>
#include<cstring>
#include<cstdlib>
#include<cstdio>
#define ll long long
using namespace std;
const int maxn=,inf=1e9;
struct poi{int too,pre,sum;}e[maxn];
int n,m,x,y,z,front,rear,tot,mx,mn;
ll dist[maxn],ans;
int h[maxn],v[maxn],last[maxn],tim[maxn];
bool flag;
void read(int &k)
{
int f=;k=;char c=getchar();
while(c<''||c>'')c=='-'&&(f=-),c=getchar();
while(c<=''&&c>='')k=k*+c-'',c=getchar();
k*=f;
}
void add(int x,int y,int z){e[++tot].too=y;e[tot].sum=z;e[tot].pre=last[x];last[x]=tot;}
void spfa()
{
for(int i=mn;i<=mx;i++)v[i]=,dist[i]=-inf;
dist[mn]=;v[mn]=;front=rear=;h[++rear]=mn;
while(front!=rear)
{
int now=h[++front];if(front==maxn)front=-;
for(int i=last[now],too=e[i].too;i;i=e[i].pre,too=e[i].too)
if(dist[too]<dist[now]+e[i].sum)
{
dist[too]=dist[now]+e[i].sum;
if(++tim[too]>){printf("-1");flag=;return;}
if(!v[too])
{
v[too]=;h[++rear]=too;
if(rear==maxn)rear=-;
}
}
v[now]=;
}
}
int main()
{
while(~scanf("%d",&n))
{
memset(last,,sizeof(last));
memset(dist,,sizeof(dist));
flag=;mx=-inf;mn=inf;
for(int i=;i<=n;i++)
{
read(x);read(y);read(z);
mx=max(mx,y);mn=min(mn,x);
add(x-,y,z);
}
mn--;
for(int i=mn;i<mx;i++)add(i,i+,),add(i+,i,-);
spfa();
if(flag)continue;
printf("%lld\n",dist[mx]);
}
return ;
}

POJ1201:Intervals(差分约束)的更多相关文章

  1. POJ1201 Intervals(差分约束)

    Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 28416   Accepted: 10966 Description You ...

  2. poj1201 Intervals——差分约束

    题目:http://poj.org/problem?id=1201 差分约束裸题: 设 s[i] 表示到 i 选了数的个数前缀和: 根据题意,可以建立以下三个限制关系: s[bi] >= s[a ...

  3. hdu 1384 Intervals (差分约束)

    Intervals Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total ...

  4. poj 1716 Integer Intervals (差分约束 或 贪心)

    Integer Intervals Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 12192   Accepted: 514 ...

  5. zoj 1508 Intervals (差分约束)

    Intervals Time Limit: 10 Seconds      Memory Limit: 32768 KB You are given n closed, integer interva ...

  6. poj 1201 Intervals(差分约束)

    题目:http://poj.org/problem?id=1201 题意:给定n组数据,每组有ai,bi,ci,要求在区间[ai,bi]内至少找ci个数, 并使得找的数字组成的数组Z的长度最小. #i ...

  7. poj 1201 Intervals——差分约束裸题

    题目:http://poj.org/problem?id=1201 差分约束裸套路:前缀和 本题可以不把源点向每个点连一条0的边,可以直接把0点作为源点.这样会快许多! 可能是因为 i-1 向 i 都 ...

  8. POJ1201基础差分约束

    题意:       有一条直线,直线上做多有50000个点,然后给你组关系 a b c表明a-b之间最少有c个点,问直线上最少多少个点. 思路:        a-b最少有c个点可以想象a到b+1的距 ...

  9. poj1201/zoj1508/hdu1384 Intervals(差分约束)

    转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud Intervals Time Limit: 10 Seconds      Mem ...

  10. POJ1201 Intervals差分约束系统(最短路)

    Description You are given n closed, integer intervals [ai, bi] and n integers c1, ..., cn. Write a p ...

随机推荐

  1. 手机APP测试如何进行兼容性测试?

    Android App兼容性测试是一个比较重要的App评价内容,实际上兼容性测试不仅仅和测试人员相关,在开发阶段就应当着重考虑,因为兼容性问题是除了实现App本身要求的功能后,必须要关注.而且至关重要 ...

  2. darknet 识别获取结果

    在examples/darknet.c文件中若使用detect命令可以看到调用了test_detector. ... else if (0 == strcmp(argv[1], "detec ...

  3. Laxcus大数据管理系统2.0(7)- 第五章 数据构建

    第五章 数据构建 在数据处理过程,我们经常会遇到这样的情况:大多数时候,用户最初输入的数据会含有大量无意义的.杂乱的信息,需要经过提炼.收集.汇总等一系列手段,才能产生有意义和用户可识别的数据内容:当 ...

  4. 完全背包问题 :背包dp

    题目描述: 有 N种物品和一个容量是 V 的背包,每种物品都有无限件可用.第 i 种物品的体积是 vi,价值是 wi. 求解将哪些物品装入背包,可使这些物品的总体积不超过背包容量,且总价值最大.输出最 ...

  5. 吴恩达深度学习 反向传播(Back Propagation)公式推导技巧

    由于之前看的深度学习的知识都比较零散,补一下吴老师的课程希望能对这块有一个比较完整的认识.课程分为5个部分(粗体部分为已经看过的): 神经网络和深度学习 改善深层神经网络:超参数调试.正则化以及优化 ...

  6. window.open()与window.showModalDialog区别

    window.open()与window.showModalDialog区别 弹出窗口两种方式:    1.window.showModalDialog:      var feature = &qu ...

  7. cookie,localstorge,sessionstorge三者总结

    相同点:都是客户端存储东西的: 不同: 1大小,cookie最小;locastorge最大 2 cookie设置好会在header头里面自动带的:但是ls和ss不会:ls同个浏览下不同网页(非跨域)都 ...

  8. Median of Two Sorted Arrays(hard)

    题目要求: 有两个排序的数组nums1和nums2分别为m和n大小. 找到两个排序数组的中位数.整体运行时间复杂度应为O(log(m + n)). 示例: 我的方法: 分别逐个读取两个数组的数,放到一 ...

  9. ZooKeeper server &&client

    写了一个关于zookeepeer应用的简单demo 服务端定时的向zookeeper集群注册,客户端监听zookeeper服务节点变化,一旦变化,立刻响应,更新服务端列表 服务端代码: #includ ...

  10. sigsuspend

    1)头文件:#include <signal.h> 2)一个保护临界区代码的错误实例:(sigprocmask()和pause()实现) #include <unistd.h> ...