Intervals---poj1201(差分约束系统)
题目链接:http://poj.org/problem?id=1201
题目说[ai, bi]区间内和点集Z至少有ci个共同元素,那也就是说如果我用Si表示区间[0,i]区间内至少有多少个元素的话,那么Sbi - Sai >= ci,这样我们就构造出来了一系列边,权值为ci,但是这远远不够,因为有很多点依然没有相连接起来(也就是从起点可能根本就还没有到终点的路线),此时,我们再看看Si的定义,也不难写出0<=Si - Si-1<=1的限制条件,虽然看上去是没有什么意义的条件,但是如果你也把它构造出一系列的边的话,这样从起点到终点的最短路也就顺理成章的出现了。
我们将上面的限制条件写为同意的形式:
Sbi - Sai >= ci
Si - Si-1 >= 0
Si-1 - Si >= -1
因为要求的是最小有多少个,所以要求最长路;
所以求出图中最小点到最大点的最大距离即可;
#include <iostream>
#include <stdio.h>
#include <math.h>
#include <string.h>
#include <queue>
#include <stack>
#include <algorithm>
#include <map>
#include <string>
typedef long long LL;
#define INF 0x3f3f3f3f
#define met(a, b) memset(a, b, sizeof(a))
#define N 51500 using namespace std; int S, E, vis[N], dist[N];
struct node
{
int u, v, w, Next;
}e[N*]; int Head[N], cnt; void Add(int u, int v, int w)
{
e[cnt].v = v;
e[cnt].w = w;
e[cnt].Next = Head[u];
Head[u] = cnt++;
} int spfa()
{
for(int i=; i<=E; i++)
dist[i] = -INF;
met(vis, );
queue<int> Q;
Q.push(S);
vis[S] = ;
dist[S] = ;
while(!Q.empty())
{
int p = Q.front();Q.pop();
vis[p] = ;
for(int i=Head[p]; i!=-; i=e[i].Next)
{
int q = e[i].v;
if(dist[q] < dist[p]+e[i].w)///最长路;
{
dist[q] = dist[p]+e[i].w;
if(!vis[q])
{
vis[q] = ;
Q.push(q);
}
}
}
}
return dist[E];
} int main()
{
int n, u, v, w;
while(scanf("%d", &n) != EOF)
{
met(e, );
met(Head, -);
cnt = ;
S = INF; E = -INF;///找到对应的起点和终点;
for(int i=; i<=n; i++)
{
scanf("%d %d %d", &u, &v, &w);
Add(u, v+, w);///u可能等于0,所以v+1比较好;
S = min(u, S);
E = max(v+, E);
}
for(int i=S; i<E; i++)///建立其他边,所以e要开3倍的N;
{
Add(i, i+, );
Add(i+, i, -);
}
int ans = spfa();
printf("%d\n", ans);
}
return ;
}
Intervals---poj1201(差分约束系统)的更多相关文章
- 【POJ 1716】Integer Intervals(差分约束系统)
id=1716">[POJ 1716]Integer Intervals(差分约束系统) Integer Intervals Time Limit: 1000MS Memory L ...
- 【POJ 1201】 Intervals(差分约束系统)
[POJ 1201] Intervals(差分约束系统) 11 1716的升级版 把原本固定的边权改为不固定. Intervals Time Limit: 2000MS Memory Limit: ...
- POJ1201 Intervals(差分约束系统)
与ZOJ2770一个建模方式,前缀和当作点. 对于每个区间[a,b]有这么个条件,Sa-Sb-1>=c,然后我就那样连边WA了好几次. 后来偷看数据才想到这题还有两个隐藏的约束条件. 这题前缀和 ...
- POJ1201 Intervals【差分约束系统】
Description You are given n closed, integer intervals [ai, bi] and n integers c1, ..., cn. Write a p ...
- poj1201 Intervals【差分约束+SPFA】
转载请注明出处,谢谢:http://www.cnblogs.com/KirisameMarisa/p/4303365.html ---by 墨染之樱花 题目链接:http://poj.org/pr ...
- POJ1201:Intervals【差分约束】
题目大意:给出N个闭区间,每个区间给出一个ci值,让你找出最小的数集Z使得每个闭区间都有不少于ci个Z中的元素,求card(Z) 思路:06年集训队论文<浅析差分约束系统>有详细的解题,设 ...
- Intervals(差分约束系统)
http://poj.org/problem?id=1201 题意:给定n个整数闭区间[a,b]和n个整数c,求一个最小的整数集合Z,满足Z里边的数中范围在闭区间[a,b]的个数不小于c个. 思路:根 ...
- Intervals(差分约束)
http://poj.org/problem?id=1201 题意:给出N个整数区间[ai,bi],并且给出一个约束ci,( 1<= ci <= bi-ai+1),使得数组Z在区间[ai, ...
- UVA11478 Halum [差分约束系统]
https://vjudge.net/problem/UVA-11478 给定一个有向图,每条边都有一个权值.每次你可以选择一个结点v和一个整数d,把所有以v为终点的边的权值减小d,把所有以v为起点的 ...
- BZOJ 2330: [SCOI2011]糖果 [差分约束系统] 【学习笔记】
2330: [SCOI2011]糖果 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 5395 Solved: 1750[Submit][Status ...
随机推荐
- BZOJ3483 : SGU505 Prefixes and suffixes(询问在线版)
将每个串正着插入Trie A中,倒着插入Trie B中. 并求出每个串在A,B中的dfs序. 每次查询等价于查询在A中dfs序在[la,ra]之间,在B中dfs序在[lb,rb]之间的串的个数,用主席 ...
- [leetCode][012] Two Sum (1)
[题目]: Given an array of integers, find two numbers such that they add up to a specific target number ...
- 使用jQuery操作Cookies的实现代码
Cookie是由服务器端生成,发送给User-Agent(一般是浏览器),浏览器会将Cookie的key/value保存到某个目录下的文本文件内,下次请求同一网站时就发送该Cookie给服务器(前提是 ...
- javascript第一弹——对象
一. 什么是对象 对象是包含一组变量(称为属性)和函数(称为方法)的集合的实例. javascript中所有事物都是对象 javascript有很多内建对象 javascript允许自定义对象 对象只 ...
- (转)js:字符串(string)转json
第一种方式: 使用js函数eval(); testJson=eval(testJson);是错误的转换方式. 正确的转换方式需要加(): testJson = eval("(" + ...
- MySQL添加字段和修改字段的方法
添加表字段 alter table table1 add transactor varchar(10) not Null; alter table table1 add id int unsign ...
- JS来添加弹出层,并且完成锁屏
上图 <html> <head> <title>弹出层</title> <style type="text/css"> ...
- HP StorageWorks MSL2024 Tape Libraries - Robotic Error Sub-Codes
Robotic error sub-codes Mechanical initialization failure 02 Connection to slave robotic failed 03 E ...
- 分布式架构高可用架构篇_07_MySQL主从复制的配置(CentOS-6.7+MySQL-5.6)
参考: 龙果学院http://www.roncoo.com/share.html?hamc=hLPG8QsaaWVOl2Z76wpJHp3JBbZZF%2Bywm5vEfPp9LbLkAjAnB%2B ...
- JavaScript系列:模块化与链式编程
模块化:闭包和和函数作用域(JS没有块级作用域ES6之前)构造模块 var man=function(){ var age=12; return { getYear:function(){ retur ...