题目:http://poj.org/problem?id=1201

差分约束裸题;

设 s[i] 表示到 i 选了数的个数前缀和;

根据题意,可以建立以下三个限制关系:

s[bi] >= s[ai-1] + ci ( 1 <= i <= n)

s[i] >= s[i-1] + 0 ( 1 <= i <= mx)

s[i-1] >= s[i] + (-1) (1 <= i <= mx)

然后求最长路,可以发现其中的 dis 值不会多余增大,也就满足题意要求的最小集合条件;

1A了好开心!

代码如下:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;
int const maxn=;
int n,hd[maxn],ct,dis[maxn],mx;
bool vis[maxn];
queue<int>q;
struct N{
int to,nxt,w;
N(int t=,int n=,int w=):to(t),nxt(n),w(w) {}
}ed[maxn*];
void add(int x,int y,int z){ed[++ct]=N(y,hd[x],z); hd[x]=ct;}
void spfa()
{
memset(dis,-,sizeof dis);
dis[]=; vis[]=; q.push();
while(q.size())
{
int x=q.front(); q.pop(); vis[x]=;
for(int i=hd[x];i;i=ed[i].nxt)
{
int u=ed[i].to;
if(dis[u]<dis[x]+ed[i].w)
{
dis[u]=dis[x]+ed[i].w;
if(!vis[u])vis[u]=,q.push(u);
}
}
}
}
int main()
{
scanf("%d",&n);
for(int i=,a,b,c;i<=n;i++)
{
scanf("%d%d%d",&a,&b,&c); if(a>b)swap(a,b);
mx=max(mx,max(a,b));
add(a-,b,c);
}
mx=max(mx,n);
for(int i=;i<=mx;i++)//
{
add(i-,i,); add(i,i-,-);
}
spfa();
printf("%d",dis[mx]);
return ;
}

poj1201 Intervals——差分约束的更多相关文章

  1. POJ1201 Intervals(差分约束)

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

  2. hdu 1384 Intervals (差分约束)

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

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

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

  4. zoj 1508 Intervals (差分约束)

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

  5. poj 1201 Intervals(差分约束)

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

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

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

  7. POJ1201基础差分约束

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

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

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

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

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

随机推荐

  1. 【转载】Appium环境搭建(Windows版)

    注:appium安装到C盘,node.js安装到C盘 一.安装node.js 1.到官网下载node.js:https://nodejs.org/en/download/ 2.获取到安装文件后,直接双 ...

  2. HDU - 2018 - 母牛的故事(dp)

    题意: 如题 思路: 递推的思想,牛只能在第4年才能开始生小牛,对于 第n年有多少牛 = n-1年的牛数量 + 新出生的牛的数量 新出生的牛的数量 = 已经出生满4年的牛的数量 = n-3年时候牛的数 ...

  3. Python学习第二阶段,day1, 装饰器,生成器,迭代器

    装饰器 不得不说,这是对初学者最难以理解的概念了,虽然我学过面向对象,但还是被搞懵逼了..前面还好理解,主要是后面“装饰器的装饰器”我理解不了.装饰器工厂,根据传入的参数不同去返回不同的装饰器,我不得 ...

  4. 爬虫----Web_WeChat

    流程: 打开的web_wechat,就有出现二维码,在network中,name中login?loginicon中,status的状态是pending,pending的意思是前端发送了一个请求,但是还 ...

  5. PAT 1123 Is It a Complete AVL Tree

    An AVL tree is a self-balancing binary search tree. In an AVL tree, the heights of the two child sub ...

  6. [bzoj4247][挂饰] (动规+排序)

    Description JOI君有N个装在手机上的挂饰,编号为1...N. JOI君可以将其中的一些装在手机上. JOI君的挂饰有一些与众不同——其中的一些挂饰附有可以挂其他挂件的挂钩.每个挂件要么直 ...

  7. 【11】AngularJS HTML DOM

    AngularJS HTML DOM AngularJS 为 HTML DOM 元素的属性提供了绑定应用数据的指令. ng-disabled 指令 ng-disabled 指令直接绑定应用程序数据到 ...

  8. [K/3Cloud]如何解决K3Cloud 2.0审批流提交时报“队列不存在,或您没有足够的权限执行该操……

    按照图上的操作即可解决不可提交的问题,但如果应用服务器是部署在域环境下,应该不会出错,这是微软support上说的

  9. [bzoj 1093][ZJOI2007]最大半联通子图(强联通缩点+DP)

    题目:http://www.lydsy.com:808/JudgeOnline/problem.php?id=1093 分析: 首先肯定是先把强联通全部缩成一个点,然后成了一个DAG 下面要知道一点: ...

  10. MYSQL中有关表的简单操作

    #创建表 CREATE TABLE table02( tid INT, tname VARCHAR(20)); #查看所有表SHOW TABLES; #查看表的结构DESC table01; #修改表 ...