poj 1201 Intervals 解题报告
| Time Limit: 2000MS | Memory Limit: 65536KB | 64bit IO Format: %lld & %llu |
Description
Write a program that:
reads the number of intervals, their end points 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
Output
Sample Input
5
3 7 3
8 10 3
6 8 1
1 3 1
10 11 1
Sample Output
6
Source
——————————————————我是华丽丽的分割线——————————————————
差分约束系统题目。
约束条件如下:
设S[i]为集合Z中小于等于i的元素个数,即S[i]=|{s|s属于Z,s<=i}|。则:
(1) S[b(i)]-S[a(i-1)]>=c(i) ------------> S[a(i-1)]-S[b(i)]<=-c(i)
(2) S[i]-S[i-1]<=1。
(3) S[i]-S[i-1]>=0,即S[i-1]-S[i]<=0.
设所有区间极右端点为mx,极左端点为mn 则 ans=min(s[mx]-s[mn-1]) 即求源点s[mx]与s[mn-1]之间的最短路 此题得解。
改进后方法如下: (1)先只用条件(1)构图,各顶点到源点的最短距离初始为0。 (2)即刻用Bellman-ford算法求各顶点到源点的最短路径。 p.s.在每次循环中,条件1判完后判断2和3.
2的判断: s[i]<=s[i-1]+1等价于s[i]-s[mx]<=s[i-1]-s[mx]+1 设dist[i]为源点mx到i的最短路径,则s[i]-s[mx]即为dist[i],s[i-1]-s[mx]+1即为dist[i-1]+1。 即若顶点i到源点的最短路径长度大于i-1到源点的最短路径长度+1,则dist[i]=dist[i-1]+1。
3的判断: s[i-1]<=s[i]等价于s[i-1]-s[mx]<=s[i]-s[mx] s[i]-s[mx]即为dist[i],s[i-1]-s[mx]即为dist[i-1]。 即若顶点i-1到源点的最短路径长度大于i到源点的最短路径长度,则dist[i-1]=dist[i]。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<queue>
#include<cstdlib>
#include<iomanip>
#include<cassert>
#include<climits>
#include<vector>
#include<list>
#include<map>
#define maxn 1000001
#define F(i,j,k) for(int i=j;i<=k;i++)
#define M(a,b) memset(a,b,sizeof(a))
#define FF(i,j,k) for(int i=j;i>=k;i--)
#define inf 0x7fffffff
#define maxm 2016
#define mod 1000000007
//#define LOCAL
using namespace std;
int read(){
int x=,f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
int n,m;
int lsh=inf,rsh;
struct EDGE
{
int from;
int to;
int value;
int next;
}e[maxn];
int head[maxn];
int tot;
inline void addedge(int u,int v,int w)
{
tot++;
e[tot].from=u;
e[tot].to=v;
e[tot].value=w;
e[tot].next=head[u];
head[u]=tot;
}
int f[maxn];
queue<int> q;
bool vis[maxn];
int d[maxn],in[maxn];
bool ford()
{
int i,t;
bool flag=true;
while(flag){
flag=false;
F(i,,n){
t=d[e[i].from]+e[i].value;
if(d[e[i].to]>t){
d[e[i].to]=t;
flag=true;
}
}
F(i,lsh,rsh){
t=d[i-]+;
if(d[i]>t){
d[i]=t;
flag=true;
}
}
FF(i,rsh,lsh){
t=d[i];
if(d[i-]>t){
d[i-]=t;
flag=true;
}
}
}
return true;
}
int main()
{
std::ios::sync_with_stdio(false);//cout<<setiosflags(ios::fixed)<<setprecision(1)<<y;
#ifdef LOCAL
freopen("data.in","r",stdin);
freopen("data.out","w",stdout);
#endif
cin>>n;M(head,-);
F(i,,n){
int a,b,c;
cin>>a>>b>>c;
addedge(b,a-,-c);
if(lsh>a) lsh=a;
if(rsh<b) rsh=b;
}
ford();
cout<<d[rsh]-d[lsh-]<<endl;
return ;
}
/*
5
3 7 3
8 10 3
6 8 1
1 3 1
10 11 1
*/
poj 1201
poj 1201 Intervals 解题报告的更多相关文章
- 【LeetCode】435. Non-overlapping Intervals 解题报告(Python)
[LeetCode]435. Non-overlapping Intervals 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemi ...
- POJ 2002 Squares 解题报告(哈希 开放寻址 & 链式)
经典好题. 题意是要我们找出所有的正方形.1000点,只有枚举咯. 如图,如果我们知道了正方形A,B的坐标,便可以推测出C,D两点的坐标.反之,遍历所有点作为A,B点,看C,D点是否存在.存在的话正方 ...
- POJ 1201 Intervals || POJ 1716 Integer Intervals 差分约束
POJ 1201 http://poj.org/problem?id=1201 题目大意: 有一个序列,题目用n个整数组合 [ai,bi,ci]来描述它,[ai,bi,ci]表示在该序列中处于[ai, ...
- poj 1201 Intervals(差分约束)
题目:http://poj.org/problem?id=1201 题意:给定n组数据,每组有ai,bi,ci,要求在区间[ai,bi]内至少找ci个数, 并使得找的数字组成的数组Z的长度最小. #i ...
- POJ 1201 Intervals(图论-差分约束)
Intervals Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 20779 Accepted: 7863 Descri ...
- 【原创】poj ----- 1182 食物链 解题报告
题目地址: http://poj.org/problem?id=1182 题目内容: 食物链 Time Limit: 1000MS Memory Limit: 10000K Total Submi ...
- poj 1201 Intervals——差分约束裸题
题目:http://poj.org/problem?id=1201 差分约束裸套路:前缀和 本题可以不把源点向每个点连一条0的边,可以直接把0点作为源点.这样会快许多! 可能是因为 i-1 向 i 都 ...
- POJ 1201 Intervals【差分约束】
传送门:http://poj.org/problem?id=1201 题意: 有n个如下形式的条件:,表示在区间[, ]内至少要选择个整数点.问你满足以上所有条件,最少需要选多少个点? 思路:第一道差 ...
- 【LeetCode】56. Merge Intervals 解题报告(Python & C++ & Java)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
随机推荐
- Codeforces Round #248 (Div. 1) C - Tachibana Kanade's Tofu AC自动机
C - Tachibana Kanade's Tofu 思路:把 n 个串丢进AC自动机中,然后dp就好啦. 我的代码居然是在CF上跑最快的.. #include<bits/stdc++.h&g ...
- memory_get_usage()查看PHP脚本使用内存
memory_get_usage()可以查看当前php使用的内存大小.对于优化算法提高内存使用效率还是很实用的,尤其是对当下的移动端程序. <?php echo memory_get_usage ...
- CentOS7的一些指令
hostnamectl --static set-hostname yuanxu#永久修改主机名 systemctl stop firewalld.service #停止firewall system ...
- 洛谷——P2936 [USACO09JAN]全流Total Flow
题目描述 Farmer John always wants his cows to have enough water and thus has made a map of the N (1 < ...
- Linux C函数库参考手册
目录 第1章 字符测试函数 isalnum(测试字符是否为英文字母或数字) isalpha(测试字符是否为英文字母) isascii(测试字符是否为ascii码字符) isblank(测试字符是否为空 ...
- centos+uwsgi+nginx+python+django服务器安装配置
1.ssh登录后使用fdisk –l查看需要格式化硬盘的名称: 2.运行fdisk /dev/vdb,对数据盘进行分区,按照提示,依次输入n,p,1,两次回车,wq,分区开始.(注意数据盘的名称,和阿 ...
- 用Win32编写发送消息至Notepad++的程序
这次利用Win32编程写一个发送"Win32 Assembly,My First SendMessage Program !" 每个程序要发送消息至另一个程序的时候,通常使用Sen ...
- [CodeForces-585F]Digits of Number Pi
题目大意: 给你一个数字串s,一个序列范围l和r,(l和r的数字位数为d)求l到r中有多少个数,满足它的长度为d/2的子串,能够在s中被匹配. 思路: 首先将s中每一个长度为d/2的子串插入后缀自动机 ...
- hdu 1514 记忆化搜索
题意是给4堆(堆的高度小于等于40)有颜色(颜色的种类小于等于20)的物品,你有一个篮子最多能装5件物品,每次从这4堆物品里面任取一件物品放进篮子里,但是取每堆物品时,必须先取上面的物品,才能取下面的 ...
- IE7 css兼容问题
1,float:right; 在IE错位问题 : 使用position:absolute:right:0px; 2,汉字在float状态下 折行 ,可能是因为父级宽度不够, 改用 display:in ...