POJ 1201 Intervals 差分约束+spfa
2012-09-20 21:37
134 查看
POJ 1201 Intervals 差分约束+spfa
Posted on 2010-04-03 00:23 Initiate 阅读(1492) 评论(3) 编辑 收藏 引用 所属分类: 贪心 、图论Intervals
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 8965 | Accepted: 3318 |
You are given n closed, integer intervals [ai, bi] and n integers c1, ..., cn.
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
The first line of the input contains an integer n (1 <= n <= 50000) -- the number of intervals. The following n lines describe the intervals. The (i+1)-th line of the input contains three integers ai, bi and ci separated by single spaces and such that 0 <= ai <= bi <= 50000 and 1 <= ci <= bi - ai+1.
Output
The output contains exactly one integer equal to the minimal size of set Z sharing at least ci elements with interval [ai, bi], for each i=1,2,...,n.
Sample Input
5
3 7 3
8 10 3
6 8 1
1 3 1
10 11 1
Sample Output
6
思路:
题目的转换真的非常非常巧妙,让我再来梳理一下。本题的题意是给了我们一些区间,然后告诉每个区间中至少需要取Ci个数。求出满足n个条件的集合C的最少的元素个数。
首先第一个转化,是找到一个合理的表示。用ti表示每一个数,如果有用就是1,否则是0。吧S(i+1)定义成S(i+1)=sigma(tj)(1<=j<=i)也就是。S[i+1]表示从0到i有多少个数是需要的。
因此,题目中的条件可以表示成S[bi+1]>=S[ai]+Ci//至少要Ci个
这与bellman中的松弛操作时很像的。因此可以看成一些点
有D[v]>=D[u]+w(u,v)
上式对任何u成立,所以v应该是里面最大的,若D[v]<D[u]+w(u,v)则D[v]=D[u]+w(u,v)
于是。可以从ai和bi+1连一条线,它的长度是ci
这里只有这些条件还是不够的,还要加上两个使其满足整数性质的条件
1>=s[i+1]-s[i]>=0
有了这么多条件,使其自然构成了一个差分约束系统。
用spfa算法得到一个最长路,第一个到最后一个节点的最长路即是需要求的值。
Source Code
1 #include<iostream>
2 #include<vector>//for map
3 #include<queue>//for spfa
4 using namespace std;
5 #define MAXN 50010
6 #define pb push_back
7 int dis[MAXN],used[MAXN];
8 int aa=INT_MAX,bb=-1;//aa最小bb最大
9 struct edge
{
int p;
int len;
}tmp;
vector<edge>map[MAXN];
void spfa()
{
int i,t;
queue<int>Q;
for(i=aa;i<=bb;i++)
dis[i]=-INT_MAX;
dis[aa]=0;
used[aa]=1;//先进一个
Q.push(aa);
while(!Q.empty())
{
t=Q.front();
Q.pop();
used[t]=0;//出队列过后,还可能再进
int nt=map[t].size();
for(i=0;i<nt;i++)
{
if(dis[map[t][i].p]<dis[t]+map[t][i].len)//求最长路
{
dis[map[t][i].p]=dis[t]+map[t][i].len;
if(!used[map[t][i].p])
{
used[map[t][i].p]=1;
Q.push(map[t][i].p);
}
}
}
}
}
int main()
{
int i,n;
scanf("%d",&n);
int u,v,w;
//s(b) - s(a) >= c
for(i=1;i<=n;i++)
{
scanf("%d%d%d",&u,&v,&w);
if(u<aa) aa=u;
if(v+1>bb) bb=v+1;
tmp.len=w;
tmp.p=v+1;
map[u].pb(tmp);
}//添加ci边
0 <= s(i + 1) - s(i) <= 1
for(i=aa;i<=bb;i++)
{
tmp.len=0;//s(i + 1) - s(i) >=0;
tmp.p=i+1;
map[i].pb(tmp);
tmp.len=-1; //s(i ) - s(i+1) >=-1;
tmp.p=i;
map[i+1].pb(tmp);
}//添加0边和-1边
spfa();
printf("%d\n",dis[bb]);
return 0;
}
相关文章推荐
- poj 1201 Intervals 差分约束+spfa
- 【POJ 1201】Intervals(差分约束+SPFA)
- POJ 1201 & HDU1384 & ZOJ 1508 Intervals(差分约束+spfa 求最长路径)
- POJ 1201/hdu 1384 Intervals【差分约束+SPFA】
- POJ 1201 & HDU1384 & ZOJ 1508 Intervals(差分约束+spfa 求最长路径)
- poj 1201 Intervals【差分约束+spfa】
- zoj 1508 | poj 1201 Intervals
- POJ-1201-Intervals
- POJ - 1201 Intervals 【差分约束 + 最长路模型】
- POJ 1201 Intervals 已翻译
- POJ1201 Intervals[差分约束系统]
- POJ 1201 Intervals 差分约束
- [POJ 1201] Intervals (区间贪心选点)
- poj 1201 差分约束+spfa
- poj 1201 Intervals(差分约束)
- poj 1201 Intervals
- POJ1201 Intervals
- poj 1201——Intervals
- POJ 1201/ SOJ 1687: Intervals
- poj1201 Intervals