POJ 2101 Intervals 差分约束
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 27746 | Accepted: 10687 |
Description
Write a program that:
reads the number of intervals, their end points and integers c1, ..., cn from the standard input,
computes the minimal size of a set Z of integers which has at least ci common elements with interval [ai, bi], for each i=1,2,...,n,
writes the answer to the standard output.
Input
Output
Sample Input
5
3 7 3
8 10 3
6 8 1
1 3 1
10 11 1
Sample Output
6
Source
差分约束系统是线性规划中的一种,在一个差分约束系统中,可以看成一个矩阵乘以一个向量小于另一个向量,求其中向量两个坐标的距离关系,约束条件对的不等式和单元最短路的松弛操作十分类似!
抽象出节点,根据节点性质和题目信息建边,最短路即可。
注意一定<=建边!
如果出现负权回路说明无解!
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<sstream>
#include<algorithm>
#include<queue>
#include<deque>
#include<iomanip>
#include<vector>
#include<cmath>
#include<map>
#include<stack>
#include<set>
#include<functional>
#include<memory>
#include<list>
#include<string>
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
//[a,b]区间内至少有c个数在集合内,问集合最少包含多少点
//a,b 可以取0 再读入的时候手动 a++,b++
//定义ti 为[0,i]内至少有多少个数字,那么由ta-1 - tb <= -c
//由ti的定义可以推出它的性质1.ti-ti+1<=0 ti+1-ti<=1 const int MAXM = * + ;
const int MAXN = + ; struct edge
{
LL to, next, dis;
}E[MAXM];
LL head[MAXN],tot;
LL dist[MAXN];
bool vis[MAXN];
void init()
{
tot = ;
memset(head, -, sizeof(head));
}
void spfa(LL ed)
{
memset(dist, 0x3f3f3f3f, sizeof(dist));
memset(vis, false, sizeof(vis));
queue<LL> q;
q.push();
vis[] = true;
dist[] = ;
while (!q.empty())
{
LL f = q.front();
q.pop();
vis[f] = false;
for (LL i = head[f]; i != -; i = E[i].next)
{
LL v = E[i].to, d = E[i].dis;
if (dist[v] > dist[f] + d)
{
dist[v] = dist[f] + d;
if (!vis[v])
{
vis[v] = true;
q.push(v);
}
}
}
}
cout << -dist[ed] << endl;
}
void addedge(LL u, LL v, LL d)
{
E[tot].to = v;
E[tot].dis = d;
E[tot].next = head[u];
head[u] = tot++;
}
int main()
{
ios::sync_with_stdio();
init();
LL f, t, d, ed;
LL n;
cin >> n;
while (n--)
{
cin >> f >> t >> d;
f++, t++;
addedge(f - , t, -d);
ed = max(t, ed);
}
for (int i = ; i < ed; i++)
{
addedge(i, i + , );
addedge(i + , i, );
}
spfa(ed);
}
POJ 2101 Intervals 差分约束的更多相关文章
- poj 1201 Intervals(差分约束)
题目:http://poj.org/problem?id=1201 题意:给定n组数据,每组有ai,bi,ci,要求在区间[ai,bi]内至少找ci个数, 并使得找的数字组成的数组Z的长度最小. #i ...
- poj 1201 Intervals——差分约束裸题
题目:http://poj.org/problem?id=1201 差分约束裸套路:前缀和 本题可以不把源点向每个点连一条0的边,可以直接把0点作为源点.这样会快许多! 可能是因为 i-1 向 i 都 ...
- poj 1716 Integer Intervals (差分约束 或 贪心)
Integer Intervals Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 12192 Accepted: 514 ...
- POJ 1201 Intervals (差分约束系统)
题意 在区间[0,50000]上有一些整点,并且满足n个约束条件:在区间[ui, vi]上至少有ci个整点,问区间[0, 50000]上至少要有几个整点. 思路 差分约束求最小值.把不等式都转换为&g ...
- POJ1201 Intervals(差分约束)
Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 28416 Accepted: 10966 Description You ...
- hdu 1384 Intervals (差分约束)
Intervals Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total ...
- zoj 1508 Intervals (差分约束)
Intervals Time Limit: 10 Seconds Memory Limit: 32768 KB You are given n closed, integer interva ...
- poj1201 Intervals——差分约束
题目:http://poj.org/problem?id=1201 差分约束裸题: 设 s[i] 表示到 i 选了数的个数前缀和: 根据题意,可以建立以下三个限制关系: s[bi] >= s[a ...
- POJ——3169Layout(差分约束)
POJ——3169Layout Layout Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 14702 Accepted ...
随机推荐
- iOS Programming Dynamic Type 1
iOS Programming Dynamic Type 1 Dynamic Type is a technology introduced in iOS 7 that helps realize ...
- php5.6.30开启redis扩展
注:5.6版本的php一定要下载phpredis3.0以上的版本,之前自己下载用的2.2.4的redis,安装配置完成后,PHP死活不支持redis的扩展,通过phpinfo打印也压根看不到,重复服务 ...
- 契约式设计(DbC)感想(一)
契约式设计可以理解为正则编程的一种实践: 如果用我的三脚猫能力将这种实践方法形式化的话,大致如下(如有不正确处,请不吝指正): 1.对于方法Method的precondition & post ...
- OpenCV2:应用篇 QT+OpenCV实现图片编辑器
一.简介 做完会放在Github上
- python note of class
reference: https://www.zhihu.com/question/27699413/answer/267906889 摘要: 我们在描述一个真实对象(物体)时包括两个方面:它可以做什 ...
- NOIP专题复习3 图论-强连通分量
目录 一.知识概述 二.典型例题 1.[HAOI2006]受欢迎的牛 2.校园网络[[USACO]Network of Schools加强版] 三.算法分析 (一)Tarjan算法 (二)解决问题 四 ...
- NOIp十连测 涂色游戏
[问题描述]小A 和小B 在做游戏.他们找到了一个n 行m 列呈网格状的画板.小A 拿出了p 支不同颜色的画笔,开始在上面涂色.看到小A 涂好的画板,小B 觉得颜色太单调了,于是把画板擦干净,希望涂上 ...
- 配置jdk环境变量和配置的作用
对于JDK要配置三个环境变量,分别是JAVA_HOME.path.classpath 对于我本人电脑来说,配置如下: JAVA_HOME:C:\Program Files\Java\jdk1.8.0_ ...
- MySQL主从复制遇到的问题
show slave status\G时,io显示:Slave_IO_State: Connecting 1.检查网络是否畅通,方法:ping主机ip.主机通畅. 2.检查复制用户的账号密码是否正确. ...
- [Python3网络爬虫开发实战] 1.9.6-Gerapy的安装
Gerapy是一个Scrapy分布式管理模块,本节就来介绍一下它的安装方式. 1. 相关链接 GitHub:https://github.com/Gerapy 2. pip安装 这里推荐使用pip安装 ...