Hdu1384-Intervals(差分约束)
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
Process to the end of file.
#include<cstdio>
#include<cstring>
#include<string>
#include<iostream>
#include<sstream>
#include<algorithm>
#include<utility>
#include<vector>
#include<set>
#include<map>
#include<queue>
#include<cmath>
#include<iterator>
#include<stack>
using namespace std;
const int INF=1e9+;
const double eps=1e-;
const int maxn=;
int N;
struct edge
{
int u,v,w,next;
edge(int u=,int v=,int w=):u(u),v(v),w(w){next=-; }
}E[maxn*];
int head[maxn],dist[maxn];
bool inq[maxn];
queue<int> que;
int spfa(int be,int en)
{
for(int i=be;i<=en;i++) dist[i]=-INF;
dist[be]=;
memset(inq,false,sizeof(inq));
while(!que.empty()) que.pop();
que.push(be);
while(!que.empty())
{
int u=que.front(); que.pop();
inq[u]=false;
for(int i=head[u];i!=-;i=E[i].next)
{
int v=E[i].v,w=E[i].w;
if(dist[v]<dist[u]+w) //更新
{
dist[v]=dist[u]+w;
if(!inq[v]){ inq[v]=true; que.push(v); }
}
}
}
return dist[en];
}
int main()
{
while(scanf("%d",&N)!=EOF)
{
memset(head,-,sizeof(head));
int u,v,w,cnt=;
int minv=INF,maxv=-INF;
for(int i=;i<N;i++)
{
scanf("%d%d%d",&u,&v,&w); //建边(u,v+1,w);
v++;
E[++cnt]=edge(u,v,w);
E[cnt].next=head[u];
head[u]=cnt;
minv=min(minv,u);
maxv=max(maxv,v);
}
for(int i=minv;i<maxv;i++)
{
E[++cnt]=edge(i,i+,); //建边(i,i+1,0)
E[cnt].next=head[i];
head[i]=cnt;
E[++cnt]=edge(i+,i,-); //建边(i+1,i,-1)
E[cnt].next=head[i+];
head[i+]=cnt;
}
printf("%d\n",spfa(minv,maxv));
}
return ;
}
Hdu1384-Intervals(差分约束)的更多相关文章
- poj1201/zoj1508/hdu1384 Intervals(差分约束)
转载请注明出处: http://www.cnblogs.com/fraud/ ——by fraud Intervals Time Limit: 10 Seconds Mem ...
- 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 ...
- zoj 1508 Intervals (差分约束)
Intervals Time Limit: 10 Seconds Memory Limit: 32768 KB You are given n closed, integer interva ...
- 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 2101 Intervals 差分约束
Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 27746 Accepted: 10687 Description You ...
- hdu 1384 Intervals (差分约束)
/* 给你 n 个区间 [Ai, Bi],要求从每一个区间中至少选出 Ci 个数出来组成一个序列 问:满足上面条件的序列的最短长度是多少? 则对于 不等式 f(b)-f(a)>=c,建立 一条 ...
随机推荐
- Java 8 新特性终极版
声明:本文翻译自Java 8 Features Tutorial – The ULTIMATE Guide,翻译过程中发现并发编程网已经有同学翻译过了:Java 8 特性 – 终极手册,我还是坚持自己 ...
- C语言伪随机数的生成
在stdlib.h中,有两个函数与伪随机数的生成有关:srand和rand.C语言中,随机数表有很多列,srand函数是根据其参数(unsigned类型)来获得一个种子(seed),根据种子来设置从哪 ...
- node js npm 和 cnpm的使用
安装nodejs后会有npm命令 npm 可以安装node插件 cnpm使用的是淘宝网的镜像http://npm.taobao.org 安装命令提示符执行:npm install cnpm -g -- ...
- python高级编程:有用的设计模式2
# -*- coding: utf-8 -*- __author__ = 'Administrator' #python高级编程:有用的设计模式 #代理 """ 代理对一 ...
- pyqt小例子 treewidget
# -*- coding: cp936 -*- from PyQt4.QtCore import * from PyQt4.QtGui import * class InlineEditor(QWid ...
- [Redux] Passing the Store Down Explicitly via Props
n the previous lessons, we used this tool to up level variable to refer to the Redux chore. The comp ...
- CSS background 属性 总结
CSS background 属性总结
- AngularJs练习Demo12Provider
@{ Layout = null; } <!DOCTYPE html> <html> <head> <meta name="viewport&quo ...
- log4net 使用步骤
1.安装log4net 官网首页:http://logging.apache.org/log4net/ 下载地址:http://logging.apache.org/log4net/download_ ...
- MFC中使用ADO方式连接数据库
文章转自:http://blog.sina.com.cn/s/blog_a43aba5601014z8h.html 一.数据库操作准备 1.导入ADO动态链接库 在工程的stdafx.h中加入如下语句 ...