poj 1716 poj 1201 差分约束+SPFA

60 篇文章 0 订阅

1716


Integer Intervals
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 13980 Accepted: 5940

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

4
3 6
2 4
0 2
4 7

Sample Output

4




题意:

给出n个区间,每一个区间至少有两个整数

求一个最小区间,能够让这n个区间都至少有两个点在里面

题目中样例的答案可以是   1  2  4  6  这四个点


用数组dist[i]表示从点0到点i-1所包含的关键点的数目;
输入数据即可转化为:dist[b+1]-dist[a]>=2      =>    dist[a]-dist[b+1]<=-2
根据实际情况还有两个约束条件:
dist[i+1]-dist[i]<=1
dist[i+1]-dist[i]>=0    =>   dist[i]-dist[i+1]<=0




#include<queue>
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;

#define INF 0x3f3f3f3f
#define mem(a) memset(a,0,sizeof(a))

int n;
int maxn,minn;
int head[300010],dis[300010];
int vis[300010],num[300010];

struct node
{
    int u,v,w,next;
}path[300010];

int top;
void add(int u,int v,int w)
{
    path[top].v=v;
    path[top].w=w;
    path[top].next=head[u];
    head[u]=top++;
}

void SPFA()
{
    mem(vis);
    for(int i=minn;i<=maxn;i++)
        dis[i]=INF;

    queue<int>q;
    vis[maxn]=1;
    dis[maxn]=0;
    q.push(maxn);

    while(q.size())
    {
        int p=q.front();
        q.pop();
        vis[p]=0;

        for(int i=head[p];i!=-1;i=path[i].next){
            int t=path[i].v;

            if(dis[t]>dis[p]+path[i].w){
                dis[t]=dis[p]+path[i].w;
                if(vis[t]==0){
                    vis[t]=1;
                    q.push(t);
                }
            }
        }
    }
}

int main()
{
    int u,v;
    top=0;
    //freopen("in.txt","r",stdin);
    memset(head,-1,sizeof(head));
    maxn=0,minn=INF;
    scanf("%d",&n);
    for(int i=0;i<n;i++){
        scanf("%d%d",&u,&v);
        maxn=maxn>v+1?maxn:v+1;
        minn=minn<u?minn:u;
        add(v+1,u,-2);
    }

    for(int i=minn;i<=maxn;i++){
        add(i,i+1,1);
        add(i+1,i,0);
    }

    SPFA();
    printf("%d\n",dis[maxn]-dis[minn]);
    return 0;
}









1201

Intervals
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 25116 Accepted: 9576

Description

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



题意:构造一个集合,这个集合内的数字满足所给的n个条件

每个条件都是指在 [ a , b ] 内至少有c个数在集合内

问集合最少包含多少个点。即求至少有多少个元素在区间[a,b]内



和poj 1716一样的代码,就只有一个地方不一样

就是

add(v+1,u,-t);




#include<queue>
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;

#define INF 0x3f3f3f3f
#define mem(a) memset(a,0,sizeof(a))

int n;
int maxn,minn;
int head[300010],dis[300010];
int vis[300010];

struct node
{
    int u,v,w,next;
}path[300010];

int top;
void add(int u,int v,int w)
{
    path[top].v=v;
    path[top].w=w;
    path[top].next=head[u];
    head[u]=top++;
}

void SPFA()
{
    mem(vis);
    for(int i=minn;i<=maxn;i++)
        dis[i]=INF;

    queue<int>q;
    vis[maxn]=1;
    dis[maxn]=0;
    q.push(maxn);

    while(q.size())
    {
        int p=q.front();
        q.pop();
        vis[p]=0;

        for(int i=head[p];i!=-1;i=path[i].next){
            int t=path[i].v;

            if(dis[t]>dis[p]+path[i].w){
                dis[t]=dis[p]+path[i].w;
                if(vis[t]==0){
                    vis[t]=1;
                    q.push(t);
                }
            }
        }
    }
}

int main()
{
    int u,v,t;
    top=0;
    freopen("in.txt","r",stdin);
    memset(head,-1,sizeof(head));
    maxn=0,minn=INF;
    scanf("%d",&n);
    for(int i=0;i<n;i++){
        scanf("%d%d%d",&u,&v,&t);
        maxn=maxn>v+1?maxn:v+1;
        minn=minn<u?minn:u;
        add(v+1,u,-t);
    }

    for(int i=minn;i<maxn;i++){
        add(i,i+1,1);
        add(i+1,i,0);
    }

    SPFA();
    printf("%d\n",dis[maxn]-dis[minn]);
    return 0;
}





理解一下题目,然后再来看下面的解释,应该会有深刻的体会

      如若一个系统由n个变量和m个不等式组成,并且这m个不等式对应的系数矩阵中每一行有且仅有一个1和-1,其它的都为0,这样的系统称为差分约束( difference constraints )系统。引例中的不等式组可以表示成如图三-1-1的系数矩阵。
图三-1-1
      然后继续回到单个不等式上来,观察 x[i] - x[j] <= a[k], 将这个不等式稍稍变形,将x[j]移到不等式右边,则有x[i] <= x[j] + a[k],然后我们令a[k] = w(j, i),再将不等式中的i和j变量替换掉,i = v, j = u,将x数组的名字改成d(以上都是等价变换,不会改变原有不等式的性质),则原先的不等式变成了以下形式:d[u] + w(u, v) >= d[v]。
      这时候联想到SPFA中的一个松弛操作:
     if (d[u]  +  w(u, v)  <  d[v]) {
        d[v]  =  d[u]  +  w(u, v);
    }
      对比上面的不等式,两个不等式的不等号正好相反,但是再仔细一想,其实它们的逻辑是一致的,因为SPFA的松弛操作是在满足小于的情况下进行松弛,力求达到d[u] + w(u, v) >= d[v],而我们之前令a[k] = w(j, i),所以我们可以将每个不等式转化成图上的有向边:
      对于每个不等式 x[i] - x[j] <= a[k],对结点 j 和 i 建立一条 j -> i的有向边,边权为a[k],求x[n-1] - x[0] 的最大值就是求 0 到n-1的最短路。
图三-1-2
      图三-1-2 展示了 图三-1-1的不等式组转化后的图。

      2、三角不等式
      如果还没有完全理解,我们可以先来看一个简单的情况,如下三个不等式:
B - A <= c      (1)
C - B <= a      (2)
C - A <= b      (3)
      我们想要知道C - A的最大值,通过(1) + (2),可以得到 C - A <= a + c,所以这个问题其实就是求min{b, a+c}。 将上面的三个不等式按照  三-1 数形结合 中提到的方式建图,如图三-2-1所示。
图三-2-1
      我们发现min{b, a+c}正好对应了A到C的最短路,而这三个不等式就是著名的三角不等式。将三个不等式推广到m个,变量推广到n个,就变成了n个点m条边的最短路问题了。


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值