【poj1201】 Intervals
http://poj.org/problem?id=1201 (题目链接)
题意
给出n个区间${[ai,bi]}$,要求选出尽可能少的数,使得每个区间i中至少存在${c[i]}$个数。
Solution
差分约束。
区间可以表示为${sum[b_i]-sum[a_i-1]}$,所以可以列出n个不等式:${sum[b_i]-sum[a_i-1]>=c[i]}$,然后每个${sum[x]}$满足$${0<=sum[x+1]-sum[x]<=1}$$$${sum[x+1]-sum[x]>=0,sum[x]-sum[x+1]>=-1}$$
这样的话构图就确定了连通性。建完图后,跑SPFA最长路即可。
代码
// poj1201
#include<algorithm>
#include<iostream>
#include<cstdlib>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<queue>
#define LL long long
#define inf 2147483640
#define MOD 998244353
#define Pi acos(-1.0)
#define free(a) freopen(a".in","r",stdin),freopen(a".out","w",stdout);
using namespace std;
inline LL getint() {
int f,x=0;char ch=getchar();
while (ch<='0' || ch>'9') {if (ch=='-') f=-1;else f=1;ch=getchar();}
while (ch>='0' && ch<='9') {x=x*10+ch-'0';ch=getchar();}
return x*f;
} const int maxn=50010;
struct edge {int to,w,next;}e[maxn<<2];
int vis[maxn],dis[maxn],head[maxn],n,cnt,L,R; void insert(int u,int v,int w) {
e[++cnt].to=v;e[cnt].next=head[u];head[u]=cnt;e[cnt].w=w;
}
int SPFA() {
queue<int> q;
for (int i=L;i<=R;i++) {
vis[i]=1;
dis[i]=0;
q.push(i);
}
while (!q.empty()) {
int x=q.front();
q.pop();
vis[x]=0;
for (int i=head[x];i;i=e[i].next)
if (e[i].w+dis[x]>dis[e[i].to]) {
dis[e[i].to]=e[i].w+dis[x];
if (!vis[e[i].to]) {vis[e[i].to]=1;q.push(e[i].to);}
}
}
return dis[R];
}
int main() {
while (scanf("%d",&n)!=EOF) {
memset(head,0,sizeof(head));
L=inf,R=0;
for (int u,v,w,i=1;i<=n;i++) {
scanf("%d%d%d",&u,&v,&w);
insert(u-1,v,w);
L=min(L,u-1);
R=max(R,v);
}
for (int i=L;i<=R;i++) {
insert(i,i+1,0);
insert(i+1,i,-1);
}
printf("%d\n",SPFA());
}
return 0;
}
【poj1201】 Intervals的更多相关文章
- 【POJ】【3680】Intervals
网络流/费用流 引用下题解: lyd: 首先把区间端点离散化,设原来的数值i离散化后的标号是c[i].这样离散化之后,整个数轴被分成了一段段小区间. 1.建立S和T,从S到离散化后的第一个点连容量K, ...
- 【POJ1021】Intervals (最短路解差分约束)
题目: Sample Input 5 3 7 3 8 10 3 6 8 1 1 3 1 10 11 1 Sample Output 6 题意: 我们选数,每个数只能选一次.给定n个条件[ai,bi]和 ...
- 【SPOJ116】Intervals
题目大意:有 N 个区间,在区间 [a, b] 中至少取任意互不相同的 c 个整数.求在满足 N 个区间约束的情况下,至少要取多少个正整数. 题解:差分约束系统模板题. 差分约束系统是对于 N 个变量 ...
- 【XSY1841】Intervals
Description 在一个长度为m的序列中选出n个区间,这些区间互不包含,且至少有一个区间的左端点为x. 问有多少种方案,注意交换两个区间的顺序视为不同方案. 答案很大,输出模10000000 ...
- 【POJ3680】Intervals(费用流)
题意:有n条线段,每条有起点,终点和一个权值 要求选取一些线段,使它们的权值和最大,并且使每一个点被覆盖不超过k次 1 ≤ K ≤ N ≤ 200 1 ≤ ai < bi ≤ 100,000, ...
- 【题解】Intervals
题目大意 有\(n\)个区间(\(1 \leq n \leq 200\)),第\(i\)个区间覆盖\((a_{i}, b_{i})\)且有权值\(w_{i}\)(\(1 \leq a_{i} &l ...
- 【HDOJ图论题集】【转】
=============================以下是最小生成树+并查集====================================== [HDU] How Many Table ...
- 【POJ 1201 Intervals】
Time Limit: 2000MSMeamory Limit: 65536K Total Submissions: 27949Accepted: 10764 Description You are ...
- 【POJ 1201】 Intervals(差分约束系统)
[POJ 1201] Intervals(差分约束系统) 11 1716的升级版 把原本固定的边权改为不固定. Intervals Time Limit: 2000MS Memory Limit: ...
随机推荐
- 紧接上篇,jQuery调用jsonp,并且在页面上展示
在上篇中提到了spring4.1+支持jsonp的调用,做了个例子,用来在页面上展示jsonp: (js写的丑了点,本人后端出生,前端大侠们轻拍~) var Menu = function () { ...
- JetBrain WebStorm 注册码
webStorm : UserName:William ===== LICENSE BEGIN ===== 45550-12042010 00001SzFN0n1bPII7FnAxnt0DDOPJA ...
- poj 1163 The Triangle
The Triangle Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 43809 Accepted: 26430 De ...
- IEnumerable和IEnumerator 详解 (转)
原文链接:http://blog.csdn.net/byondocean/article/details/6871881 参考链接:http://www.cnblogs.com/hsapphire/a ...
- StaticFileMiddleware中间件如何处理针对文件请求
StaticFileMiddleware中间件如何处理针对文件请求 我们通过<以Web的形式发布静态文件>和<条件请求与区间请求>中的实例演示,以及上面针对条件请求和区间请求的 ...
- C++哈弗曼编码
// haffman.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include<iostream> using name ...
- benchmark
redis benchmark How many requests per second can I get out of Redis? Using New Relic to Understand R ...
- [git]图解git常用命令
本文图解git中最常用的命令.如果你稍微理解git的工作原理,这篇文章能够让你理解的更透彻. 基本用法 约定 命令详解 Diff Commit Checkout Detached HEAD(匿名分支提 ...
- Linux操作系统基础(完结)
摘要 一.Linux操作系统概述 二.Linux操作系统安装 三.Linux文件系统及文件基础 四.Linux操作系统命令使用基础 五.Linux应用程序的安装与卸载基础 五.用户及进程 六.相关信息 ...
- [CareerCup] 13.10 Allocate a 2D Array 分配一个二维数组
13.10 Write a function in C called my2DAlloc which allocates a two-dimensional array. Minimize the n ...