POJ 1201 Intervals(差分约束+spfa 求最长路径)

题目链接:http://poj.org/problem?id=1201

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

Source

Southwestern Europe 2002

题意:(转)

[ai, bi]区间内和点集Z至少有ci个共同元素,那也就是说如果我用Si表示区间[0,i]区间内至少有多少个元素的话,那么Sbi - Sai >= ci,这样我们就构造出来了一系列边,权值为ci,但是这远远不够,因为有很多点依然没有相连接起来(也就是从起点可能根本就还没有到终点的路线),此时,我们再看看Si的定义,也不难写出0<=Si
- Si-1<=1的限制条件,虽然看上去是没有什么意义的条件,但是如果你也把它构造出一系列的边的话,这样从起点到终点的最短路也就顺理成章的出现了。

我们将上面的限制条件写为同意的形式:

Sbi - Sai >= ci

Si - Si-1 >= 0

Si-1 - Si >= -1

这样一来就构造出了三种权值的边,而最短路自然也就没问题了。

但要注意的是,由于查分约束系统里常常会有负权边,所以为了避免负权回路,往往用Bellman-Ford或是SPFA求解(存在负权回路则最短路不存在)。

PS:

因为求的是[ai,bi]区间,所以我们添加边的时候需要(u-1, v, w)!

把距离dis初始化为负无穷, if(dis[v] < dis[u] + w)即可!

代码如下:

#include <cstdio>
#include <cstring>
#include <stack>
#include <iostream>
#include <algorithm>
using namespace std;
#define INF 0x3f3f3f3f
#define N 50017
#define M 50017
int n, m, k;
int Edgehead[N], dis[N];
struct Edge
{
    int v,w,next;
} Edge[3*M];
bool vis[N];
//int cont[N];
int minn, maxx;
int MIN(int a, int b)
{
    if(a < b)
        return a;
    return b;
}
int MAX(int a, int b)
{
    if(a > b)
        return a;
    return b;
}
void Addedge(int u, int v, int w)
{
    Edge[k].next = Edgehead[u];
    Edge[k].w = w;
    Edge[k].v = v;
    Edgehead[u] = k++;
}
int SPFA( int start)//stack
{
    int sta[N];
    int top = 0;
    for(int i = 1 ; i <= n ; i++ )
        dis[i] = -INF;
    dis[start] = 0;
    //++cont[start];
    memset(vis,false,sizeof(vis));
    sta[++top] = start;
    vis[start] = true;
    while(top)
    {
        int u = sta[top--];
        vis[u] = false;
        for(int i = Edgehead[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] )//防止出现环
                {
                    sta[++top] = v;
                    vis[v] = true;
                }
                //	if(++cont[v] > n)//有负环
                //		return -1;
            }
        }
    }
    return dis[maxx];
}
int main()
{
    int u, v, w;
    while(~scanf("%d",&n))//n为目的地
    {
        k = 1;
        memset(Edgehead,-1,sizeof(Edgehead));
        minn = INF;
        maxx = -1;
        for(int i = 1 ; i <= n ; i++ )
        {
            scanf("%d%d%d",&u,&v,&w);
            Addedge(u-1,v,w);
            maxx = MAX(v,maxx);
            minn = MIN(u-1,minn);

        }
        for(int i = minn; i <= maxx; i++)//新边,保证图的连通性还必须添加每相邻两个整数点i,i+1的边
        {
            Addedge(i,i+1,0);
            Addedge(i+1,i,-1);
        }
        int ans = SPFA(minn);//从点minn开始寻找最短路
        printf("%d\n",ans);
    }
    return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-11-03 14:21:23

POJ 1201 Intervals(差分约束+spfa 求最长路径)的相关文章

POJ 1201 Intervals 差分约束

http://poj.org/problem?id=1201 TLE了很久,因为用了cin..... 思路和其他差分约束差不多,http://www.cppblog.com/menjitianya/archive/2015/11/19/212292.html 如果区间[a, b]中至少有c个元素,如果用上面的博客,那么说明xa - xb >= c,但是注意这里是闭区间,xa - xb是不包括b这个点的, 就比如用了[a, b]有c个元素,[b, d]有x个,那么ans = c + x - 1个,

POJ 3169 Layout (差分约束+SPFA)

Layout Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6832   Accepted: 3292 Description Like everyone else, cows like to stand close to their friends when queuing for feed. FJ has N (2 <= N <= 1,000) cows numbered 1..N standing along a

POJ 2101 Intervals 差分约束

Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 27746   Accepted: 10687 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

poj 1201 Intervals(差分约束系统)(困难)

Intervals Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 23205   Accepted: 8764 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 po

poj 3159 candies (差分约束 spfa+stack)

http://poj.org/problem?id=3159 题意:一个班有n个人 每人分到若干糖果 且u的糖果数不能比v少w个 求第1个人与第n个人最大数量差 照着模板spfa+queue果断tle了 之后照着题解说的把queue改成stack就过了 但是还不明白为什么会快 而且如果用数组直接模拟会比stl更快 #include<cstdio> #include<cstring> #include<iostream> #include<algorithm>

poj 1201 Intervals 差分约束系统

题目链接:http://poj.org/problem?id=1201 题意:给定n(1<= n <= 50000)个 闭区间,每个区间后面带一个值 Ci, 问集合Z个数的最小值使得在每个区间中的数的个数 “不少于Ci”? 思路: S[i] 表示 小于等于i 的个数,这样可以直接按照输入建立不等式之后转化为有向网即可: 需要注意的是 在原始的两个不等式 S[i-1] - s[i] <= 0 和 s[i-1] - s[i] <= 1不宜在建边时就加入,这会使得有向网络中的边数达到3*

POJ 1201 Intervals(图论-差分约束)

Intervals Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 20779   Accepted: 7863 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 po

UVa 515 - King (差分约束系统 + SPFA求带负权最短路)

下面是差分约束系统的详细介绍,以及解决方法~ 摘抄自 xuezhongfenfei(他好像也是转的....) 差分约束系统 X1 - X2 <= 0 X1 - X5 <= -1 X2 - X5 <= 1 X3 - X1 <= 5 X4 - X1 <= 4 X4 - X3 <= -1 X5 - X3 <= -3 X5 - X4 <= -3 不等式组(1) 全都是两个未知数的差小于等于某个常数(大于等于也可以,因为左右乘以-1就可以化成小于等于).这样的不等式组

POJ 1364 King --差分约束第一题

题意:求给定的一组不等式是否有解,不等式要么是:SUM(Xi) (a<=i<=b) > k (1) 要么是 SUM(Xi) (a<=i<=b) < k (2) 分析:典型差分约束题,变换,令Ti = SUM(Xj) (0<=j<=i).  则表达式(1)可以看做T(a+b)-T(a-1) > k,也就是T(a-1)-T(a+b) < -k,又因为全是整数,所以T(a-1)-T(a+b) <= -k-1.  同理,(2)看做T(a+b)-T(