POJ 1201 Intervals (差分约束系统)
题意
在区间[0,50000]上有一些整点,并且满足n个约束条件:在区间[ui, vi]上至少有ci个整点,问区间[0, 50000]上至少要有几个整点。
思路
差分约束求最小值。把不等式都转换为>=形式,那么显然有xvi >= xui-1 + ci,那么就在约束图中连一条(ui-1, vi, ci)的边;当然不要忘记隐含的不等式:xi >= xi-1 + 0; xi-1 >= xi -1.
建完图后SPFA求最长路径即可
代码
[cpp]
#include <iostream>
#include <cstdio>
#include <cmath>
#include <vector>
#include <algorithm>
#include <string>
#include <queue>
#include <cstring>
#define MID(x,y) ((x+y)/2)
#define MEM(a,b) memset(a,b,sizeof(a))
#define REP(i, begin, m) for (int i = begin; i < begin+m; i ++)
using namespace std;
const int MAXN = 50005;
const int oo = 0x3fffffff;
struct Edge{
int to;
int w;
Edge(){}
Edge(int _to, int _w){ to = _to; w = _w; }
};
struct Gragh{
vector <Edge> adj[MAXN];
queue <int> q;
int vn;
int dist[MAXN], inq_num[MAXN];
bool inq[MAXN];
void init(int n){
vn = n;
for (int i = 0; i <= n; i ++)
adj[i].clear();
}
//if xj >= xi + c, add (i, j, c)
void add_edge(int u, int v, int w){
adj[u].push_back(Edge(v, w));
}
//spfa calculate longest path
bool solve(int s, int t){
while(!q.empty())
q.pop();
MEM(inq, false); MEM(inq_num, 0); MEM(dist, -1); //Note : dist shouldn't initially be 0
dist[s] = 0; inq[s] = true; inq_num[s] ++; q.push(s);
while(!q.empty()){
int u = q.front();
q.pop();
inq[u] = false;
for (int i = 0; i < adj[u].size(); i ++){
int v = adj[u][i].to;
if (dist[v] < dist[u] + adj[u][i].w){
dist[v] = dist[u] + adj[u][i].w;
if (!inq[v]){
inq[v] = true;
inq_num[v] ++;
if (inq_num[v] > vn)
return false;
q.push(v);
}
}
}
}
if (dist[t] < oo){
return true;
}
}
}spfa;
struct intervals{
int u, v, w;
}inte[MAXN];
int main(){
//freopen("test.in", "r", stdin);
//freopen("test.out", "w", stdout);
int n;
while(scanf("%d", &n) != EOF){
int maxn = 0;
REP(i, 1, n){
scanf("%d %d %d", &inte[i].u, &inte[i].v, &inte[i].w);
inte[i].u ++, inte[i].v ++;
maxn = max(maxn, inte[i].v);
}
spfa.init(maxn+1);
REP(i, 1, maxn){
spfa.add_edge(i-1, i, 0);
spfa.add_edge(i, i-1, -1);
}
REP(i, 1, n){
spfa.add_edge(inte[i].u-1, inte[i].v, inte[i].w);
}
if (spfa.solve(0, maxn)){
printf("%d\n", spfa.dist[maxn]);
}
}
return 0;
}
[/cpp]
POJ 1201 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 都 ...
- PKU 1201 Intervals(差分约束系统+Spfa)
题目大意:原题链接 构造一个集合,这个集合内的数字满足所给的n个条件,每个条件都是指在区间[a,b]内至少有c个数在集合内.问集合最少包含多少个点.即求至少有多少个元素在区间[a,b]内. 解题思路: ...
- poj 1201 Intervals(差分约束)
做的第一道差分约束的题目,思考了一天,终于把差分约束弄懂了O(∩_∩)O哈哈~ 题意(略坑):三元组{ai,bi,ci},表示区间[ai,bi]上至少要有ci个数字相同,其实就是说,在区间[0,500 ...
- POJ 1201 Intervals(图论-差分约束)
Intervals Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 20779 Accepted: 7863 Descri ...
- POJ 1201 Intervals(差分约束 区间约束模版)
关于差分约束详情可阅读:http://www.cppblog.com/menjitianya/archive/2015/11/19/212292.html 题意: 给定n个区间[L,R], 每个区间至 ...
- POJ 1201 Intervals || POJ 1716 Integer Intervals 差分约束
POJ 1201 http://poj.org/problem?id=1201 题目大意: 有一个序列,题目用n个整数组合 [ai,bi,ci]来描述它,[ai,bi,ci]表示在该序列中处于[ai, ...
- poj 1201 Intervals 解题报告
Intervals Time Limit: 2000MS Memory Limit: 65536KB 64bit IO Format: %lld & %llu Submit Statu ...
- Intervals(差分约束系统)
http://poj.org/problem?id=1201 题意:给定n个整数闭区间[a,b]和n个整数c,求一个最小的整数集合Z,满足Z里边的数中范围在闭区间[a,b]的个数不小于c个. 思路:根 ...
随机推荐
- javascript利用拷贝的方法实现导出excel(可以导出表格线)
Js代码: <script language=javascript> function preview() { window.clipboardData.setData("Tex ...
- root 授权
错误:The user specified as a definer ('root'@'%') does not exist 解决: grant all privileges on *.* to ro ...
- C#遍历打印机
#region 获取本机默认打印机名称 ArrayList al1=new ArrayLIst(); private static PrintDocument fPrintDocument = new ...
- lintcode:Pow(x, n)
Pow(x, n) Implement pow(x, n). 解题 直接顺序求解,时间复杂度O(N) public class Solution { /** * @param x the base n ...
- Linux下find一次查找多个指定类型文件,指定文件或者排除某类文件,在 GREP 中匹配多个关键 批量修改文件名等
http://blog.sina.com.cn/s/blog_62e7fe670101dg9d.html linux下二进制文件查找: strings 0000.ts | grep -o " ...
- haproxy.cfg
# this config needs haproxy-1.1.28 or haproxy-1.2.1 global log 127.0.0.1 local0 log 127.0.0.1 local1 ...
- VCC、VDD、VEE、VSS等有关电源标注的区别
Almost all integrated circuits (ICs) have at least two pins which connect to the power rails of the ...
- Axure 注册码
用户名:axureuser 序列号:8wFfIX7a8hHq6yAy6T8zCz5R0NBKeVxo9IKu+kgKh79FL6IyPD6lK7G6+tqEV4LG
- MSBuild和Jenkins搭建持续集成环境
http://www.2cto.com/os/201409/334323.html http://my.oschina.net/anxuyong/blog/353897 http://www.cnbl ...
- SQL Server 联表字段合并查询
经常遇到统计报表中,子表记录合并为一个字段的情况.例如:省表中各省经济水平前五的城市统计. 有如下两表:dbo.省 和 dbo.市 (好吧,你可能会吐槽为什么用中文表名,其实我是为了方便查找替换) 这 ...