poj 1716 Integer Intervals(差分约束系统)
2016-01-28 17:29
302 查看
Integer Intervals
Description
An integer interval [a,b], a < b, is a set of all consecutive integers beginning with a and ending with b.
Write a program that: finds the minimal number of elements in a set containing at least two different integers from each interval.
Input
The 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
Output the minimal number of elements in a set containing at least two different integers from each interval.
Sample Input
Sample Output
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 13437 | Accepted: 5711 |
An integer interval [a,b], a < b, is a set of all consecutive integers beginning with a and ending with b.
Write a program that: finds the minimal number of elements in a set containing at least two different integers from each interval.
Input
The 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
Output the minimal number of elements in a set containing at least two different integers from each interval.
Sample Input
4 3 6 2 4 0 2 4 7
Sample Output
4
#include <iostream> #include <cstring> #include <cstdio> #include <cstdlib> #include <cmath> #include <queue> #include <stack> #include <set> #include <map> #include <list> #include <vector> #include <algorithm> #include <iomanip> #define RR freopen("in.txt","r"m,stdin) #define WW freopen("out.txt","w",stdout) #define LL long long #pragma comment(linker, "/STACK:1024000000,1024000000") using namespace std; const int INF = 0x3f3f3f3f; const int MAXN = 100010; const double eps = 1e-9; typedef struct node { int v,w,next; }Edge; Edge edge[MAXN]; int Dis[MAXN*3], head[MAXN], Left, Right,top; bool vis[MAXN]; void Add(int u, int v, int w) { edge[top].v = v; edge[top].w = w; edge[top].next = head[u]; head[u] = top++; } void SPFA() { for(int i=Left; i<=Right; i++) Dis[i] = -INF; queue<int >Q; Dis[Left] = 0; Q.push(Left); while(!Q.empty()) { int u = Q.front(); Q.pop(); vis[u] = false; for(int i=head[u]; i!=-1; i=edge[i].next) { int v = edge[i].v; int w = edge[i].w; if(Dis[v] < Dis[u] + w) { Dis[v] = Dis[u] + w; if(!vis[v]) { Q.push(v); vis[v] = true; } } } } } int main() { int n,u,v; while(~scanf("%d",&n)) { top = 0; memset(head, -1, sizeof(head)); memset(vis, false, sizeof(vis)); memset(edge, 0, sizeof(edge)); Left = INF, Right = -INF; for(int i=0;i<n;i++) { scanf("%d %d",&u, &v); Add(u,v+1,2); Left = min(Left, u); Right = max(Right, v+1); } for(int i=Left; i<Right; i++) { Add(i, i+1, 0); Add(i+1, i, -1); } SPFA(); printf("%d\n",Dis[Right]); } return 0; }
相关文章推荐
- 生产环境业务日志的管理
- android 混淆 proguard 注意
- C#调用Oracle存储过程
- git版本回退知识
- Java解决Emoji表情符问题
- Android 5.0学习之使用Material主题
- iOS 整体框架类图
- JAVA CAS原理深度分析
- Excel批量转换科学计数法为文本格式
- 第三章 授权——《跟我学Shiro》
- Flesch Reading Ease -POJ3371模拟
- Android Thread多线程
- [Asp.net mvc]实体更新异常:存储区更新、插入或删除语句影响到了意外的行数(0)。实体在加载后可能被修改或删除。
- Flesch Reading Ease -POJ3371模拟
- 删除指定目录下的指定后缀的文件
- iOS JSON NSString Convert to NSDictionary
- 微信oauth2.0和自定义菜单携带openid
- 劫持系统调用
- 设计模式之单例模式
- C#委托