zoj 1508 Intervals (差分约束)
Intervals
Time Limit: 10 Seconds Memory Limit: 32768 KB
You are given n closed, integer intervals [ai, bi] and n integers c1, ..., cn.
Write a program that:
> reads the number of intervals, their endpoints 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
The first line of the input contains an integer n (1 <= n <= 50 000) - the number of intervals. The following n lines describe the intervals. The i+1-th line of the input contains three integers ai, bi and ci separated by single spaces and such that 0 <= ai <= bi <= 50 000 and 1 <= ci <= bi - ai + 1.
Process to the end of file.
Output
The output contains exactly one integer equal to the minimal size of set Z sharing at least ci elements with interval [ai, bi], for each i = 1, 2, ..., n.
Sample Input
5
3 7 3
8 10 3
6 8 1
1 3 1
10 11 1
Sample Output
6
Source: Southwestern Europe 2002
//2013-11-25 09:45:31 Accepted 1508 C++ 2120 2124
/* 题意:
有一个序列,题目用n个整数组合 [ai,bi,ci]来描述它,[ai,bi,ci]表示在该
序列中处于[ai,bi]这个区间的整数至少有ci个。如果存在这样的序列,请求出满足
题目要求的最短的序列长度是多少。如果不存在则输出 -1。 差分约束:
第一题差分约束,感觉还好,基本看过资料的应该都可以做。
难点在于构图部分,构好图后直接求其单源最短路径。
构图就是要满足
1)S(bi) - S(a(i-1))>=Ci
2)Si - S(i-1)>=0
3)S(i-1) - Si>=-1 即 map[bi][a(i-1)]=-ci
map[i][i-1]=0
map[i-1][i]=1 然后在采用最短路算法求点n到点0的最短路,值再取反即为解
存在负权环时不存在 */
#include<stdio.h>
#include<string.h>
#define N 50005
#define inf 0x7ffffff
using namespace std;
struct node{
int u,v,w;
}edge[*N];
int n,edgenum,m;
int d[N];
int Max(int a,int b)
{
return a>b?a:b;
}
void addedge(int u,int v,int w)
{
edge[edgenum].u=u;
edge[edgenum].v=v;
edge[edgenum++].w=w;
}
bool bellman_ford()
{
bool flag;
for(int i=;i<=m;i++) d[i]=inf;
d[m]=;
for(int i=;i<m;i++){
flag=false;
for(int j=;j<edgenum;j++){
if(d[edge[j].u]!=inf && d[edge[j].v]>d[edge[j].u]+edge[j].w){
flag=true;
d[edge[j].v]=d[edge[j].u]+edge[j].w;
}
}
if(!flag) break;
}
for(int i=;i<edgenum;i++)
if(d[edge[i].u]!=inf && d[edge[i].v]>d[edge[i].u]+edge[i].w)
return false;
return true;
}
int main(void)
{
int a,b,c;
while(scanf("%d",&n)!=EOF)
{
edgenum=;
m=;
for(int i=;i<n;i++){
scanf("%d%d%d",&a,&b,&c);
addedge(b,a-,-c);
m=Max(m,Max(a,b));
}
for(int i=;i<m;i++){
addedge(i,i+,);
addedge(i+,i,);
}
if(!bellman_ford()) puts("-1");
else printf("%d\n",-d[]);
}
return ;
}
贴一下SPFA的解法:
//2013-11-25 22:05:24 Accepted 1508 C++ 130 5444
#include<iostream>
#include<vector>
#include<queue>
#include<stdio.h>
#include<string.h>
#define N 50005
#define inf 0x7ffffff
using namespace std;
struct node{
int v,w;
node(int a,int b){
v=a;w=b;
}
};
vector<node>V[N];
int d[N],vis[N],in[N];
int n,m;
int spfa()
{
memset(vis,,sizeof(vis));
memset(in,,sizeof(in));
for(int i=;i<=m;i++) d[i]=-inf;
queue<int>Q;
Q.push(m);
d[m]=;
while(!Q.empty()){
int u=Q.front();
Q.pop();
if(in[u]>m) return ;
vis[u]=;
int n0=V[u].size();
for(int i=;i<n0;i++){
int v=V[u][i].v;
int w=V[u][i].w;
if(d[v]<d[u]+w){
d[v]=d[u]+w;
if(!vis[v]){
in[v]++;
Q.push(v);
vis[v]=;
}
}
}
}
return ;
}
int main(void)
{
int a,b,c;
while(scanf("%d",&n)!=EOF)
{
m=;
for(int i=;i<N;i++) V[i].clear();
for(int i=;i<n;i++){
scanf("%d%d%d",&a,&b,&c);
m=max(m,max(a,b));
V[b].push_back(node(a-,c));
}
for(int i=;i<m;i++){
V[i].push_back(node(i+,-));
V[i+].push_back(node(i,));
}
if(!spfa()) puts("impossible");
else printf("%d\n",d[]);
}
return ;
}
zoj 1508 Intervals (差分约束)的更多相关文章
- poj 1201/zoj 1508 intervals 差分约束系统
// 思路 : // 图建好后 剩下的就和上一篇的 火烧连营那题一样了 求得解都是一样的 // 所以稍微改了就过了 // 最下面还有更快的算法 速度是这个算法的2倍#include <ios ...
- 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 ...
- poj 1716 Integer Intervals (差分约束 或 贪心)
Integer Intervals Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 12192 Accepted: 514 ...
- 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 都 ...
- poj1201 Intervals——差分约束
题目:http://poj.org/problem?id=1201 差分约束裸题: 设 s[i] 表示到 i 选了数的个数前缀和: 根据题意,可以建立以下三个限制关系: s[bi] >= s[a ...
- POJ 1201 & HDU1384 & ZOJ 1508 Intervals(差分约束+spfa 求最长路径)
题目链接: POJ:http://poj.org/problem?id=1201 HDU:http://acm.hdu.edu.cn/showproblem.php? pid=1384 ZOJ:htt ...
- POJ 2101 Intervals 差分约束
Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 27746 Accepted: 10687 Description You ...
随机推荐
- 15、SpringBoot------整合swagger2
开发工具:STS 前言: 对外提供一个Api,无论是对开发.测试.维护,都有很大的帮助. 下面我们来实现swagger2. 参考实例:https://blog.csdn.net/weixin_3947 ...
- jsonp 请求和回传实现
JSONP最主要的是可以解决跨域问题,不然谁会没事用这种格式. 下面是我用JSONP的一些心得体会: JSONP是JSON with Padding的略称.它是一个非官方的协议,它允许在服务器端集成S ...
- Java连接数据库的一个问题
问题描述: 利用HTML+servlet+MySQL写一个简单的登录注册案例,抛出了异常No suitable driver found for jdbc 解决方法 将mysql-connector- ...
- python中正则表达式在中文字符串匹配时的坑
之前一直有使用python 正则表达式来做中文字符串或者中英文数字混合的字符串的匹配,发现有不少情况下会匹配失灵或者结果混乱,并且在不同操作系统上匹配结果也不一致,查了很久都不知道是什么原因.今天终于 ...
- HyperLedger Fabric 1.4 区块链技术发展(1.3)
区块链技术发展经历区块链1.0(数字货币).区块链2.0(数字资产与智能合约)和区块链3.0(各种行业分布式应用落地)三个阶段.区块链在应用上分为公有链(PublicBlockChains).联盟链( ...
- Ubuntu 16.04上安装并配置Postfix作为只发送SMTP服务器
如果大家已经在使用第三方邮件服务方案发送并收取邮件,则无需运行自己的邮件服务器.然而,如果大家管理一套云服务器,且其中安装的应用需要发送邮件通知,那么运行一套本地只发送SMTP服务器则更为理想. 如何 ...
- 四大VDI客户端 总有一款适合你
[TechTarget中国原创] 交付虚拟桌面时IT管理员必须要考虑到用户如何访问虚拟桌面,因为这会影响用户体验以及VDI部署最终的成败. IT可以转向简便的HTML5客户端,HTML 5客户端功能丰 ...
- 三层还是DDD,ORM还是Ado.Net,何去何从?
我本想把这个问题放到博问去,前几次有去博问问过之类的问题,无奈大神们可能都不屑回答别人的低级问题.所以放到随笔里,一方面把自己对ORM.架构的一些看法写下来抛砖引玉,另一方面最主要的是想寻求大神们指指 ...
- USACO Section1.5 Number Triangles 解题报告
numtri解题报告 —— icedream61 博客园(转载请注明出处)--------------------------------------------------------------- ...
- python中subprocess.Popen执行命令并持续获取返回值
先举一个Android查询连接设备的命令来看看Python中subprocess.Popen怎么样的写法.用到的命令为 adb devices. import subprocess order='ad ...