poj1716 Integer Intervals(差分约束)
转载请注明出处: http://www.cnblogs.com/fraud/ ——by fraud
Time Limit: 1000MS | Memory Limit: 10000K |
Description
Write a program that: finds the minimal number of elements in a set
containing at least two different integers from each interval.
Input
first line of the input contains the number of intervals n, 1 <= n
<= 10000. Each of the following n lines contains two integers a, b
separated by a single space, 0 <= a < b <= 10000. They are the
beginning and the end of an interval.
Output
Sample Input
4
3 6
2 4
0 2
4 7
Sample Output
4
上一题差分约束的阉割版(传送门)
#include <iostream>
#include <cstring>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <queue>
using namespace std;
typedef long long ll;
typedef pair<int,int> PII;
#define MAXN 50010
#define REP(A,X) for(int A=0;A<X;A++)
#define INF 0x7fffffff
#define CLR(A,X) memset(A,X,sizeof(A))
struct node {
int v,d,next;
}edge[*MAXN];
int head[MAXN];
int e=;
void init()
{
REP(i,MAXN)head[i]=-;
}
void add_edge(int u,int v,int d)
{
edge[e].v=v;
edge[e].d=d;
edge[e].next=head[u];
head[u]=e;
e++;
}
bool vis[MAXN];
int dis[MAXN];
void spfa(int s){
CLR(vis,);
REP(i,MAXN)dis[i]=i==s?:INF;
queue<int>q;
q.push(s);
vis[s]=;
while(!q.empty())
{
int x=q.front();
q.pop();
int t=head[x];
while(t!=-)
{
int y=edge[t].v;
int d=edge[t].d;
t=edge[t].next;
if(dis[y]>dis[x]+d)
{
dis[y]=dis[x]+d;
if(vis[y])continue;
vis[y]=;
q.push(y);
}
}
vis[x]=;
}
}
int main()
{
ios::sync_with_stdio(false);
int n;
int u,v,d;
int ans=;
int maxn=;
int minn=MAXN;
while(scanf("%d",&n)!= EOF&&n)
{
e=;
init();
REP(i,n)
{
scanf("%d%d",&u,&v);
add_edge(u,v+,-);
maxn=max(maxn,v+);
minn=min(u,minn);
}
for(int i=minn;i<maxn;i++){
add_edge(i+,i,);
add_edge(i,i+,);
}
spfa(minn);
cout<<-dis[maxn]<<endl;
}
return ;
}
代码君
poj1716 Integer Intervals(差分约束)的更多相关文章
- poj 1716 Integer Intervals (差分约束 或 贪心)
Integer Intervals Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 12192 Accepted: 514 ...
- POJ 1716 Integer Intervals 差分约束
题目:http://poj.org/problem?id=1716 #include <stdio.h> #include <string.h> #include <ve ...
- POJ 1201 Intervals || POJ 1716 Integer Intervals 差分约束
POJ 1201 http://poj.org/problem?id=1201 题目大意: 有一个序列,题目用n个整数组合 [ai,bi,ci]来描述它,[ai,bi,ci]表示在该序列中处于[ai, ...
- 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 ...
- zoj 1508 Intervals (差分约束)
Intervals Time Limit: 10 Seconds Memory Limit: 32768 KB You are given n closed, integer interva ...
- POJ 2101 Intervals 差分约束
Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 27746 Accepted: 10687 Description You ...
- poj 1201 Intervals(差分约束)
题目:http://poj.org/problem?id=1201 题意:给定n组数据,每组有ai,bi,ci,要求在区间[ai,bi]内至少找ci个数, 并使得找的数字组成的数组Z的长度最小. #i ...
- poj1201/zoj1508/hdu1384 Intervals(差分约束)
转载请注明出处: http://www.cnblogs.com/fraud/ ——by fraud Intervals Time Limit: 10 Seconds Mem ...
随机推荐
- <转> 纸牌屋1-4集分析
原文:http://blog.sina.com.cn/s/blog_b86c61490102v56t.html 第一季第一集 主人公弗兰克的出场,是以对待一只邻家将死之狗的态度展开的,充分显示了主人公 ...
- ifstream中文路径问题分析
最近维护一个项目,遇到了ifstream在中文路径下打开文件失败的bug,我搜索了一下,最后整理成下文以后日后查阅. 一.问题重现 vs2008下创建一个简单win32工程. 使用ANSI编码方式:项 ...
- VMware 虚拟机使用RedHat,出现 connect: Network is unreachable解決方法
http://www.linuxidc.com/Linux/2015-02/113119.htm http://www.osyunwei.com/archives/7829.html
- js 按元素向数组中最佳删除元素
追加::: var a = [];// 创建数组 a.push(1); // 添加到最后 a.unshift(); // 添加到第一个位置 删除:::如果你没有使用第三方框架,有类似的扩展功能可以根据 ...
- linux for循环
一定要记得写后面的分号:http://www.runoob.com/linux/linux-shell-variable.html 这个页面的课程的循环教程是有问题的 for color in yel ...
- 手把手教你如何使用webpack+react
上一篇随笔讲述了新手入门入门前端 里面提到的第四阶段跟上当前前端的发展需要入门一个框架和自动化工具,当时推荐的是webpack+react 今天正好有空,也把自己入门webpack + react 的 ...
- jquery 获取多个dom对象的方法
$("[name=trade]").each(function(){ for(j=0;j<info.trade.length;j++){ if( $(this).val() ...
- django-filter 使用Filter来筛选你的数据
django-filter Django-filter is a generic, reusable application to alleviate writing some of the more ...
- 【Python学习】指定两点地理位置经纬度的距离计算
指定两点地理位置经纬度的距离计算 #coding=utf-8 from math import * # input Lat_A 纬度A # input Lng_A 经度A # input Lat_B ...
- LeetCode_Sort Colors
Given an array with n objects colored red, white or blue, sort them so that objects of the same colo ...