差分约束经典题。设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. PHP计算两个已知经纬度之间的距离

    /** *求两个已知经纬度之间的距离,单位为千米 *@param lng1,lng2 经度 *@param lat1,lat2 纬度 *@return float 距离,单位千米 **/ privat ...

  2. 动画效果 ObjectAnimator

    学习了一下动画效果的使用,做一下笔记 ImageView imageView = findViewById(R.id.imageView); ObjectAnimator.ofFloat(imageV ...

  3. Reject Inference: Your Data is Deceiving You

    Keyword: Reject Inference Suppose there is a dataset of several attributes, including working condit ...

  4. [LeetCode] 53. Maximum Subarray 解题思路

    Find the contiguous subarray within an array (containing at least one number) which has the largest ...

  5. Python中的赋值语法

    Python中复制语法有6种 Basic Form >>>spam = 'spam' Tuple assignment >>>spam, ham = 'spam', ...

  6. Linux上安装MySQL - 12条命令搞定MySql

    从零开始安装mysql数据库 : 按照该顺序执行 :  a. 查看是否安装有mysql:yum list installed mysql*, 如果有先卸载掉, 然后在进行安装; b. 安装mysql客 ...

  7. lintcode-161-旋转图像

    161-旋转图像 给定一个N×N的二维矩阵表示图像,90度顺时针旋转图像. 样例 给出一个矩形[[1,2],[3,4]],90度顺时针旋转后,返回[[3,1],[4,2]] 挑战 能否在原地完成? 标 ...

  8. 【Docker 命令】- run命令

    docker run :创建一个新的容器并运行一个命令 语法: docker run [OPTIONS] IMAGE [COMMAND] [ARG...] OPTIONS说明: -a stdin: 指 ...

  9. JAVA的中文字符乱码问题

    来源:http://luzefengoo.blog.163.com/blog/static/1403593882012754428536/ JAVA的中文字符乱码问题一直很让人头疼.特别是在WEB应用 ...

  10. Calendar简单用法