POJ-1201-Intervals(差分约束)
来源:互联网 发布:linux怎么删除文件夹 编辑:程序博客网 时间:2024/04/29 23:59
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
53 7 38 10 36 8 11 3 110 11 1
Sample Output
6
Source
首先讲一下什么是差分约束:
源自:http://www.cnblogs.com/void/archive/2011/08/26/2153928.html
比如给出三个不等式,b-a<=k1,c-b<=k2,c-a<=k3,求出c-a的最大值,我们可以把a,b,c转换成三个点,k1,k2,k3是边上的权,如图
由题我们可以得知,这个有向图中,由题b-a<=k1,c-b<=k2,得出c-a<=k1+k2,因此比较k1+k2和k3的大小,求出最小的就是c-a的最大值了
根据以上的解法,我们可能会猜到求解过程实际就是求从a到c的最短路径,没错的....简单的说就是从a到c沿着某条路径后把所有权值和k求出就是c -a<=k的一个
推广的不等式约束,既然这样,满足题目的肯定是最小的k,也就是从a到c最短距离...
总结一下:
//差分约束
//求最大值:变为x-y<=k的标准形式,求出最短路径
//求最小值:变为x-y>=k的标准形式,求出最长路径
回到本题:
大致题意:
给出数轴上的n个区间[ai,bi],每个区间都是连续的int区间。
现在要在数轴上任意取一堆元素,构成一个元素集合V
要求每个区间[ai,bi]和元素集合V的交集至少有ci不同的元素
求集合V最小的元素个数。
显然我们可以令S[i]为start->i区间与V交集的不同元素个数,
那么我们可以知道S[b] - S[a-1] >= c,又0<=S[i]-S[i-1]<=1;
所以得到以下三个约束关系:
// S[b] - S[a-1] >= c
// S[i] - S[i-1] >= 0
// S[i-1] - S[i] >= -1
建图,题意求最小值,则求出最长路径即可。
CODE:
#include <iostream>#include <cstdio>#include <cstring>#include <set>#include <queue>#include <cmath>#include <algorithm>using namespace std;#define INF 0x3f3f3f3f#define bug cout<<"bug\n"const int MAXN = 50007;const int MAXM = 200007;int head[MAXN],index;struct node{ int v,next,cost;} edge[MAXM];void add_edge(int u,int v,int cost){ edge[index].v=v; edge[index].cost=cost; edge[index].next=head[u]; head[u]=index++;}bool vis[MAXN];int dist[MAXN];int SPFA(int s,int t){ memset(vis,0,sizeof(vis)); for(int i=s; i<=t; ++i)dist[i]=-INF; dist[s]=0; queue<int> q; while(!q.empty())q.pop(); q.push(s); while(!q.empty()) { int u=q.front(); q.pop(); vis[u]=0; for(int i=head[u]; i+1; i=edge[i].next) { int v=edge[i].v; if(dist[v]<dist[u]+edge[i].cost) { dist[v]=dist[u]+edge[i].cost; if(!vis[v]) { vis[v]=1; q.push(v); } } } } return dist[t];}//差分约束:- -//求最大值:变为x-y<=k的标准形式, 求出最短路径//求最小值:变为x-y>=k的标准形式,求出最长路径//S[b] - S[a-1] >= c//S[i] - S[i-1] >= 0//S[i-1] - S[i] >= -1int main(){ int n,a,b,c; while(scanf("%d",&n)!=EOF) { index=0; memset(head,-1,sizeof(head)); int maxx=0,minx=INF; for(int i=0; i<n; ++i) { scanf("%d%d%d",&a,&b,&c); add_edge(a,b+1,c); minx=min(minx,a); maxx=max(b+1,maxx); } for(int i=minx; i<maxx; ++i) { add_edge(i,i+1,0); add_edge(i+1,i,-1); } cout<<SPFA(minx,maxx)<<endl; } return 0;}
- poj 1201 Intervals(差分约束)
- POJ 1201 Intervals(差分约束)
- poj 1201 Intervals (差分约束)
- poj 1201 Intervals(差分约束)
- poj 1201 Intervals(差分约束)
- POJ-1201-Intervals(差分约束)
- POJ 1201 Intervals(差分约束)
- POJ-1201 Intervals(差分约束)
- POJ 1201 Intervals (差分约束)
- POJ 1201 Intervals 差分约束+spfa
- poj 1201 Intervals 差分约束 + spfa
- POJ 1201 Intervals [差分约束]
- POJ 1201 Intervals 差分约束系统
- POJ 1201 Intervals (差分约束)
- poj 1201 Intervals 差分约束+spfa
- POJ 1201 Intervals (差分约束)
- poj-1201- Intervals-差分约束问题
- POJ 1201 Intervals 差分约束系统
- css3高级滤镜-切换滤镜
- java中abstract怎么使用
- 《More Effective C++》学习笔记之一引用与指针
- ZOJ 3860: - Find the Spy
- 对数据库数据操作之查找
- POJ-1201-Intervals(差分约束)
- 未加载mfc120ud.i386.pdb:VS2013调试MFC程序解决方法
- css3高级滤镜-光照滤镜
- 在基于ZedBoard的Linux系统下添加自定义硬件IP(2)——自定义硬件IP设计
- springmvc中复杂数据绑定以及表单回显实现
- iOS多线程
- Container With Most Water
- Oracle之分组查询
- ZOJ 3861 - Valid Pattern Lock