POJ 1716 Integer Intervals 差分约束
题目:http://poj.org/problem?id=1716
#include <stdio.h>
#include <string.h>
#include <vector>
#include <queue> const int INF = 0x3f3f3f3f; struct Edge
{
int v, w;
Edge(){};
Edge(int v, int w)
{
this->v = v;
this->w = w;
}
}; std::vector<Edge>map[];
int dist[];
bool inque[]; void spfa(int s)
{
std::queue<int>q;
memset(inque, , sizeof(inque));
dist[s] = ;
q.push(s);
inque[s] = ;
while(!q.empty())
{
int u = q.front();
q.pop();
inque[u] = ;
for(int i = ; i < map[u].size(); i++)
{
int v = map[u][i].v;
if(dist[v] < dist[u] + map[u][i].w)
{
dist[v] = dist[u] + map[u][i].w;
if(!inque[v])
{
q.push(v);
inque[v] = ;
}
}
}
}
} int main()
{
int n;
int x, y;
while(scanf("%d", &n) != EOF)
{
for(int i = ; i <= ; i++)
{
map[i].clear();
}
int low = INF, high = ;
for(int i = ; i < n; i++)
{
scanf("%d %d", &x, &y);
map[x].push_back(Edge(y+, ));
if(low > x)low = x;
if(high < y)high = y;
}
for(int i = low; i <= high+; i++)
{
map[].push_back(Edge(i, ));
map[i].push_back(Edge(i+, ));
map[i+].push_back(Edge(i, -));
dist[i] = -INF;
}
map[].push_back(Edge(high+, ));
spfa(low);
printf("%d\n", dist[high+] - dist[low]);
}
return ;
}
POJ 1716 Integer Intervals 差分约束的更多相关文章
- poj 1716 Integer Intervals (差分约束 或 贪心)
Integer Intervals Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 12192 Accepted: 514 ...
- POJ 1201 Intervals || POJ 1716 Integer Intervals 差分约束
POJ 1201 http://poj.org/problem?id=1201 题目大意: 有一个序列,题目用n个整数组合 [ai,bi,ci]来描述它,[ai,bi,ci]表示在该序列中处于[ai, ...
- poj 1716 Integer Intervals(差分约束)
1716 -- Integer Intervals 跟之前个人赛的一道二分加差分约束差不多,也是求满足条件的最小值. 题意是,给出若干区间,需要找出最少的元素个数,使得每个区间至少包含两个这里的元素. ...
- POJ 1716 Integer Intervals
题意:给出一些区间,求一个集合的长度要求每个区间里都至少有两个集合里的数. 解法:贪心或者差分约束.贪心的思路很简单,只要将区间按右边界排序,如果集合里最后两个元素都不在当前区间内,就把这个区间内的最 ...
- POJ 1716 Integer Intervals#贪心
(- ̄▽ ̄)-* //求一个集合,这个集合与任意一个区间的交集,需至少有两个数字 //贪心过程:按n个区间的最右值从小到大对区间进行排列, //集合首先取第一个区间的最右两个数字, //到第二个区间, ...
- 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 都 ...
- POJ 2101 Intervals 差分约束
Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 27746 Accepted: 10687 Description You ...
- POJ 3159 Candies(差分约束,最短路)
Candies Time Limit: 1500MS Memory Limit: 131072K Total Submissions: 20067 Accepted: 5293 Descrip ...
随机推荐
- [AngularJS] angular-formly: Default Options
angular-formly allows you to keep your forms as DRY as possible. TheoptionsTypes property is one way ...
- keepalived + haproxy 实现web 双主模型的高可用负载均衡--转
原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法律责任.http://xz159065974.blog.51cto.com/8618592/140581 ...
- find——文件查找命令 linux一些常用命令
find 命令eg: 一般文件查找方法: 1. find /home -name file , 在/home目录下查找文件名为file的文件2. find /home -name '*file ...
- 关于字符串计算size的方法比较
往往字符串需要计算size来满足UI排版的自适应的需要,而一般字符串也是放在UILabel里的. 而在计算size的方法里,一般有两种方式(deprecated的就不说了). NSString的方法 ...
- javascript源码阅读推荐
作者:马 岩(Furzoom) (http://www.cnblogs.com/furzoom/)版权声明:本文的版权归作者与博客园共同所有.转载时请在明显地方注明本文的详细链接,未经作者同意请不要删 ...
- php加载memcache
安装php加载memcache[root@web-server ~]# rpm -qa | grep libevent [root@web-server ~]# yum -y install libe ...
- C#扫盲之:前台线程后台线程
1.线程分类 线程由程序员创建,可是创建的方式不同,总体来说有两种,一种是个人构造,也就是使用thread类new线程对象创建,这一类线程是大部分程序员知道的,也叫专用线程;还有一种是由CLR创建,这 ...
- magento addFieldToFilter()方法常用的过滤条件
记录一下Magento模型集合Model Collection中addFieldToFilter()方法常用的过滤条件.以下参数也同样适用于产品实体的addAttributeToFilter()方法. ...
- 调试环境部署续:vs远程调试
原文http://www.bitscn.com/weixin/464994.html 第一步 IIS的配置 进入iis,点击网址,选择你的网站,在窗口的右边编辑网站中点击绑定,如图所示. 进入网站绑 ...
- ADB操作多台设备
1.adb devices 查看所有连接设备. 2.adb -s <设备名> shell 指定device来执行adb shell. 3.adb -s <设备名> <指令 ...