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,建立 一条 ...
随机推荐
- phpcms:三、头部包含
1‘标题:{if isset($SEO['title']) && !empty($SEO['title'])}{$SEO['title']}{/if}{$SEO['site_title ...
- iOS 使用pods报错问题 pod --version
错误信息如下 find_spec_for_exe': can't find gem cocoapods (>= 0.a) (Gem::GemNotFoundException) from /Us ...
- asp.net页面之间的跳转
调用Request.CurrentExecutionFilePath方法返回到当前页面 站点中常常要跳转页面,调用Request.CurrentExecutionFilePath方法能够获取当前页面的 ...
- Eclipse 将projectBuild Path中引用的jar包自己主动复制到WEB-INF下的lib目录下
在用用 Eclipse进行Java Web开发时,web应用中引用的jar须要复制到WEB-INF下的lib目录下,否则常常出现ClassNotFound异常. 通过以下方法,能够不用手动拷贝jar包 ...
- Solr 安装与集成IK中文分词器
创建wangchuanfu core 1. 在example目录下创建wangchuanfu-solr文件夹: 2. 将./solr下的solr.xml拷贝到wangchuanfu-solr目录下 ...
- vsftp虚拟用户登录配置详解
一.安装:1.安装Vsftpd服务:# yum install vsftpd 2.安装DB4部件包:这里要特别安装一个db4的包,用来支持文件数据库.# yum install db4-utils 二 ...
- Visual Studio 2015 开发MVC4出现错误
在Visual Studio 2015(以下简称VS2015)中开发MVC4项目时,编译报错"当前上下文中不存在ViewBag",一直无法编译,这个是否是VS2015的Bug? 本 ...
- C#窗体实现文件拖拽功能
1.首先要把你的窗体或者空间的AllowDrag属性设置为允许 2.注册DragEnter事件 3.获得文件路径,先通过e.Data.GetFormats()方法获得所有数据格式 4.调用e.GetD ...
- cellForRowAtIndexPath方法不执行的那些坑
今天用到了uitableview,是xib形式的.不过cellForRowAtIndexPath方法死活不执行,检查了返回的row数量,section的数量,数据源,代理都没问题,不过cellForR ...
- WCF Service端Inspector
问题 在使用WCF的过程中,有时候需要在service端截取client和service之间的消息来做一些如写log,检查message是否合法的操作. 那么如何才能实现呢? 解决方案 使用WCF提供 ...