poj 1201(查分约束之spfa)

Intervals

Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 21758   Accepted: 8191

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

AC代码:

#include<cstring>
#include<iostream>
#include<stdio.h>
#include<stack>
using namespace std;
struct Node{
    int v,w;
    int next;
}Edge[150010];
int n,k;
int mx,mn;
int head[50005];
int spfa(){
    stack <int> st;
    int vis[50005],dis[50005];
    for(int i=mn;i<=mx;i++){
        vis[i]=0;
        dis[i]=-999999;
    }
    dis[mn]=0;
    st.push(mn);
    while(!st.empty()){
        int u=st.top(); st.pop();
        vis[u]=0;
        for(int i=head[u];i;i=Edge[i].next){
            int v=Edge[i].v;
            if(dis[v]<dis[u]+Edge[i].w){
                dis[v]=dis[u]+Edge[i].w;
                if(!vis[v]){
                    st.push(v);
                    vis[v]=1;
                }
            }
        }
    }
    return dis[mx];
}
int main(){
    while(scanf("%d",&n)!=EOF){
        memset(head,0,sizeof(head));
        mn=999999; mx=0;
        k=0;
        while(n--){
            int x,y,s;
            scanf("%d%d%d",&x,&y,&s);
            y++;
            mn=min(x,mn); mx=max(y,mx);
            k++;
            Edge[k].v=y; Edge[k].w=s;
            Edge[k].next=head[x];
            head[x]=k;
        }
        for(int i=mn;i<mx;i++){
            k++;
            Edge[k].v=i+1; Edge[k].w=0;
            Edge[k].next=head[i];
            head[i]=k;

            k++;
            Edge[k].v=i; Edge[k].w=-1;
            Edge[k].next=head[i+1];
            head[i+1]=k;
        }
        printf("%d\n",spfa());
    }
    return 0;
}
时间: 2024-07-31 14:26:39

poj 1201(查分约束之spfa)的相关文章

POJ 2983 查分约束+SPFA

点击打开链接 题意:n个点,m个条件,P,a,b,c代表a到c的距离为c,V,a,b代表a到b的距离大于等于1,问所有条件是否可以成立 思路:看了查分约束来做这道题,还可以不是很难.若固定了位置则可以写出两个表达式a-b>=c&&b-a<=c:另一个则是a-b>=c,将条件全部转化为<=的,则可以变成查分约束,用SPFA判断有没有负环即可,但是这题要注意的是,图可能不是联通的,那么我们可以有两种方法,我们可以将全部的点都压进队列,还可以建立一个源点0,与每个点的距离

POJ 3159 Candies(查分约束)

Description: During the kindergarten days, flymouse was the monitor of his class. Occasionally the head-teacher brought the kids of flymouse’s class a large bag of candies and had flymouse distribute them. All the kids loved candies very much and oft

【查分约束】我爱你啊

某看了秒5而内心激动的人创作本题 我爱你啊 [题目描述]呐,贵树真的是一个很帅的男孩子呢,所以好多女孩都给他写至少一封了情书.那每个女孩给了贵树写了多少情书呢?我们不知道,但是我们知道一些女孩子写情书数量的关系,你的任务是推断出贵树最少受到了多少情书.[输入文件]输入的第一行为两个整数 N,K,表示一共 N 个女孩,知道 K 对关系接下来 K 行,每行三个整数 t,A,B如果 t=1,则表示 A 的情书和 B 的情书数量一样如果 t=2,则表示 A 的情书少于 B 的情书数量如果 t=3,则表示

洛谷P1993 小 K 的农场(查分约束)

/* 加深一下对查分约束的理解 建图的时候为了保证所有点联通 虚拟一个点 它与所有点相连 权值为0 然后跑SPFA判负环 这题好像要写dfs的SPFA 要不超时 比较懒 改了改重复进队的条件~ */ #include<iostream> #include<cstdio> #include<cstring> #include<queue> #define maxn 40010 using namespace std; int n,m,num,head[maxn

HDU 3592 查分约束+判环

点击打开链接 题意:有N个人,然后X个关系和Y个关系,X关系代表的是这两个人的距离不能超过C,Y代表的是这两个人的距离要大于等于C,若不能满足所有的输出-1,若1与N的位置可以无穷大输出-2,否则输出两个人的最大距离 思路:就是查分约束的模型,X个关系按照位置建边,Y也是直接按位置建边就行了,就是两个初始方向不同,然后有负环的话就输出-1,距离无穷大输出-2,否则就是dis[N]的值就行了,自己讨论一下就可以出来了 #include <stdio.h> #include <string.

HDU 3440 查分约束

点击打开链接 题意:给个n个不同的高度,一个人从最低点跳跃,每次可以跳到第一个比它高的位置,最后跳到最高点,然后每次最多可以跳的距离为D,而且在跳跃时可以在不改变给定顺序的情况下移动这些高度,使得最后起始点和终点的位置最远, 思路:自己想了一会,想的方向错了,我自己想的方法是将最小高度记为0,最大高度记为n-1,然后写查分约束方程,这了一会发现条件不足,没想法了,看了大牛们的解法发现原来以给定的顺序直接进行条件就可以了,而且好简单,因为我们不能调整给定的顺序,那么对于给定的顺序就可以有pos(i

HDU 1384 查分约束

点击打开链接 题意:给了n个区间,要求每个区间至少有C个数字出现,问满足的最小的数字个数 思路:用Si代表0到i的区间内的数字个数,然后可以写出查分约束方程,对于一个区间则Sa-S(b-1)>=C的,然后隐含的一个条件就是Si-S(i-1)>=0且<=1的,然后泡个最长路就行,对于查分约束系统求最大值是<=的形式,而求最小值则是>=的形式 #include <queue> #include <stdio.h> #include <stdlib.h

HDU 1534 查分约束

点击打开链接 题意:要完成n个任务,每个任务有完成的时间,然后下面给了四种条件,问最快完成的情况下,每一个任务的开始时间 思路:四个条件就是给出的四个查分约束方程,然后因为是要时间最短,所以求得是最长路,但是因为没有起点和终点,那么我们可以添加一个起点,使它与任务点相连一个0的边,最后求出的距离就是开始的时间,然后有环则是impossible #include <queue> #include <vector> #include <stdio.h> #include &

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个,