用s[i]表示小于等于i的元数个数
可得约束条件为:s[node.from]-s[node.to]<-c,隐含的为0<=s[i]-s[i-1]<=1
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
const int MAXN=50010;
struct cnode
{
int from;
int to;
int w;
}node[MAXN];
int d[MAXN];
int n;
int mmin;
int mmax;
bool bellman_ford()
{
memset(d,0,sizeof(d));
bool flag=1;
bool f;
int i;
while(flag)
{
flag=0;
for(i=0;i<n;i++)
{
if(d[node[i].from]>d[node[i].to]+node[i].w)//from-to<c
{
d[node[i].from]=d[node[i].to]+node[i].w;
flag=1;
}
}
for(i=mmin;i<=mmax;i++)
{
if(d[i-1]+1<d[i])//0<=d[i]-d[i-1]<=1
{
d[i]=d[i-1]+1;
f=1;
}
}
for(i=mmax;i>=mmin;i--)
{
if(d[i-1]>d[i])
{
d[i-1]=d[i];
f=1;
}
}
}
return true;
}
int main()
{
int FROM,TO,W;
int i;
while(scanf("%d",&n)!=EOF)
{
mmin=800000000;
mmax=-100;
for(i=0;i<n;i++)
{
scanf("%d %d %d",&FROM,&TO,&W);
node[i].from=FROM-1;
node[i].to=TO;
node[i].w=-W;
if(FROM<mmin)
{
mmin=FROM;
}
if(TO>mmax)
{
mmax=TO;
}
}
bellman_ford();
printf("%d/n",d[mmax]-d[mmin-1]);
return 0;
}
return 0;
}
附题目:
| Time Limit: 2000MS | Memory Limit: 65536K | |
| Total Submissions: 13145 | Accepted: 4860 |
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
5 3 7 3 8 10 3 6 8 1 1 3 1 10 11 1
Sample Output
6
本文介绍了一个区间问题的求解算法,通过Bellman-Ford算法计算最小集合大小,使得该集合与每个给定区间至少有ci个公共元素。适用于解决特定类型的最优化问题。
715

被折叠的 条评论
为什么被折叠?



