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 ...
随机推荐
- android开发学习 ------- 弹出框
这是一种方法,是我觉得简单易懂代码量较少的一种: /* 创建AlertDialog对象并显示 */ final AlertDialog alertDialog = new AlertDialog.Bu ...
- shell编写的多服务器自动互信脚本(安装ceph)
相信大家都使用过分布式存储,而在分布式存储中较为出色的非ceph莫属了,但是这里就不深入聊ceph啦,我们只是聊聊安装ceph时遇到的问题. ceph需要多台主机进行ssh互信.三台还能忍受,但是当超 ...
- 绿化VSCode
通过啃源码, 终于找到了解决办法, 设置环境变量: VSCODE_APPDATA=C:\Program Files\VSCode\UserData VSCODE_EXTENSIONS=%VSCODE_ ...
- C++ class、struct区别
一.默认访问控制不同(最主要) struct默认为public,class默认为private.这个访问控制既是指成员的默认访问属性,又指继承时默认的继承属性. 二.定义template时不同 在模版 ...
- 转:谈谈iOS中粘性动画以及果冻效果的实现
在最近做个一个自定义PageControl——KYAnimatedPageControl中,我实现了CALayer的形变动画以及CALayer的弹性动画,效果先过目: 先做个提纲: 第一个分享的主题是 ...
- element-ui iview-admin 都是基于vue的ui框架
element-ui iview-admin 都是基于vue的ui框架
- EditControl 限制输入文本的三种方法
下边是三种限制编辑框输入内容的方法.在VS里建立基于对话框的应用程序CMyEdit,打开资源视图,删除对话框上自带的确定和取消按钮.然后添加一编辑框控件,将其ID修改为IDC_MY_EDIT,通过类向 ...
- 02CSS基本语法
CSS基本语法 id选择符 在HTML文档中,需要唯一标识一个元素时,就会赋予它一个id标识,以便在对整个文档进行处理时能够很快地找到这个元素. 而id选择符就是用来对这个单一元素定义单独的样式.#号 ...
- vue 清除keep-Alive页面缓存
- 启动web项目卡在Initializing Spring root WebApplicationContext不动
这几天在和同学一起做一个电教器材管理系统的Web项目,用SVN互通,在此记录下经常遇到的bug. Bug: 启动项目一直卡在Initializing Spring root WebApplicatio ...